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

Linux Cross Reference
Tina5/tina-tools/tinatool/gphx/x11/gphxX11_tiff_dump.c

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

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/gphx/x11/gphxX11_tiff_dump.c,v $
 37  * Date    :  $Date: 2007/02/15 01:55:50 $
 38  * Version :  $Revision: 1.3 $
 39  * CVS Id  :  $Id: gphxX11_tiff_dump.c,v 1.3 2007/02/15 01:55:50 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  *
 45  *    Baseline TIFF 6.0 writer for screen dump
 46  *    writes simple header for 8 bit data    
 47  *    with fixed endian definition and no   
 48  *    compression                          
 49  *    
 50  *
 51  *********
 52 */
 53 
 54 #include <tina/file/fileUtil_io.h>
 55 #include "gphxX11_tiff_dump.h"
 56 
 57 #if HAVE_CONFIG_H
 58   #include <config.h>
 59 #endif
 60 
 61 #include <stdio.h>
 62 #include <tinatool/gphx/x11/gphx_X11Def.h>
 63 #include <tinatool/gphx/x11/gphxX11_size.h>
 64 
 65 static void (*tiff_dump) (Tv_screen *, char *) = NULL;
 66 
 67 static unsigned char hdr[8] = { 0x4D, 0x4D, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x08};
 68 /* endian
 69     hdr[0] = 0x49;
 70     hdr[1] = 0x49;
 71 */
 72 static unsigned char ifd[2] = {0x00,0x0C};
 73 static unsigned char ifdcol[2] = {0x00,0x0D};
 74 /* data fields defined tags(4), type(4) , and value/offset(4) */
 75 static unsigned char newsubfile[12] = {0x00,0xFE,0x00,0x04,0x00,0x00,0x00,0x01,
 76                                                    0x00,0x00,0x00,0x00};
 77 static unsigned char image_width[12] = {0x01,0x00,0x00,0x04,0x00,0x00,0x00,0x01,
 78                                                    0x00,0x00,0x00,0x00};
 79 static unsigned char image_height[12] = {0x01,0x01,0x00,0x04,0x00,0x00,0x00,0x01,
 80                                                    0x00,0x00,0x00,0x00};
 81 static unsigned char bitspersample[12] = {0x01,0x02,0x00,0x03,0x00,0x00,0x00,0x01,
 82                                                    0x00,0x08,0x00,0x00};
 83 static unsigned char bitspersamcol[12] = {0x01,0x02,0x00,0x03,0x00,0x00,0x00,0x03,
 84                                                    0x00,0x00,0x00,0x00};
 85 static unsigned char coldata[6] = {0x00,0x08,0x00,0x08,0x00,0x08};
 86 static unsigned char compression[12] = {0x01,0x03,0x00,0x03,0x00,0x00,0x00,0x01,
 87                                                    0x00,0x01,0x00,0x00};
 88 static unsigned char photonumeric[12] = {0x01,0x06,0x00,0x03,0x00,0x00,0x00,0x01,
 89                                                    0x00,0x01,0x00,0x00};
 90 static unsigned char photonumecol[12] = {0x01,0x06,0x00,0x03,0x00,0x00,0x00,0x01,
 91                                                    0x00,0x02,0x00,0x00};
 92 static unsigned char stripoffsets[12] = {0x01,0x11,0x00,0x04,0x00,0x00,0x00,0x01,
 93                                                    0x00,0x00,0x00,0x00};
 94 static unsigned char rowsperstrip[12] = {0x01,0x16,0x00,0x04,0x00,0x00,0x00,0x01,
 95                                                    0x00,0x00,0x00,0x00};
 96 static unsigned char stripbytes[12] = {0x01,0x17,0x00,0x04,0x00,0x00,0x00,0x01,
 97                                                    0x00,0x00,0x00,0x00};
 98 static unsigned char xresolution[12] = {0x01,0x1A,0x00,0x05,0x00,0x00,0x00,0x01,
 99                                                    0x00,0x00,0x00,0x00};
