~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/X11/tiff_dump.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*    Baseline TIFF 6.0 writer for screen dump  */
  2 /*    writes simple header for 8 bit data       */
  3 /*    with fixed endian definition and no       */
  4 /*    compression                               */
  5 /*    NAT 28/3/2000                             */
  6 #include <tina/sys.h>
  7 #include <tina/math.h>
  8 #include <tina/vision.h>
  9 #include <tina/tv.h>
 10 #include <tina/tvfuncs.h>
 11 #include <tina/file.h>
 12 #include <tina/draw.h>
 13 #include <tina/tv_screen.h>
 14 
 15 void tv_screen_tiff_dump_bw(Tv_screen *, char *);
 16 static void tv_screen_tiff_dump_1bpp(Tv_screen *, char *);
 17 static void tv_screen_tiff_dump_color(Tv_screen *, char *);
 18 static void (*tiff_dump) (Tv_screen *, char *) = tv_screen_tiff_dump_color;
 19 
 20 static unsigned char hdr[8] = { 0x4D, 0x4D, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x08};
 21 /* endian
 22     hdr[0] = 0x49;
 23     hdr[1] = 0x49;
 24 */
 25 static unsigned char ifd[2] = {0x00,0x0C};
 26 static unsigned char ifdcol[2] = {0x00,0x0D};
 27 /* data fields defined tags(4), type(4) , and value/offset(4) */
 28 static unsigned char newsubfile[12] = {0x00,0xFE,0x00,0x04,0x00,0x00,0x00,0x01,
 29                                                    0x00,0x00,0x00,0x00};
 30 static unsigned char image_width[12] = {0x01,0x00,0x00,0x04,0x00,0x00,0x00,0x01,
 31                                                    0x00,0x00,0x00,0x00};
 32 static unsigned char image_height[12] = {0x01,0x01,0x00,0x04,0x00,0x00,0x00,0x01,
 33                                                    0x00,0x00,0x00,0x00};
 34 static unsigned char bitspersample[12] = {0x01,0x02,0x00,0x03,0x00,0x00,0x00,0x01,
 35                                                    0x00,0x08,0x00,0x00};
 36 static unsigned char bitspersamcol[12] = {0x01,0x02,0x00,0x03,0x00,0x00,0x00,0x03,
 37                                                    0x00,0x00,0x00,0x00};
 38 static unsigned char coldata[6] = {0x00,0x08,0x00,0x08,0x00,0x08};
 39 static unsigned char compression[12] = {0x01,0x03,0x00,0x03,0x00,0x00,0x00,0x01,
 40                                                    0x00,0x01,0x00,0x00};
 41 static unsigned char photonumeric[12] = {0x01,0x06,0x00,0x03,0x00,0x00,0x00,0x01,
 42                                                    0x00,0x01,0x00,0x00};
 43 static unsigned char photonumecol[12] = {0x01,0x06,0x00,0x03,0x00,0x00,0x00,0x01,
 44                                                    0x00,0x02,0x00,0x00};
 45 static unsigned char stripoffsets[12] = {0x01,0x11,0x00,0x04,0x00,0x00,0x00,0x01,
 46                                                    0x00,0x00,0x00,0x00};
 47 static unsigned char rowsperstrip[12] = {0x01,0x16,0x00,0x04,0x00,0x00,0x00,0x01,
 48                                                    0x00,0x00,0x00,0x00};
 49 static unsigned char stripbytes[12] = {0x01,0x17,0x00,0x04,0x00,0x00,0x00,0x01,
 50                                                    0x00,0x00,0x00,0x00};
 51 static unsigned char xresolution[12] = {0x01,0x1A,0x00,0x05,0x00,0x00,0x00,0x01,
 52                                                    0x00,0x00,0x00,0x00};
 53 static unsigned char xdata[8] = {0x00,0x00,0x01,0x2C,0x00,0x00,0x00,0x01};
 54 static unsigned char yresolution[12] = {0x01,0x1B,0x00,0x05,0x00,0x00,0x00,0x01,
 55                                                    0x00,0x00,0x00,0x00};
 56 static unsigned char ydata[8] = {0x00,0x00,0x01,0x2C,0x00,0x00,0x00,0x01};
 57 static unsigned char resolutionunit[12] = {0x01,0x28,0x00,0x03,0x00,0x00,0x00,0x01,
 58                                                    0x00,0x01,0x00,0x00};
 59 static unsigned char nextifd[8] = {0x00,0x00,0x00,0x00};
 60 static unsigned char samplesperpix[12] = {0x01,0x15,0x00,0x03,0x00,0x00,0x00,0x01,
 61                                                    0x00,0x03,0x00,0x00};
 62 
 63 static void set_tiff_dump_type(Dump_type type)
 64 {
 65     switch (type)
 66     {
 67         case DT_BW:
 68         tiff_dump = tv_screen_tiff_dump_bw;
 69         break;
 70     case DT_COLOR:
 71         tiff_dump = tv_screen_tiff_dump_color;
 72         break;
 73     case DT_ONE_BIT_PER_PIXEL:
 74         tiff_dump = tv_screen_tiff_dump_1bpp;
 75         break;
 76     }
 77 }
 78 
 79 static void tv_screen_write_data_1bpp(FILE * fp, Tv_screen * tv_screen)
 80 {
 81     int     width, height;
 82     XImage *ximage;
 83     char   *imagepix;
 84     int     bkgrndpix;
 85     Tina_pixel *lut;
 86     int     x, y;
 87     int     bit_count, count, byte, pixel;
 88 
 89     width = tv_screen_check_width(tv_screen);
 90     height = tv_screen_check_height(tv_screen);
 91 
 92     imagepix = (char *)nvector_alloc(0,height*(width+7)/8,sizeof(char)); 
 93     ximage = XGetImage(tv_screen->display, tv_screen->window,
 94                        0, 0, width, height, AllPlanes, ZPixmap);
 95     lut = tv_screen->tv->cmap_data_visible->std_lut;
 96 
 97     bkgrndpix = (int) lut[bkgrnd];
 98 
 99     count = 0;
100     for (y = 0; y < height; y++)
101     {
102         bit_count = 0;
103         byte = 0xff;
104         for (x = 0; x < width; x++)
105         {
106             pixel = XGetPixel(ximage, x, y);
107             if (pixel != bkgrndpix)
108                 byte ^= 0x80 >> bit_count;
109             bit_count = (bit_count + 1) % 8;
110             if (bit_count == 0)
111             {
112                 imagepix[count++] = byte;
113                 byte = 0xff;
114             }
115         }
116         if (bit_count > 0) imagepix[count++] = byte;
117         byte = 0xff;
118     }
119     fwrite(imagepix,sizeof(char),height*(width+7)/8,fp);
120     nvector_free(imagepix,0,sizeof(char));
121 
122     XDestroyImage(ximage);
123 }
124 
125 static void tv_screen_write_data_bw(FILE * fp, Tv_screen * tv_screen)
126 {
127     int     i, j, width, height;
128     char   *imagepix;
129     int     count = 0;
130     XImage *ximage;
131     int     bkgrndpix;
132     Tina_pixel *lut;
133 
134     width = tv_screen_check_width(tv_screen);
135     height = tv_screen_check_height(tv_screen);
136 
137     ximage = XGetImage(tv_screen->display, tv_screen->window,
138                        0, 0, width, height, AllPlanes, ZPixmap);
139 
140     imagepix = (char *)nvector_alloc(0,height*width,sizeof(char)); 
141     lut = tv_screen->tv->cmap_data_visible->std_lut;
142 
143     bkgrndpix = (int) lut[bkgrnd];
144 
145     count = 0;
146     for (i = 0; i < height; i++)
147     {
148         for (j = 0; j < width; j++)
149         {
150             unsigned long c, grey;
151             XColor  color;
152             int     r, g, b;
153 
154 
155             c = XGetPixel(ximage, j, i);        /* X pixel value */
156             color.pixel = c;
157             XQueryColor(tv_screen->display, tv_screen->colormap, &color);
158             r = color.red >> 8;
159             g = color.green >> 8;
160             b = color.blue >> 8;
161             if (r == g && g == b)
162                grey = r;       /** grey **/
163             else
164                grey = MAX3(r, g, b);
165             imagepix[count++] = grey;
166         }
167     }
168 
169     fwrite(imagepix,sizeof(char),height*width,fp);
170     nvector_free(imagepix,0,sizeof(char)); 
171     XDestroyImage(ximage);
172 }
173 
174 static void tv_screen_write_data_col(FILE * fp, Tv_screen * tv_screen)
175 {
176     int     i, j, width, height;
177     char   *imagepix;
178     int     count = 0;
179     XImage *ximage;
180     int     bkgrndpix;
181     Tina_pixel *lut;
182 
183     width = tv_screen_check_width(tv_screen);
184     height = tv_screen_check_height(tv_screen);
185 
186     ximage = XGetImage(tv_screen->display, tv_screen->window,
187                        0, 0, width, height, AllPlanes, ZPixmap);
188 
189     imagepix = (char *)nvector_alloc(0,3*height*width,sizeof(char)); 
190     lut = tv_screen->tv->cmap_data_visible->std_lut;
191 
192     bkgrndpix = (int) lut[bkgrnd];
193 
194     count = 0;
195     for (i = 0; i < height; i++)
196     {
197         for (j = 0; j < width; j++)
198         {
199             unsigned long c, grey;
200             XColor  color;
201             int     r, g, b;
202 
203 
204             c = XGetPixel(ximage, j, i);        /* X pixel value */
205             color.pixel = c;
206             XQueryColor(tv_screen->display, tv_screen->colormap, &color);
207             r = color.red >> 8;
208             g = color.green >> 8;
209             b = color.blue >> 8;
210             imagepix[count++] = r;
211             imagepix[count++] = g;
212             imagepix[count++] = b;
213         }
214     }
215 
216     fwrite(imagepix,sizeof(char),3*height*width,fp);
217     nvector_free(imagepix,0,sizeof(char)); 
218     XDestroyImage(ximage);
219 }
220 
221 void tv_screen_tiff_dump_1bpp(Tv_screen * tv_screen, char *tiff_dump_file)
222 {
223     FILE   *fp = fopen(tiff_dump_file, "w");
224     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
225 
226     if (fp == NULL)
227     {
228       error("epsf_dump: couldn't open file\n", non_fatal);
229       return;
230     }
231     width = tv_screen_check_width(tv_screen);
232     height = tv_screen_check_height(tv_screen);
233     bytes = height*(width+7)/8;
234     
235     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
236     fwrite_2(ifd,sizeof(char),2,fp,tiff_dump_file);
237     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
238     image_width[11] = width%256;
239     image_width[10] = width/256;
240     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
241     image_height[11] = height%256;
242     image_height[10] = height/256;
243     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
244     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
245     fwrite_2(photonumeric,sizeof(char),12,fp,tiff_dump_file);
246     stripoffsets[11] = 8+2+132+8+8+8;
247     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
248     rowsperstrip[11] = image_height[11];
249     rowsperstrip[10] = image_height[10];
250     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
251     stripbytes[11] = bytes%256;
252     bytes /=256;
253     stripbytes[10] = bytes%256;
254     bytes /=256;
255     stripbytes[9] = bytes%256;
256     bytes /=256;
257     stripbytes[8] = bytes%256;
258     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
259     xresolution[11] = stripoffsets[11]-16;
260     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
261     yresolution[11] = stripoffsets[11]-8;
262     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
263     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
264     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
265     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
266     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
267 
268     tv_screen_write_data_1bpp(fp, tv_screen);
269     fclose(fp);
270 }
271 
272 void tv_screen_tiff_dump_bw(Tv_screen * tv_screen, char *tiff_dump_file)
273 {
274     FILE   *fp = fopen(tiff_dump_file, "w");
275     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
276 
277     if (fp == NULL)
278     {
279       error("epsf_dump: couldn't open file\n", non_fatal);
280       return;
281     }
282     width = tv_screen_check_width(tv_screen);
283     height = tv_screen_check_height(tv_screen);
284     bytes = height*width;
285     
286     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
287     fwrite_2(ifd,sizeof(char),2,fp,tiff_dump_file);
288     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
289     image_width[11] = width%256;
290     image_width[10] = width/256;
291     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
292     image_height[11] = height%256;
293     image_height[10] = height/256;
294     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
295     fwrite_2(bitspersample,sizeof(char),12,fp,tiff_dump_file);
296     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
297     fwrite_2(photonumeric,sizeof(char),12,fp,tiff_dump_file);
298     stripoffsets[11] = 8+2+144+8+8+8;
299     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
300     rowsperstrip[11] = image_height[11];
301     rowsperstrip[10] = image_height[10];
302     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
303     stripbytes[11] = bytes%256;
304     bytes /=256;
305     stripbytes[10] = bytes%256;
306     bytes /=256;
307     stripbytes[9] = bytes%256;
308     bytes /=256;
309     stripbytes[8] = bytes%256;
310     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
311     xresolution[11] = stripoffsets[11]-16;
312     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
313     yresolution[11] = stripoffsets[11]-8;
314     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
315     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
316     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
317     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
318     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
319 
320     tv_screen_write_data_bw(fp, tv_screen);
321     fclose(fp);
322 }
323 
324 void tv_screen_tiff_dump_color(Tv_screen * tv_screen, char *tiff_dump_file)
325 {
326     FILE   *fp = fopen(tiff_dump_file, "w");
327     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
328 
329     if (fp == NULL)
330     {
331       error("epsf_dump: couldn't open file\n", non_fatal);
332       return;
333     }
334     width = tv_screen_check_width(tv_screen);
335     height = tv_screen_check_height(tv_screen);
336     bytes = 3*height*width;
337     
338     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
339     fwrite_2(ifdcol,sizeof(char),2,fp,tiff_dump_file);
340     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
341     image_width[11] = width%256;
342     image_width[10] = width/256;
343     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
344     image_height[11] = height%256;
345     image_height[10] = height/256;
346     stripoffsets[11] = 8+2+156+8+6+8+8;
347     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
348     bitspersamcol[11] = stripoffsets[11]-22;
349     fwrite_2(bitspersamcol,sizeof(char),12,fp,tiff_dump_file);
350     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
351     fwrite_2(photonumecol,sizeof(char),12,fp,tiff_dump_file);
352     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
353     fwrite_2(samplesperpix,sizeof(char),12,fp,tiff_dump_file);
354     rowsperstrip[11] = image_height[11];
355     rowsperstrip[10] = image_height[10];
356     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
357     stripbytes[11] = bytes%256;
358     bytes /=256;
359     stripbytes[10] = bytes%256;
360     bytes /=256;
361     stripbytes[9] = bytes%256;
362     bytes /=256;
363     stripbytes[8] = bytes%256;
364     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
365     xresolution[11] = stripoffsets[11]-16;
366     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
367     yresolution[11] = stripoffsets[11]-8;
368     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
369     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
370     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
371     fwrite_2(coldata,sizeof(char),6,fp,tiff_dump_file);
372     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
373     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
374 
375     tv_screen_write_data_col(fp, tv_screen);
376     fclose(fp);
377 }
378 
379 void    tv_screen_tiff_dump(Dump_type type, Tv_screen * tv_screen, char *filename)
380 {
381     if (tv_screen == NULL || tv_screen->tv == NULL)
382         return;
383     set_tiff_dump_type(type);
384     (*tiff_dump) (tv_screen, filename);
385 }
386 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.