1 /*
2  * 	pbmimage.cpp		(C) 2006, Aurélien Croc (AP²C)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the
15  *  Free Software Foundation, Inc.,
16  *  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  *  $Id$
19  *
20  */
21 #include "pbmimage.h"
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include "error.h"
29 
30 /*
31  * Constructeur - Destructeur
32  * Init - Uninit
33  */
PbmImage(const char * black,const char * cyan,const char * magenta,const char * yellow)34 PbmImage::PbmImage(const char *black, const char *cyan, const char *magenta,
35     const char *yellow)
36 {
37     _color = !cyan && !magenta && !yellow ? false : true;
38     _blackFile = black;
39     _cyanFile = cyan;
40     _magentaFile = magenta;
41     _yellowFile = yellow;
42     _black = NULL;
43     _cyan = NULL;
44     _magenta = NULL;
45     _yellow = NULL;
46 
47     _width = 0;
48     _height = 0;
49     _lineSize = 0;
50     _line = 0;
51     _lineBuffer = NULL;
52     _currentColor = 1;
53 
54 }
55 
~PbmImage()56 PbmImage::~PbmImage()
57 {
58     unload();
59 }
60 
61 
62 
63 /*
64  * Chargement de l'image
65  * Load the image
66  */
unload()67 void PbmImage::unload()
68 {
69     if (_black) {
70         fclose(_black);
71         _blackFile = NULL;
72         _black = NULL;
73     }
74     if (_color) {
75         if (_cyan) {
76             fclose(_cyan);
77             _cyanFile = NULL;
78             _cyan = NULL;
79         }
80         if (_magenta) {
81             fclose(_magenta);
82             _magentaFile = NULL;
83             _magenta = NULL;
84         }
85         if (_yellow) {
86             fclose(_yellow);
87             _yellowFile = NULL;
88             _yellow = NULL;
89         }
90     }
91     if (_lineBuffer) {
92         delete[] _lineBuffer;
93         _lineBuffer= NULL;
94     }
95 }
96 
load()97 int PbmImage::load()
98 {
99     uint32_t width, height;
100     char buffer[1024];
101 
102     // Open the different files
103     if (!(_black = fopen(_blackFile, "r"))) {
104         fprintf(stderr, _("Cannot open black file %s\n"), _blackFile);
105         return -1;
106     }
107     if (_color) {
108         if (_cyanFile && !(_cyan = fopen(_cyanFile, "r"))) {
109             fprintf(stderr, _("Cannot open cyan file %s\n"), _cyanFile);
110             return -1;
111         }
112         if (_magentaFile && !(_magenta = fopen(_magentaFile, "r"))) {
113             fprintf(stderr, _("Cannot open magenta file %s\n"), _magentaFile);
114             return -1;
115         }
116         if (_yellowFile && !(_yellow = fopen(_yellowFile, "r"))) {
117             fprintf(stderr, _("Cannot open yellow file %s\n"), _yellowFile);
118             return -1;
119         }
120     }
121 
122     // Read the PBM header
123     fgets((char *)&buffer, sizeof(buffer), _black);
124     if (strcmp((char *)&buffer, "P4\n")) {
125         fprintf(stderr, _("Invalid PBM file for file %s\n"), _blackFile);
126         return -1;
127     }
128     fgets((char *)&buffer, sizeof(buffer), _black);
129     fscanf(_black, "%u %u\n", &width, &height);
130     _width = width;
131     _height = height;
132     _lineSize = (width + 7) >> 3;
133 
134     if (_color) {
135         unsigned int tmpW, tmpH;
136 
137         if (_cyan) {
138             fgets((char *)&buffer, sizeof(buffer), _cyan);
139             if (strcmp((char *)&buffer, "P4\n")) {
140                 fprintf(stderr, _("Invalid PBM file for file %s\n"), _cyanFile);
141                 return -1;
142             }
143             fgets((char *)&buffer, sizeof(buffer), _cyan);
144             fscanf(_cyan, "%u %u\n", &tmpW, &tmpH);
145             if ((tmpW != width) || (tmpH != height)) {
146                 fprintf(stderr, _("The different PBM layers must have the same "
147                     "size\n"));
148                 return -1;
149             }
150         }
151         if (_magenta) {
152             fgets((char *)&buffer, sizeof(buffer), _magenta);
153             if (strcmp((char *)&buffer, "P4\n")) {
154                 fprintf(stderr, _("Invalid PBM file for file %s\n"),
155                     _magentaFile);
156                 return -1;
157             }
158             fgets((char *)&buffer, sizeof(buffer), _magenta);
159             fscanf(_magenta, "%u %u\n", &tmpW, &tmpH);
160             if ((tmpW != width) || (tmpH != height)) {
161                 fprintf(stderr, _("The different PBM layers must have the same "
162                     "size\n"));
163                 return -1;
164             }
165         }
166         if (_yellow) {
167             fgets((char *)&buffer, sizeof(buffer), _yellow);
168             if (strcmp((char *)&buffer, "P4\n")) {
169                 fprintf(stderr, _("Invalid PBM file for file %s\n"),
170                     _yellowFile);
171                 return -1;
172             }
173             fgets((char *)&buffer, sizeof(buffer), _yellow);
174             fscanf(_yellow, "%u %u\n", &tmpW, &tmpH);
175             if ((tmpW != width) || (tmpH != height)) {
176                 fprintf(stderr, _("The different PBM layers must have the same "
177                     "size\n"));
178                 return -1;
179             }
180         }
181     }
182 
183     return 0;
184 }
185 
loadPage(Printer * printer)186 int PbmImage::loadPage(Printer *printer)
187 {
188     printer->setCompVersion(0x11);
189     if (_line)
190         return 1;
191 
192     return 0;
193 }
194 
195 
196 
197 /*
198  * Lecture d'une ligne
199  * Read a line
200  */
readLine()201 int PbmImage::readLine()
202 {
203     if (!_lineBuffer)
204         _lineBuffer = new unsigned char[_lineSize];
205 
206     if (_line >= _height) {
207         memset(_lineBuffer, 0x00, _lineSize);
208         return _lineSize;
209     }
210 
211     if (_color) {
212         switch(_currentColor) {
213             case 1:
214                 if (_cyan)
215                     fread(_lineBuffer, 1, _lineSize, _cyan);
216                 else
217                     memset(_lineBuffer, 0x00, _lineSize);
218                 _currentColor++;
219                 break;
220             case 2:
221                 if (_magenta)
222                     fread(_lineBuffer, 1, _lineSize,
223                             _magenta);
224                 else
225                     memset(_lineBuffer, 0x00, _lineSize);
226                 _currentColor++;
227                 break;
228             case 3:
229                 if (_yellow)
230                     fread(_lineBuffer, 1, _lineSize,
231                             _yellow);
232                 else
233                     memset(_lineBuffer, 0x00, _lineSize);
234                 _currentColor++;
235                 break;
236             case 4:
237                 fread(_lineBuffer, 1, _lineSize, _black);
238                 _currentColor = 1;
239                 _line++;
240                 break;
241         }
242     } else {
243         fread(_lineBuffer, 1, _lineSize, _black);
244         _line++;
245     }
246     return _lineSize;
247 }
248 
249 /* vim: set expandtab tabstop=4 shiftwidth=4 smarttab tw=80 cin enc=utf8: */
250 
251