Debian Codegraph @

pTk/mTk/generic/tkImgPPM.c

raw symbols SCIP
1 /*
2 * tkImgPPM.c --
3 *
4 * A photo image file handler for PPM (Portable PixMap) files.
5 *
6 * Copyright (c) 1994 The Australian National University.
7 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * Author: Paul Mackerras ([email protected]),
13 * Department of Computer Science,
14 * Australian National University.
15 *
16 * RCS: @(#) $Id: tkImgPPM.c,v 1.10 2002/06/14 13:35:48 dkf Exp $
17 */
18
19 #if 0
20 /*
21 * The way perl/Tk's equivelent to stubs works makes it
22 * nessary to convert this to new style
23 */
24 #define USE_OLD_IMAGE
25 #endif
26
27 #include "tkInt.h"
28 #include "tkPort.h"
29 #include "tkVMacro.h"
30
31 /*
32 * The maximum amount of memory to allocate for data read from the
33 * file. If we need more than this, we do it in pieces.
34 */
35
36 #define MAX_MEMORY 10000 /* don't allocate > 10KB */
37
38 /*
39 * Define PGM and PPM, i.e. gray images and color images.
40 */
41
42 #define PGM 1
43 #define PPM 2
44
45 /*
46 * The format record for the PPM file format:
47 */
48
49 static int FileMatchPPM _ANSI_ARGS_((Tcl_Channel chan,
50 Tcl_Obj *fileName, Tcl_Obj *formatString,
51 int *widthPtr, int *heightPtr, Tcl_Interp *interp));
52 static int FileReadPPM _ANSI_ARGS_((Tcl_Interp *interp,
53 Tcl_Channel chan, Tcl_Obj *fileName,
54 Tcl_Obj *formatString, Tk_PhotoHandle imageHandle,
55 int destX, int destY, int width, int height,
56 int srcX, int srcY));
57 static int FileWritePPM _ANSI_ARGS_((Tcl_Interp *interp,
58 char *fileName, Tcl_Obj *formatString,
59 Tk_PhotoImageBlock *blockPtr));
60
61 Tk_PhotoImageFormat tkImgFmtPPM = {
62 "PPM", /* name */
63 FileMatchPPM, /* fileMatchProc */
64 NULL, /* stringMatchProc */
65 FileReadPPM, /* fileReadProc */
66 NULL, /* stringReadProc */
67 FileWritePPM, /* fileWriteProc */
68 NULL, /* stringWriteProc */
69 };
70
71 /*
72 * Prototypes for local procedures defined in this file:
73 */
74
75 static int ReadPPMFileHeader _ANSI_ARGS_((Tcl_Channel chan,
76 int *widthPtr, int *heightPtr,
77 int *maxIntensityPtr));
78
79 /*
80 *----------------------------------------------------------------------
81 *
82 * FileMatchPPM --
83 *
84 * This procedure is invoked by the photo image type to see if
85 * a file contains image data in PPM format.
86 *
87 * Results:
88 * The return value is >0 if the first characters in file "f" look
89 * like PPM data, and 0 otherwise.
90 *
91 * Side effects:
92 * The access position in f may change.
93 *
94 *----------------------------------------------------------------------
95 */
96
97 static int
98 FileMatchPPM(chan, fileName, formatString, widthPtr, heightPtr, interp)
99 Tcl_Channel chan; /* The image file, open for reading. */
100 Tcl_Obj *fileName; /* The name of the image file. */
101 Tcl_Obj *formatString; /* User-specified format string, or NULL. */
102 int *widthPtr, *heightPtr; /* The dimensions of the image are
103 * returned here if the file is a valid
104 * raw PPM file. */
105 Tcl_Interp *interp;
106 {
107 int dummy;
108
109 return ReadPPMFileHeader(chan, widthPtr, heightPtr, &dummy);
110 }
111
112 /*
113 *----------------------------------------------------------------------
114 *
115 * FileReadPPM --
116 *
117 * This procedure is called by the photo image type to read
118 * PPM format data from a file and write it into a given
119 * photo image.
120 *
121 * Results:
122 * A standard TCL completion code. If TCL_ERROR is returned
123 * then an error message is left in the interp's result.
124 *
125 * Side effects:
126 * The access position in file f is changed, and new data is
127 * added to the image given by imageHandle.
128 *
129 *----------------------------------------------------------------------
130 */
131
132 static int
133 FileReadPPM(interp, chan, fileNameObj, formatString, imageHandle, destX, destY,
134 width, height, srcX, srcY)
135 Tcl_Interp *interp; /* Interpreter to use for reporting errors. */
136 Tcl_Channel chan; /* The image file, open for reading. */
137 Tcl_Obj *fileNameObj; /* The name of the image file. */
138 Tcl_Obj *formatString; /* User-specified format string, or NULL. */
139 Tk_PhotoHandle imageHandle; /* The photo image to write into. */
140 int destX, destY; /* Coordinates of top-left pixel in
141 * photo image to be written to. */
142 int width, height; /* Dimensions of block of photo image to
143 * be written to. */
144 int srcX, srcY; /* Coordinates of top-left pixel to be used
145 * in image being read. */
146 {
147 int fileWidth, fileHeight, maxIntensity;
148 int nLines, nBytes, h, type, count;
149 unsigned char *pixelPtr;
150 Tk_PhotoImageBlock block;
151 char *fileName =Tcl_GetString(fileNameObj);
152
153 type = ReadPPMFileHeader(chan, &fileWidth, &fileHeight, &maxIntensity);
154 if (type == 0) {
155 Tcl_AppendResult(interp, "couldn't read raw PPM header from file \"",
156 fileName, "\"", NULL);
157 return TCL_ERROR;
158 }
159 if ((fileWidth <= 0) || (fileHeight <= 0)) {
160 Tcl_AppendResult(interp, "PPM image file \"", fileName,
161 "\" has dimension(s) <= 0", (char *) NULL);
162 return TCL_ERROR;
163 }
164 if ((maxIntensity <= 0) || (maxIntensity >= 256)) {
165 char buffer[TCL_INTEGER_SPACE];
166
167 sprintf(buffer, "%d", maxIntensity);
168 Tcl_AppendResult(interp, "PPM image file \"", fileName,
169 "\" has bad maximum intensity value ", buffer,
170 (char *) NULL);
171 return TCL_ERROR;
172 }
173
174 if ((srcX + width) > fileWidth) {
175 width = fileWidth - srcX;
176 }
177 if ((srcY + height) > fileHeight) {
178 height = fileHeight - srcY;
179 }
180 if ((width <= 0) || (height <= 0)
181 || (srcX >= fileWidth) || (srcY >= fileHeight)) {
182 return TCL_OK;
183 }
184
185 if (type == PGM) {
186 block.pixelSize = 1;
187 block.offset[0] = 0;
188 block.offset[1] = 0;
189 block.offset[2] = 0;
190 }
191 else {
192 block.pixelSize = 3;
193 block.offset[0] = 0;
194 block.offset[1] = 1;
195 block.offset[2] = 2;
196 }
197 block.offset[3] = 0;
198 block.width = width;
199 block.pitch = block.pixelSize * fileWidth;
200
201 Tk_PhotoExpand(imageHandle, destX + width, destY + height);
202
203 if (srcY > 0) {
204 Tcl_Seek(chan, (Tcl_WideInt)(srcY * block.pitch), SEEK_CUR);
205 }
206
207 nLines = (MAX_MEMORY + block.pitch - 1) / block.pitch;
208 if (nLines > height) {
209 nLines = height;
210 }
211 if (nLines <= 0) {
212 nLines = 1;
213 }
214 nBytes = nLines * block.pitch;
215 pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes);
216 block.pixelPtr = pixelPtr + srcX * block.pixelSize;
217
218 for (h = height; h > 0; h -= nLines) {
219 if (nLines > h) {
220 nLines = h;
221 nBytes = nLines * block.pitch;
222 }
223 count = Tcl_Read(chan, (char *) pixelPtr, nBytes);
224 if (count != nBytes) {
225 Tcl_AppendResult(interp, "error reading PPM image file \"",
226 fileName, "\": ",
227 Tcl_Eof(chan) ? "not enough data" : Tcl_PosixError(interp),
228 (char *) NULL);
229 ckfree((char *) pixelPtr);
230 return TCL_ERROR;
231 }
232 if (maxIntensity != 255) {
233 unsigned char *p;
234
235 for (p = pixelPtr; count > 0; count--, p++) {
236 *p = (((int) *p) * 255)/maxIntensity;
237 }
238 }
239 block.height = nLines;
240 Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, nLines,
241 TK_PHOTO_COMPOSITE_SET);
242 destY += nLines;
243 }
244
245 ckfree((char *) pixelPtr);
246 return TCL_OK;
247 }
248
249 /*
250 *----------------------------------------------------------------------
251 *
252 * FileWritePPM --
253 *
254 * This procedure is invoked to write image data to a file in PPM
255 * format.
256 *
257 * Results:
258 * A standard TCL completion code. If TCL_ERROR is returned
259 * then an error message is left in the interp's result.
260 *
261 * Side effects:
262 * Data is written to the file given by "fileName".
263 *
264 *----------------------------------------------------------------------
265 */
266
267 static int
268 FileWritePPM(interp, fileName, formatString, blockPtr)
269 Tcl_Interp *interp;
270 char *fileName;
271 Tcl_Obj *formatString;
272 Tk_PhotoImageBlock *blockPtr;
273 {
274 Tcl_Channel chan;
275 int w, h;
276 int greenOffset, blueOffset, nBytes;
277 unsigned char *pixelPtr, *pixLinePtr;
278 char header[16 + TCL_INTEGER_SPACE * 2];
279
280 chan = Tcl_OpenFileChannel(interp, fileName, "w", 0666);
281 if (chan == NULL) {
282 return TCL_ERROR;
283 }
284
285 if (Tcl_SetChannelOption(interp, chan, "-translation", "binary")
286 != TCL_OK) {
287 Tcl_Close(NULL, chan);
288 return TCL_ERROR;
289 }
290 if (Tcl_SetChannelOption(interp, chan, "-encoding", "binary")
291 != TCL_OK) {
292 Tcl_Close(NULL, chan);
293 return TCL_ERROR;
294 }
295
296 sprintf(header, "P6\n%d %d\n255\n", blockPtr->width, blockPtr->height);
297 Tcl_Write(chan, header, -1);
298
299 pixLinePtr = blockPtr->pixelPtr + blockPtr->offset[0];
300 greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
301 blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
302
303 if ((greenOffset == 1) && (blueOffset == 2) && (blockPtr->pixelSize == 3)
304 && (blockPtr->pitch == (blockPtr->width * 3))) {
305 nBytes = blockPtr->height * blockPtr->pitch;
306 if (Tcl_Write(chan, (char *) pixLinePtr, nBytes) != nBytes) {
307 goto writeerror;
308 }
309 } else {
310 for (h = blockPtr->height; h > 0; h--) {
311 pixelPtr = pixLinePtr;
312 for (w = blockPtr->width; w > 0; w--) {
313 if ((Tcl_Write(chan, (char *) &pixelPtr[0], 1) == -1)
314 || (Tcl_Write(chan, (char *) &pixelPtr[greenOffset], 1) == -1)
315 || (Tcl_Write(chan, (char *) &pixelPtr[blueOffset], 1) == -1)) {
316 goto writeerror;
317 }
318 pixelPtr += blockPtr->pixelSize;
319 }
320 pixLinePtr += blockPtr->pitch;
321 }
322 }
323
324 if (Tcl_Close(NULL, chan) == 0) {
325 return TCL_OK;
326 }
327 chan = NULL;
328
329 writeerror:
330 Tcl_AppendResult(interp, "error writing \"", fileName, "\": ",
331 Tcl_PosixError(interp), (char *) NULL);
332 if (chan != NULL) {
333 Tcl_Close(NULL, chan);
334 }
335 return TCL_ERROR;
336 }
337
338 /*
339 *----------------------------------------------------------------------
340 *
341 * ReadPPMFileHeader --
342 *
343 * This procedure reads the PPM header from the beginning of a
344 * PPM file and returns information from the header.
345 *
346 * Results:
347 * The return value is PGM if file "f" appears to start with
348 * a valid PGM header, PPM if "f" appears to start with a valid
349 * PPM header, and 0 otherwise. If the header is valid,
350 * then *widthPtr and *heightPtr are modified to hold the
351 * dimensions of the image and *maxIntensityPtr is modified to
352 * hold the value of a "fully on" intensity value.
353 *
354 * Side effects:
355 * The access position in f advances.
356 *
357 *----------------------------------------------------------------------
358 */
359
360 static int
361 ReadPPMFileHeader(chan, widthPtr, heightPtr, maxIntensityPtr)
362 Tcl_Channel chan; /* Image file to read the header from */
363 int *widthPtr, *heightPtr; /* The dimensions of the image are
364 * returned here. */
365 int *maxIntensityPtr; /* The maximum intensity value for
366 * the image is stored here. */
367 {
368 #define BUFFER_SIZE 1000
369 char buffer[BUFFER_SIZE];
370 int i, numFields;
371 int type = 0;
372 char c;
373
374 /*
375 * Read 4 space-separated fields from the file, ignoring
376 * comments (any line that starts with "#").
377 */
378
379 if (Tcl_Read(chan, &c, 1) != 1) {
380 return 0;
381 }
382 i = 0;
383 for (numFields = 0; numFields < 4; numFields++) {
384 /*
385 * Skip comments and white space.
386 */
387
388 while (1) {
389 while (isspace(UCHAR(c))) {
390 if (Tcl_Read(chan, &c, 1) != 1) {
391 return 0;
392 }
393 }
394 if (c != '#') {
395 break;
396 }
397 do {
398 if (Tcl_Read(chan, &c, 1) != 1) {
399 return 0;
400 }
401 } while (c != '\n');
402 }
403
404 /*
405 * Read a field (everything up to the next white space).
406 */
407
408 while (!isspace(UCHAR(c))) {
409 if (i < (BUFFER_SIZE-2)) {
410 buffer[i] = c;
411 i++;
412 }
413 if (Tcl_Read(chan, &c, 1) != 1) {
414 goto done;
415 }
416 }
417 if (i < (BUFFER_SIZE-1)) {
418 buffer[i] = ' ';
419 i++;
420 }
421 }
422 done:
423 buffer[i] = 0;
424
425 /*
426 * Parse the fields, which are: id, width, height, maxIntensity.
427 */
428
429 if (strncmp(buffer, "P6 ", 3) == 0) {
430 type = PPM;
431 } else if (strncmp(buffer, "P5 ", 3) == 0) {
432 type = PGM;
433 } else {
434 return 0;
435 }
436 if (sscanf(buffer+3, "%d %d %d", widthPtr, heightPtr, maxIntensityPtr)
437 != 3) {
438 return 0;
439 }
440 return type;
441 }