100 static unsigned char xdata[8] = {0x00,0x00,0x01,0x2C,0x00,0x00,0x00,0x01};
101 static unsigned char yresolution[12] = {0x01,0x1B,0x00,0x05,0x00,0x00,0x00,0x01,
102                                                    0x00,0x00,0x00,0x00};
103 static unsigned char ydata[8] = {0x00,0x00,0x01,0x2C,0x00,0x00,0x00,0x01};
104 static unsigned char resolutionunit[12] = {0x01,0x28,0x00,0x03,0x00,0x00,0x00,0x01,
105                                                    0x00,0x01,0x00,0x00};
106 static unsigned char nextifd[8] = {0x00,0x00,0x00,0x00};
107 static unsigned char samplesperpix[12] = {0x01,0x15,0x00,0x03,0x00,0x00,0x00,0x01,
108                                                    0x00,0x03,0x00,0x00};
109 static void tv_screen_write_data_1bpp(FILE * fp, Tv_screen * tv_screen)
110 {
111     int     width, height;
112     XImage *ximage;
113     char   *imagepix;
114     int     bkgrndpix;
115     Tina_pixel *lut;
116     int     x, y;
117     int     bit_count, count, byte, pixel;
118 
119     width = tv_screen_check_width(tv_screen);
120     height = tv_screen_check_height(tv_screen);
121 
122     imagepix = (char *)nvector_alloc(0,height*(width+7)/8,sizeof(char)); 
123     ximage = XGetImage(tv_screen->display, tv_screen->window,
124                        0, 0, width, height, AllPlanes, ZPixmap);
125     lut = tv_screen->tv->cmap_data_visible->std_lut;
126 
127     bkgrndpix = (int) lut[bkgrnd];
128 
129     count = 0;
130     for (y = 0; y < height; y++)
131     {
132         bit_count = 0;
133         byte = 0xff;
134         for (x = 0; x < width; x++)
135         {
136             pixel = XGetPixel(ximage, x, y);
137             if (pixel != bkgrndpix)
138                 byte ^= 0x80 >> bit_count;
139             bit_count = (bit_count + 1) % 8;
140             if (bit_count == 0)
141             {
142                 imagepix[count++] = byte;
143                 byte = 0xff;
144             }
145         }
146         if (bit_count > 0) imagepix[count++] = byte;
147         byte = 0xff;
148     }
149     fwrite(imagepix,sizeof(char),height*(width+7)/8,fp);
150     nvector_free(imagepix,0,sizeof(char));
151 
152     XDestroyImage(ximage);
153 }
154 
155 static void tv_screen_write_data_bw(FILE * fp, Tv_screen * tv_screen)
156 {
157     int     i, j, width, height;
158     char   *imagepix;
159     int     count = 0;
160     XImage *ximage;
161     int     bkgrndpix;
162     Tina_pixel *lut;
163 
164     width = tv_screen_check_width(tv_screen);
165     height = tv_screen_check_height(tv_screen);
166 
167     ximage = XGetImage(tv_screen->display, tv_screen->window,
168                        0, 0, width, height, AllPlanes, ZPixmap);
169 
170     imagepix = (char *)nvector_alloc(0,height*width,sizeof(char)); 
171     lut = tv_screen->tv->cmap_data_visible->std_lut;
172 
173     bkgrndpix = (int) lut[bkgrnd];
174 
175     count = 0;
176     for (i = 0; i < height; i++)
177     {
178         for (j = 0; j < width; j++)
179         {
180             unsigned long c, grey;
181             XColor  color;
182             int     r, g, b;
183 
184 
185             c = XGetPixel(ximage, j, i);        /* X pixel value */
186             color.pixel = c;
187             XQueryColor(tv_screen->display, tv_screen->colormap, &color);
188             r = color.red >> 8;
189             g = color.green >> 8;
190             b = color.blue >> 8;
191             if (r == g && g == b)
192                grey = r;       /** grey **/
193             else
194                grey = MAX3(r, g, b);
195             imagepix[count++] = grey;
196         }
197     }
198 
199     fwrite(imagepix,sizeof(char),height*width,fp);
200     nvector_free(imagepix,0,sizeof(char)); 
201     XDestroyImage(ximage);
202 }
203 
204 static void tv_screen_write_data_col(FILE * fp, Tv_screen * tv_screen)
205 {
206     int     i, j, width, height;
207     char   *imagepix;
208     int     count = 0;
209     XImage *ximage;
210     int     bkgrndpix;
211     Tina_pixel *lut;
212 
213     width = tv_screen_check_width(tv_screen);
214     height = tv_screen_check_height(tv_screen);
215 
216     ximage = XGetImage(tv_screen->display, tv_screen->window,
217                        0, 0, width, height, AllPlanes, ZPixmap);
218 
219     imagepix = (char *)nvector_alloc(0,3*height*width,sizeof(char)); 
220     lut = tv_screen->tv->cmap_data_visible->std_lut;
221 
222     bkgrndpix = (int) lut[bkgrnd];
223 
224     count = 0;
225     for (i = 0; i < height; i++)
226     {
227         for (j = 0; j < width; j++)
228         {
229             unsigned long c, grey;
230             XColor  color;
231             int     r, g, b;
232 
233 
234             c = XGetPixel(ximage, j, i);        /* X pixel value */
235             color.pixel = c;
236             XQueryColor(tv_screen->display, tv_screen->colormap, &color);
237             r = color.red >> 8;
238             g = color.green >> 8;
239             b = color.blue >> 8;
240             imagepix[count++] = r;
241             imagepix[count++] = g;
242             imagepix[count++] = b;
243         }
244     }
245 
246     fwrite(imagepix,sizeof(char),3*height*width,fp);
247     nvector_free(imagepix,0,sizeof(char)); 
248     XDestroyImage(ximage);
249 }
250 
251 void tv_screen_tiff_dump_1bpp(Tv_screen * tv_screen, char *tiff_dump_file)
252 {
253     FILE   *fp = fopen(tiff_dump_file, "w");
254     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
255 
256     if (fp == NULL)
257     {
258       error("epsf_dump: couldn't open file\n", non_fatal);
259       return;
260     }
261     width = tv_screen_check_width(tv_screen);
262     height = tv_screen_check_height(tv_screen);
263     bytes = height*(width+7)/8;
264     
265     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
266     fwrite_2(ifd,sizeof(char),2,fp,tiff_dump_file);
267     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
268     image_width[11] = width%256;
269     image_width[10] = width/256;
270     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
271     image_height[11] = height%256;
272     image_height[10] = height/256;
273     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
274     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
275     fwrite_2(photonumeric,sizeof(char),12,fp,tiff_dump_file);
276     stripoffsets[11] = 8+2+132+8+8+8;
277     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
278     rowsperstrip[11] = image_height[11];
279     rowsperstrip[10] = image_height[10];
280     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
281     stripbytes[11] = bytes%256;
282     bytes /=256;
283     stripbytes[10] = bytes%256;
284     bytes /=256;
285     stripbytes[9] = bytes%256;
286     bytes /=256;
287     stripbytes[8] = bytes%256;
288     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
289     xresolution[11] = stripoffsets[11]-16;
290     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
291     yresolution[11] = stripoffsets[11]-8;
292     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
293     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
294     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
295     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
296     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
297 
298     tv_screen_write_data_1bpp(fp, tv_screen);
299     fclose(fp);
300 }
301 
302 void tv_screen_tiff_dump_bw(Tv_screen * tv_screen, char *tiff_dump_file)
303 {
304     FILE   *fp = fopen(tiff_dump_file, "w");
305     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
306 
307     if (fp == NULL)
308     {
309       error("epsf_dump: couldn't open file\n", non_fatal);
310       return;
311     }
312     width = tv_screen_check_width(tv_screen);
313     height = tv_screen_check_height(tv_screen);
314     bytes = height*width;
315     
316     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
317     fwrite_2(ifd,sizeof(char),2,fp,tiff_dump_file);
318     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
319     image_width[11] = width%256;
320     image_width[10] = width/256;
321     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
322     image_height[11] = height%256;
323     image_height[10] = height/256;
324     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
325     fwrite_2(bitspersample,sizeof(char),12,fp,tiff_dump_file);
326     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
327     fwrite_2(photonumeric,sizeof(char),12,fp,tiff_dump_file);
328     stripoffsets[11] = 8+2+144+8+8+8;
329     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
330     rowsperstrip[11] = image_height[11];
331     rowsperstrip[10] = image_height[10];
332     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
333     stripbytes[11] = bytes%256;
334     bytes /=256;
335     stripbytes[10] = bytes%256;
336     bytes /=256;
337     stripbytes[9] = bytes%256;
338     bytes /=256;
339     stripbytes[8] = bytes%256;
340     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
341     xresolution[11] = stripoffsets[11]-16;
342     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
343     yresolution[11] = stripoffsets[11]-8;
344     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
345     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
346     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
347     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
348     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
349 
350     tv_screen_write_data_bw(fp, tv_screen);
351     fclose(fp);
352 }
353 
354 void tv_screen_tiff_dump_color(Tv_screen * tv_screen, char *tiff_dump_file)
355 {
356     FILE   *fp = fopen(tiff_dump_file, "w");
357     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
358 
359     if (fp == NULL)
360     {
361       error("epsf_dump: couldn't open file\n", non_fatal);
362       return;
363     }
364     width = tv_screen_check_width(tv_screen);
365     height = tv_screen_check_height(tv_screen);
366     bytes = 3*height*width;
367     
368     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
369     fwrite_2(ifdcol,sizeof(char),2,fp,tiff_dump_file);
370     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
371     image_width[11] = width%256;
372     image_width[10] = width/256;
373     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
374     image_height[11] = height%256;
375     image_height[10] = height/256;
376     stripoffsets[11] = 8+2+156+8+6+8+8;
377     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
378     bitspersamcol[11] = stripoffsets[11]-22;
379     fwrite_2(bitspersamcol,sizeof(char),12,fp,tiff_dump_file);
380     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
381     fwrite_2(photonumecol,sizeof(char),12,fp,tiff_dump_file);
382     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
383     fwrite_2(samplesperpix,sizeof(char),12,fp,tiff_dump_file);
384     rowsperstrip[11] = image_height[11];
385     rowsperstrip[10] = image_height[10];
386     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
387     stripbytes[11] = bytes%256;
388     bytes /=256;
389     stripbytes[10] = bytes%256;
390     bytes /=256;
391     stripbytes[9] = bytes%256;
392     bytes /=256;
393     stripbytes[8] = bytes%256;
394     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
395     xresolution[11] = stripoffsets[11]-16;
396     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
397     yresolution[11] = stripoffsets[11]-8;
398     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
399     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
400     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
401     fwrite_2(coldata,sizeof(char),6,fp,tiff_dump_file);
402     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
403     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
404 
405     tv_screen_write_data_col(fp, tv_screen);
406     fclose(fp);
407 }
408 
409 static void set_tiff_dump_type(Dump_type type)
410 {
411     if (tiff_dump == NULL)
412         tiff_dump = tv_screen_tiff_dump_color;
413 
414     switch (type)
415     {
416         case DT_BW:
417         tiff_dump = tv_screen_tiff_dump_bw;
418         break;
419     case DT_COLOR:
420         tiff_dump = tv_screen_tiff_dump_color;
421         break;
422     case DT_ONE_BIT_PER_PIXEL:
423         tiff_dump = tv_screen_tiff_dump_1bpp;
424         break;
425     }
426 }
427 
428 void    tv_screen_tiff_dump(Dump_type type, Tv_screen * tv_screen, char *filename)
429 {
430     if (tv_screen == NULL || tv_screen->tv == NULL)
431         return;
432     set_tiff_dump_type(type);
433     (*tiff_dump) (tv_screen, filename);
434 }
435 

~ [ 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.