Debian Codegraph @

pTk/mTk/additions/imgBMP.c

raw
1 /*
2 * tkImgBMP.c --
3 *
4 * A photo image file handler for BMP files.
5 *
6 */
7
8 /* Author : Jan Nijtmans */
9 /* Date : 7/22/97 */
10
11 #include "tk.h"
12 #include "tkVMacro.h"
13 #include "imgInt.h"
14 #include <string.h>
15 #include <stdlib.h>
16
17 /*
18 * The format record for the BMP file format:
19 */
20
21 static int ChnMatchBMP _ANSI_ARGS_((Tcl_Channel chan, Tcl_Obj *fileName,
22 Tcl_Obj *format, int *widthPtr, int *heightPtr,Tcl_Interp *interp));
23 static int ObjMatchBMP _ANSI_ARGS_((Tcl_Obj *dataObj,
24 Tcl_Obj *format, int *widthPtr, int *heightPtr, Tcl_Interp *interp));
25 static int ChnReadBMP _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Channel chan,
26 Tcl_Obj *fileName, Tcl_Obj *format, Tk_PhotoHandle imageHandle,
27 int destX, int destY, int width, int height, int srcX, int srcY));
28 static int ObjReadBMP _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *dataObj,
29 Tcl_Obj *format, Tk_PhotoHandle imageHandle,
30 int destX, int destY, int width, int height, int srcX, int srcY));
31 static int ChnWriteBMP _ANSI_ARGS_((Tcl_Interp *interp, char *filename,
32 Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr));
33 static int StringWriteBMP _ANSI_ARGS_((Tcl_Interp *interp,
34 Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr));
35
36 Tk_PhotoImageFormat imgFmtBMP = {
37 "bmp", /* name */
38 ChnMatchBMP, /* fileMatchProc */
39 ObjMatchBMP, /* stringMatchProc */
40 ChnReadBMP, /* fileReadProc */
41 ObjReadBMP, /* stringReadProc */
42 ChnWriteBMP, /* fileWriteProc */
43 StringWriteBMP, /* stringWriteProc */
44 };
45
46 /*
47 * Prototypes for local procedures defined in this file:
48 */
49
50 static int CommonMatchBMP _ANSI_ARGS_((MFile *handle, int *widthPtr,
51 int *heightPtr, unsigned char **colorMap, int *numBits,
52 int *numCols, int *comp));
53 static int CommonReadBMP _ANSI_ARGS_((Tcl_Interp *interp, MFile *handle,
54 Tk_PhotoHandle imageHandle, int destX, int destY, int width,
55 int height, int srcX, int srcY));
56 static int CommonWriteBMP _ANSI_ARGS_((Tcl_Interp *interp, MFile *handle,
57 Tk_PhotoImageBlock *blockPtr));
58 static void putint _ANSI_ARGS_((MFile *handle, int i));
59
60 static int ChnMatchBMP(chan, fileName, format, widthPtr, heightPtr, interp)
61 Tcl_Channel chan;
62 Tcl_Obj *fileName;
63 Tcl_Obj *format;
64 int *widthPtr, *heightPtr;
65 Tcl_Interp *interp;
66 {
67 MFile handle;
68
69 ImgFixChanMatchProc(&interp, &chan, &fileName, &format, &widthPtr, &heightPtr);
70
71 handle.data = (char *) chan;
72 handle.state = IMG_CHAN;
73
74 return CommonMatchBMP(&handle, widthPtr, heightPtr, NULL, NULL, NULL, NULL);
75 }
76
77 static int ObjMatchBMP(data, format, widthPtr, heightPtr, interp)
78 Tcl_Obj *data;
79 Tcl_Obj *format;
80 int *widthPtr, *heightPtr;
81 Tcl_Interp *interp;
82 {
83 MFile handle;
84
85 ImgFixObjMatchProc(&interp, &data, &format, &widthPtr, &heightPtr);
86
87 if (!ImgReadInit(data,'B',&handle)) {
88 return 0;
89 }
90 return CommonMatchBMP(&handle, widthPtr, heightPtr, NULL, NULL, NULL, NULL);
91 }
92
93 static int CommonMatchBMP(handle, widthPtr, heightPtr, colorMap, numBits, numCols, comp)
94 MFile *handle;
95 int *widthPtr, *heightPtr;
96 unsigned char **colorMap;
97 int *numBits, *numCols, *comp;
98 {
99 unsigned char buf[28];
100 int c,i, compression, nBits, clrUsed, offBits;
101
102 if ((ImgRead(handle, (char *) buf, 2) != 2)
103 || (strncmp("BM", (char *) buf, 2) != 0)
104 || (ImgRead(handle, (char *) buf, 24) != 24)
105 || buf[13] || buf[14] || buf[15]) {
106 return 0;
107 }
108
109 offBits = (buf[11]<<24) + (buf[10]<<16) + (buf[9]<<8) + buf[8];
110 c = buf[12];
111 if ((c == 40) || (c == 64)) {
112 *widthPtr = (buf[19]<<24) + (buf[18]<<16) + (buf[17]<<8) + buf[16];
113 *heightPtr = (buf[23]<<24) + (buf[22]<<16) + (buf[21]<<8) + buf[20];
114 if (ImgRead(handle, (char *) buf, 24) != 24) {
115 return 0;
116 }
117 nBits = buf[2];
118 compression = buf[4];
119 clrUsed = (buf[21]<<8) + buf[20];
120 offBits -= c+14;
121 } else if (c == 12) {
122 *widthPtr = (buf[17]<<8) + buf[16];
123 *heightPtr = (buf[19]<<8) + buf[18];
124 nBits = buf[22];
125 compression = 0;
126 clrUsed = 0;
127 } else {
128 return 0;
129 }
130 if (colorMap) {
131 if (c > 36) ImgRead(handle, (char *) buf, c - 36);
132 if (!clrUsed && nBits != 24) {
133 clrUsed = 1 << nBits;
134 }
135 if (nBits<24) {
136 unsigned char colbuf[4], *ptr;
137 offBits -= (3+(c!=12)) * clrUsed;
138 *colorMap = ptr = (unsigned char *) ckalloc(3*clrUsed);
139 for (i = 0; i < clrUsed; i++) {
140 ImgRead(handle, (char *) colbuf, 3+(c!=12));
141 *ptr++ = colbuf[0]; *ptr++ = colbuf[1]; *ptr++ = colbuf[2];
142 /*printf("color %d: %d %d %d\n", i, colbuf[2], colbuf[1], colbuf[0]);*/
143 }
144 }
145 while (offBits>28) {
146 offBits -= 28;
147 ImgRead(handle, (char *) buf, 28);
148 }
149 if (offBits) ImgRead(handle, (char *) buf, offBits);
150 if (numCols) {
151 *numCols = clrUsed;
152 }
153 }
154 if (numBits) {
155 *numBits = nBits;
156 }
157 if (comp) {
158 *comp = compression;
159 }
160 return 1;
161 }
162
163 static int ChnReadBMP(interp, chan, fileName, format, imageHandle,
164 destX, destY, width, height, srcX, srcY)
165 Tcl_Interp *interp;
166 Tcl_Channel chan;
167 Tcl_Obj *fileName;
168 Tcl_Obj *format;
169 Tk_PhotoHandle imageHandle;
170 int destX, destY;
171 int width, height;
172 int srcX, srcY;
173 {
174 MFile handle;
175
176 handle.data = (char *) chan;
177 handle.state = IMG_CHAN;
178
179 return CommonReadBMP(interp, &handle, imageHandle, destX, destY,
180 width, height, srcX, srcY);
181 }
182
183 static int ObjReadBMP(interp, data, format, imageHandle,
184 destX, destY, width, height, srcX, srcY)
185 Tcl_Interp *interp;
186 Tcl_Obj *data;
187 Tcl_Obj *format;
188 Tk_PhotoHandle imageHandle;
189 int destX, destY;
190 int width, height;
191 int srcX, srcY;
192 {
193 MFile handle;
194
195 ImgReadInit(data,'B',&handle);
196
197 return CommonReadBMP(interp, &handle, imageHandle, destX, destY,
198 width, height, srcX, srcY);
199 }
200
201 typedef struct myblock {
202 Tk_PhotoImageBlock ck;
203 int dummy; /* extra space for offset[3], in case it is not
204 included already in Tk_PhotoImageBlock */
205 } myblock;
206
207 #define block bl.ck
208
209 static int CommonReadBMP(interp, handle, imageHandle, destX, destY,
210 width, height, srcX, srcY)
211 Tcl_Interp *interp;
212 MFile *handle;
213 Tk_PhotoHandle imageHandle;
214 int destX, destY;
215 int width, height;
216 int srcX, srcY;
217 {
218 myblock bl;
219 int numBits, bytesPerLine, numCols, comp, x, y;
220 int fileWidth, fileHeight;
221 unsigned char *colorMap = NULL;
222 char buf[10];
223 unsigned char *line = NULL, *expline = NULL;
224
225 CommonMatchBMP(handle, &fileWidth, &fileHeight, &colorMap, &numBits,
226 &numCols, &comp);
227
228 /* printf("reading %d-bit BMP %dx%d\n", numBits, width, height); */
229 if (comp != 0) {
230 Tcl_AppendResult(interp,
231 "Compressed BMP files not (yet) supported", (char *) NULL);
232 goto error;
233 return TCL_ERROR;
234 }
235
236 Tk_PhotoExpand(imageHandle, destX + width, destY + height);
237
238 bytesPerLine = ((numBits * fileWidth + 31)/32)*4;
239 line = (unsigned char *) ckalloc(bytesPerLine);
240
241 for(y=srcY+height; y<fileHeight; y++) {
242 ImgRead(handle, line, bytesPerLine);
243 }
244 block.pixelSize = 3;
245 block.pitch = bytesPerLine;
246 block.width = width;
247 block.height = 1;
248 block.offset[0] = 2;
249 block.offset[1] = 1;
250 block.offset[2] = 0;
251 block.offset[3] = block.offset[0];
252 switch (numBits) {
253 case 24:
254 block.pixelPtr = line + srcX*3;
255 for( y = height-1; y>=0; y--) {
256 ImgRead(handle, line, bytesPerLine);
257 Tk_PhotoPutBlock(imageHandle, &block, destX, destY+y,
258 width,1, TK_PHOTO_COMPOSITE_SET);
259 }
260 break;
261 case 8:
262 block.pixelPtr = expline = (unsigned char *) ckalloc(3*width);
263 for( y = height-1; y>=0; y--) {
264 ImgRead(handle, line, bytesPerLine);
265 for (x = srcX; x < (srcX+width); x++) {
266 memcpy(expline, colorMap+(3*line[x]),3);
267 expline += 3;
268 }
269 Tk_PhotoPutBlock(imageHandle, &block, destX, destY+y,
270 width,1, TK_PHOTO_COMPOSITE_SET);
271 expline = block.pixelPtr;
272 }
273 break;
274 case 4:
275 block.pixelPtr = expline = (unsigned char *) ckalloc(3*width);
276 for( y = height-1; y>=0; y--) {
277 int c;
278 ImgRead(handle, line, bytesPerLine);
279 for (x = srcX; x < (srcX+width); x++) {
280 if (x&1) {
281 c = line[x/2] & 0x0f;
282 } else {
283 c = line[x/2] >> 4;
284 }
285 memcpy(expline, colorMap+(3*c),3);
286 expline += 3;
287 }
288 Tk_PhotoPutBlock(imageHandle, &block, destX, destY+y,
289 width,1, TK_PHOTO_COMPOSITE_SET);
290 expline = block.pixelPtr;
291 }
292 break;
293 case 1:
294 block.pixelPtr = expline = (unsigned char *) ckalloc(3*width);
295 for( y = height-1; y>=0; y--) {
296 int c;
297 ImgRead(handle, line, bytesPerLine);
298 for (x = srcX; x < (srcX+width); x++) {
299 c = (line[x/8] >> (7-(x%8))) & 1;
300 memcpy(expline, colorMap+(3*c),3);
301 expline += 3;
302 }
303 Tk_PhotoPutBlock(imageHandle, &block, destX, destY+y,
304 width,1, TK_PHOTO_COMPOSITE_SET);
305 expline = block.pixelPtr;
306 }
307 break;
308 default:
309 sprintf(buf,"%d", numBits);
310 Tcl_AppendResult(interp, buf,
311 "-bits BMP file not (yet) supported", (char *) NULL);
312 goto error;
313 }
314
315 if (colorMap) {
316 ckfree((char *) colorMap);
317 }
318 if (line) {
319 ckfree((char *) line);
320 }
321 if (expline) {
322 ckfree((char *) expline);
323 }
324 return TCL_OK ;
325
326 error:
327 if (colorMap) {
328 ckfree((char *) colorMap);
329 }
330 if (line) {
331 ckfree((char *) line);
332 }
333 if (expline) {
334 ckfree((char *) expline);
335 }
336 return TCL_ERROR;
337 }
338
339 static int ChnWriteBMP(interp, filename, format, blockPtr)
340 Tcl_Interp *interp;
341 char *filename;
342 Tcl_Obj *format;
343 Tk_PhotoImageBlock *blockPtr;
344 {
345 Tcl_Channel chan;
346 MFile handle;
347 int result;
348
349 chan = ImgOpenFileChannel(interp, filename, 0644);
350 if (!chan) {
351 return TCL_ERROR;
352 }
353
354 handle.data = (char *) chan;
355 handle.state = IMG_CHAN;
356
357 result = CommonWriteBMP(interp, &handle, blockPtr);
358 if (Tcl_Close(interp, chan) == TCL_ERROR) {
359 return TCL_ERROR;
360 }
361 return result;
362 }
363
364 static int StringWriteBMP(interp, format, blockPtr)
365 Tcl_Interp *interp;
366 Tcl_Obj *format;
367 Tk_PhotoImageBlock *blockPtr;
368 {
369 MFile handle;
370 int result;
371 Tcl_DString data;
372 Tcl_DString *dataPtr;
373
374 ImgFixStringWriteProc(&data, &interp, &dataPtr, &format, &blockPtr);
375
376 ImgWriteInit(dataPtr, &handle);
377 result = CommonWriteBMP(interp, &handle, blockPtr);
378 ImgPutc(IMG_DONE, &handle);
379
380 if ((result == TCL_OK) && (dataPtr == &data)) {
381 Tcl_DStringResult(interp, dataPtr);
382 }
383 return result;
384 }
385
386 static void putint(handle, i)
387 MFile *handle;
388 int i;
389 {
390 unsigned char buf[4];
391 buf[0] = i;
392 buf[1] = i>>8;
393 buf[2] = i>>16;
394 buf[3] = i>>24;
395 ImgWrite(handle, (char *) buf, 4);
396 }
397
398 static int CommonWriteBMP(interp, handle, blockPtr)
399 Tcl_Interp *interp;
400 MFile *handle;
401 Tk_PhotoImageBlock *blockPtr;
402 {
403 int bperline, nbytes, ncolors, i, x, y, greenOffset, blueOffset, alphaOffset;
404 unsigned char *imagePtr, *pixelPtr;
405 unsigned char buf[4];
406 int colors[256];
407
408 greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
409 blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
410 alphaOffset = blockPtr->offset[0];
411 if (alphaOffset < blockPtr->offset[2]) {
412 alphaOffset = blockPtr->offset[2];
413 }
414 if (++alphaOffset < blockPtr->pixelSize) {
415 alphaOffset -= blockPtr->offset[0];
416 } else {
417 alphaOffset = 0;
418 }
419 ncolors = 0;
420 if (greenOffset || blueOffset) {
421 for (y = 0; ncolors <= 256 && y < blockPtr->height; y++) {
422 pixelPtr = blockPtr->pixelPtr + y*blockPtr->pitch + blockPtr->offset[0];
423 for (x=0; ncolors <= 256 && x<blockPtr->width; x++) {
424 int pixel;
425 if (alphaOffset && (pixelPtr[alphaOffset] == 0))
426 pixel = 0xd9d9d9;
427 else
428 pixel = (pixelPtr[0]<<16) | (pixelPtr[greenOffset]<<8) | pixelPtr[blueOffset];
429 for (i = 0; i < ncolors && pixel != colors[i]; i++);
430 if (i == ncolors) {
431 if (ncolors < 256) {
432 colors[ncolors] = pixel;
433 }
434 ncolors++;
435 }
436 pixelPtr += blockPtr->pixelSize;
437 }
438 }
439 if (ncolors <= 256 && (blockPtr->width * blockPtr->height >= 512)) {
440 while (ncolors < 256) {
441 colors[ncolors++] = 0;
442 }
443 nbytes = 1;
444 } else {
445 nbytes = 3;
446 ncolors = 0;
447 }
448 } else {
449 nbytes = 1;
450 }
451
452 bperline = ((blockPtr->width * nbytes + 3) / 4) * 4;
453
454 ImgWrite(handle,"BM", 2);
455 putint(handle, 54 + (ncolors*4) + bperline * blockPtr->height);
456 putint(handle, 0);
457 putint(handle, 54 + (ncolors*4));
458 putint(handle, 40);
459 putint(handle, blockPtr->width);
460 putint(handle, blockPtr->height);
461 putint(handle, 1 + (nbytes<<19));
462 putint(handle, 0);
463 putint(handle, bperline * blockPtr->height);
464 putint(handle, 75*39);
465 putint(handle, 75*39);
466 putint(handle, ncolors);
467 putint(handle, ncolors);
468
469 for (i = 0; i < ncolors ; i++) {
470 putint(handle, colors[i]);
471 }
472
473 bperline -= blockPtr->width * nbytes;
474
475 imagePtr = blockPtr->pixelPtr + blockPtr->offset[0]
476 + blockPtr->height * blockPtr->pitch;
477 for (y = 0; y < blockPtr->height; y++) {
478 pixelPtr = imagePtr -= blockPtr->pitch;
479 for (x=0; x<blockPtr->width; x++) {
480 if (ncolors) {
481 int pixel;
482 if (alphaOffset && (pixelPtr[alphaOffset] == 0))
483 pixel = 0xd9d9d9;
484 else
485 pixel = (pixelPtr[0]<<16)|(pixelPtr[greenOffset]<<8)|pixelPtr[blueOffset];
486 for (i = 0; i < ncolors && pixel != colors[i]; i += 1);
487 buf[0] = i;
488 } else if (alphaOffset && (pixelPtr[alphaOffset] == 0)) {
489 buf[0] = buf[1] = buf[2] = 0xd9;
490 } else {
491 buf[0] = pixelPtr[blueOffset];
492 buf[1] = pixelPtr[greenOffset];
493 buf[2] = pixelPtr[0];
494 }
495 ImgWrite(handle, (char *) buf, nbytes);
496 pixelPtr += blockPtr->pixelSize;
497 }
498 if (bperline) {
499 ImgWrite(handle, "\0\0\0", bperline);
500 }
501 }
502 return(TCL_OK);
503 }
504