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