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

Linux Cross Reference
Tina5/tina-tools/tinatool/gphx/gdk2/gphxGdk_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/gdk2/gphxGdk_tiff_dump.c,v $
 37  * Date    :  $Date: 2007/12/07 01:09:00 $
 38  * Version :  $Revision: 1.2 $
 39  * CVS Id  :  $Id: gphxGdk_tiff_dump.c,v 1.2 2007/12/07 01:09:00 paul Exp $
 40  *
 41  * Author  :  paul.bromiley@manchester.ac.uk
 42  *
 43  *********
 44 */
 45 /** 
 46  *  @file  gphxGdk_***.c
 47  *  @brief .
 48  *
 49  *  . 
 50  *
 51  *  Note: the functions in this file are duplicated for each graphics lib (x11, GDK, etc ...).
 52  * 
 53  *  The xview and motif code uses the max of r,g,b in the B&W code and a comparison to the 
 54  *  TINA background color in the 1bpp code. The former gives a problem writing out imcalc
 55  *  histograms as B&W (the red histogram bars disappear) and the latter would require some
 56  *  awkward colormap conversions from TINA colors to GdkRGB.  Therefore, I have changed 
 57  *  both of these.  The B&W code now writes out intesity (i.e. the I of an HSI conversion 
 58  *  of the RGB).  The B&W code splits the intensity at 128: this should work since all TINA
 59  *  Tv's scale the dynamic range of the images, regardless of the highest intensity.
 60  *  PAB 03/12/2007 
 61  */
 62 
 63 
 64 #include <tina/file/fileUtil_io.h>
 65 #include "gphxGdk_tiff_dump.h"
 66 
 67 #if HAVE_CONFIG_H
 68   #include <config.h>
 69 #endif
 70 
 71 #include <stdio.h>
 72 #include <tinatool/gphx/gdk2/gphx_GdkDef.h>
 73 #include <tinatool/gphx/gdk2/gphxGdk_size.h>
 74 #include <tinatool/gphx/gdk2/gphxGdk_picture.h>
 75 
 76 static void (*tiff_dump) (Tv_screen *, char *) = NULL;
 77 
 78 static unsigned char hdr[8] = { 0x4D, 0x4D, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x08};
 79 /* endian
 80     hdr[0] = 0x49;
 81     hdr[1] = 0x49;
 82 */
 83 static unsigned char ifd[2] = {0x00,0x0C};
 84 static unsigned char ifdcol[2] = {0x00,0x0D};
 85 /* data fields defined tags(4), type(4) , and value/offset(4) */
 86 static unsigned char newsubfile[12] = {0x00,0xFE,0x00,0x04,0x00,0x00,0x00,0x01,
 87                                                    0x00,0x00,0x00,0x00};
 88 static unsigned char image_width[12] = {0x01,0x00,0x00,0x04,0x00,0x00,0x00,0x01,
 89                                                    0x00,0x00,0x00,0x00};
 90 static unsigned char image_height[12] = {0x01,0x01,0x00,0x04,0x00,0x00,0x00,0x01,
 91                                                    0x00,0x00,0x00,0x00};
 92 static unsigned char bitspersample[12] = {0x01,0x02,0x00,0x03,0x00,0x00,0x00,0x01,
 93                                                    0x00,0x08,0x00,0x00};
 94 static unsigned char bitspersamcol[12] = {0x01,0x02,0x00,0x03,0x00,0x00,0x00,0x03,
 95                                                    0x00,0x00,0x00,0x00};
 96 static unsigned char coldata[6] = {0x00,0x08,0x00,0x08,0x00,0x08};
 97 static unsigned char compression[12] = {0x01,0x03,0x00,0x03,0x00,0x00,0x00,0x01,
 98                                                    0x00,0x01,0x00,0x00};
 99 static unsigned char photonumeric[12] = {0x01,0x06,0x00,0x03,0x00,0x00,0x00,0x01,
100                                                    0x00,0x01,0x00,0x00};
101 static unsigned char photonumecol[12] = {0x01,0x06,0x00,0x03,0x00,0x00,0x00,0x01,
102                                                    0x00,0x02,0x00,0x00};
103 static unsigned char stripoffsets[12] = {0x01,0x11,0x00,0x04,0x00,0x00,0x00,0x01,
104                                                    0x00,0x00,0x00,0x00};
105 static unsigned char rowsperstrip[12] = {0x01,0x16,0x00,0x04,0x00,0x00,0x00,0x01,
106                                                    0x00,0x00,0x00,0x00};
107 static unsigned char stripbytes[12] = {0x01,0x17,0x00,0x04,0x00,0x00,0x00,0x01,
108                                                    0x00,0x00,0x00,0x00};
109 static unsigned char xresolution[12] = {0x01,0x1A,0x00,0x05,0x00,0x00,0x00,0x01,
110                                                    0x00,0x00,0x00,0x00};
111 static unsigned char xdata[8] = {0x00,0x00,0x01,0x2C,0x00,0x00,0x00,0x01};
112 static unsigned char yresolution[12] = {0x01,0x1B,0x00,0x05,0x00,0x00,0x00,0x01,
113                                                    0x00,0x00,0x00,0x00};
114 static unsigned char ydata[8] = {0x00,0x00,0x01,0x2C,0x00,0x00,0x00,0x01};
115 static unsigned char resolutionunit[12] = {0x01,0x28,0x00,0x03,0x00,0x00,0x00,0x01,
116                                                    0x00,0x01,0x00,0x00};
117 static unsigned char nextifd[8] = {0x00,0x00,0x00,0x00};
118 static unsigned char samplesperpix[12] = {0x01,0x15,0x00,0x03,0x00,0x00,0x00,0x01,
119                                                    0x00,0x03,0x00,0x00};
120 
121 static void tv_screen_write_data_1bpp(FILE * fp, Tv_screen * tv_screen)
122 {
123     Tv_picture *picture=NULL;
124     GdkImage *gdkimage=NULL;
125     GdkColormap *colormap;
126     GdkColor color;
127     unsigned long pixel, grey;
128     char   *imagepix;
129     int width, height, x, y, r, g, b, count, bit_count, byte;
130 
131     if (tv_screen == NULL || tv_screen->tv == NULL) return;
132     width = tv_screen_check_width(tv_screen);
133     height = tv_screen_check_height(tv_screen);
134     picture = tv_screen_get_picture(tv_screen);
135     if(picture==NULL)  return;
136 
137     imagepix = (char *)nvector_alloc(0,height*(width+7)/8,sizeof(char)); 
138     gdkimage = (GdkImage *)picture->pixmap;
139     colormap = gdk_rgb_get_cmap();
140 
141     count = 0;
142     for (y = 0; y < height; y++)
143     {
144         bit_count = 0;
145         byte = 0xff;
146         for (x = 0; x < width; x++)
147         {
148             pixel = gdk_image_get_pixel(gdkimage, x, y);
149 
150             gdk_colormap_query_color(colormap, pixel, &color);
151             r = (int)(color.red >>8);
152             g = (int)(color.green >>8);
153             b = (int)(color.blue >>8);
154             grey = (r+g+b)/3; 
155 
156             if (grey<128) byte ^= 0x80 >> bit_count;
157             bit_count = (bit_count + 1) % 8;
158             if (bit_count == 0)
159             {
160                 imagepix[count++] = byte;
161                 byte = 0xff;
162             }
163         }
164         if (bit_count > 0) imagepix[count++] = byte;
165         byte = 0xff;
166     }
167     fwrite(imagepix,sizeof(char),height*(width+7)/8,fp);
168     nvector_free(imagepix,0,sizeof(char));
169     picture_free(picture); 
170 }
171 
172 static void tv_screen_write_data_bw(FILE * fp, Tv_screen * tv_screen)
173 {
174     Tv_picture *picture=NULL;
175     GdkImage *gdkimage=NULL;
176     GdkColormap *colormap;
177     GdkColor color;
178     unsigned long pixel;
179     char   *imagepix;
180     int width, height, x, y, r, g, b, count=0;
181 
182     if (tv_screen == NULL || tv_screen->tv == NULL) return;
183     width = tv_screen_check_width(tv_screen);
184     height = tv_screen_check_height(tv_screen);
185     picture = tv_screen_get_picture(tv_screen);
186     if(picture==NULL)  return;
187 
188     imagepix = (char *)nvector_alloc(0,height*width,sizeof(char)); 
189     gdkimage = (GdkImage *)picture->pixmap;
190     colormap = gdk_rgb_get_cmap();
191 
192     count = 0;
193     for (y = 0; y < height; y++)
194     {
195         for (x = 0; x < width; x++)
196         {
197             pixel = gdk_image_get_pixel(gdkimage, x, y);
198             gdk_colormap_query_color(colormap, pixel, &color);
199             r = (int)(color.red >>8);
200             g = (int)(color.green >>8);
201             b = (int)(color.blue >>8);
202             imagepix[count++] = (r+g+b)/3; 
203         }
204     }
205     fwrite(imagepix,sizeof(char),height*width,fp);
206     nvector_free(imagepix,0,sizeof(char)); 
207     picture_free(picture); 
208 }
209 
210 static void tv_screen_write_data_col(FILE * fp, Tv_screen * tv_screen)
211 {
212     Tv_picture *picture=NULL;
213     GdkImage *gdkimage=NULL;
214     GdkColormap *colormap;
215     GdkColor color;
216     unsigned long pixel;
217     char   *imagepix;
218     int width, height, x, y, count=0;
219 
220     if (tv_screen == NULL || tv_screen->tv == NULL) return;
221     width = tv_screen_check_width(tv_screen);
222     height = tv_screen_check_height(tv_screen);
223     picture = tv_screen_get_picture(tv_screen);
224     if(picture==NULL)  return;
225 
226     imagepix = (char *)nvector_alloc(0,3*height*width,sizeof(char)); 
227     gdkimage = (GdkImage *)picture->pixmap;
228     colormap = gdk_rgb_get_cmap();
229 
230     count = 0;
231     for (y = 0; y < height; y++)
232     {
233         for (x = 0; x < width; x++)
234         {
235             pixel = gdk_image_get_pixel(gdkimage, x, y);
236             gdk_colormap_query_color(colormap, pixel, &color);
237 
238             imagepix[count++] = color.red >>8;
239             imagepix[count++] = color.green >>8;
240             imagepix[count++] = color.blue >>8;
241         }
242     }
243     fwrite(imagepix,sizeof(char),3*height*width,fp);
244     nvector_free(imagepix,0,sizeof(char)); 
245     picture_free(picture); 
246 }
247 
248 void tv_screen_tiff_dump_1bpp(Tv_screen * tv_screen, char *tiff_dump_file)
249 {
250     FILE   *fp = fopen(tiff_dump_file, "w");
251     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
252 
253     if (fp == NULL)
254     {
255       error("epsf_dump: couldn't open file\n", non_fatal);
256       return;
257     }
258     width = tv_screen_check_width(tv_screen);
259     height = tv_screen_check_height(tv_screen);
260     bytes = height*(width+7)/8;
261     
262     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
263     fwrite_2(ifd,sizeof(char),2,fp,tiff_dump_file);
264     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
265     image_width[11] = width%256;
266     image_width[10] = width/256;
267     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
268     image_height[11] = height%256;
269     image_height[10] = height/256;
270     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
271     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
272     fwrite_2(photonumeric,sizeof(char),12,fp,tiff_dump_file);
273     stripoffsets[11] = 8+2+132+8+8+8;
274     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
275     rowsperstrip[11] = image_height[11];
276     rowsperstrip[10] = image_height[10];
277     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
278     stripbytes[11] = bytes%256;
279     bytes /=256;
280     stripbytes[10] = bytes%256;
281     bytes /=256;
282     stripbytes[9] = bytes%256;
283     bytes /=256;
284     stripbytes[8] = bytes%256;
285     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
286     xresolution[11] = stripoffsets[11]-16;
287     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
288     yresolution[11] = stripoffsets[11]-8;
289     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
290     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
291     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
292     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
293     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
294 
295     tv_screen_write_data_1bpp(fp, tv_screen);
296     fclose(fp);
297 }
298 
299 void tv_screen_tiff_dump_bw(Tv_screen * tv_screen, char *tiff_dump_file)
300 {
301     FILE   *fp = fopen(tiff_dump_file, "w");
302     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
303 
304     if (fp == NULL)
305     {
306       error("epsf_dump: couldn't open file\n", non_fatal);
307       return;
308     }
309     width = tv_screen_check_width(tv_screen);
310     height = tv_screen_check_height(tv_screen);
311     bytes = height*width;
312     
313     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
314     fwrite_2(ifd,sizeof(char),2,fp,tiff_dump_file);
315     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
316     image_width[11] = width%256;
317     image_width[10] = width/256;
318     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
319     image_height[11] = height%256;
320     image_height[10] = height/256;
321     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
322     fwrite_2(bitspersample,sizeof(char),12,fp,tiff_dump_file);
323     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
324     fwrite_2(photonumeric,sizeof(char),12,fp,tiff_dump_file);
325     stripoffsets[11] = 8+2+144+8+8+8;
326     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
327     rowsperstrip[11] = image_height[11];
328     rowsperstrip[10] = image_height[10];
329     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
330     stripbytes[11] = bytes%256;
331     bytes /=256;
332     stripbytes[10] = bytes%256;
333     bytes /=256;
334     stripbytes[9] = bytes%256;
335     bytes /=256;
336     stripbytes[8] = bytes%256;
337     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
338     xresolution[11] = stripoffsets[11]-16;
339     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
340     yresolution[11] = stripoffsets[11]-8;
341     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
342     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
343     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
344     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
345     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
346 
347     tv_screen_write_data_bw(fp, tv_screen);
348     fclose(fp);
349 }
350 
351 void tv_screen_tiff_dump_color(Tv_screen * tv_screen, char *tiff_dump_file)
352 {
353     FILE   *fp = fopen(tiff_dump_file, "w");
354     int     width, height, depth, bytes, pixwidth, pixheight, bitsperpixel;
355 
356     if (fp == NULL)
357     {
358       error("epsf_dump: couldn't open file\n", non_fatal);
359       return;
360     }
361     width = tv_screen_check_width(tv_screen);
362     height = tv_screen_check_height(tv_screen);
363     bytes = 3*height*width;
364     
365     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
366     fwrite_2(ifdcol,sizeof(char),2,fp,tiff_dump_file);
367     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
368     image_width[11] = width%256;
369     image_width[10] = width/256;
370     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
371     image_height[11] = height%256;
372     image_height[10] = height/256;
373     stripoffsets[11] = 8+2+156+8+6+8+8;
374     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
375     bitspersamcol[11] = stripoffsets[11]-22;
376     fwrite_2(bitspersamcol,sizeof(char),12,fp,tiff_dump_file);
377     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
378     fwrite_2(photonumecol,sizeof(char),12,fp,tiff_dump_file);
379     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
380     fwrite_2(samplesperpix,sizeof(char),12,fp,tiff_dump_file);
381     rowsperstrip[11] = image_height[11];
382     rowsperstrip[10] = image_height[10];
383     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
384     stripbytes[11] = bytes%256;
385     bytes /=256;
386     stripbytes[10] = bytes%256;
387     bytes /=256;
388     stripbytes[9] = bytes%256;
389     bytes /=256;
390     stripbytes[8] = bytes%256;
391     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
392     xresolution[11] = stripoffsets[11]-16;
393     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
394     yresolution[11] = stripoffsets[11]-8;
395     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
396     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
397     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
398     fwrite_2(coldata,sizeof(char),6,fp,tiff_dump_file);
399     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
400     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
401 
402     tv_screen_write_data_col(fp, tv_screen);
403     fclose(fp);
404 }
405 
406 static void set_tiff_dump_type(Dump_type type)
407 {
408     if (tiff_dump == NULL)
409         tiff_dump = tv_screen_tiff_dump_color;
410 
411     switch (type)
412     {
413         case DT_BW:
414         tiff_dump = tv_screen_tiff_dump_bw;
415         break;
416     case DT_COLOR:
417         tiff_dump = tv_screen_tiff_dump_color;
418         break;
419     case DT_ONE_BIT_PER_PIXEL:
420         tiff_dump = tv_screen_tiff_dump_1bpp;
421         break;
422     }
423 }
424 
425 void    tv_screen_tiff_dump(Dump_type type, Tv_screen * tv_screen, char *filename)
426 {
427     if (tv_screen == NULL || tv_screen->tv == NULL)
428         return;
429     set_tiff_dump_type(type);
430     (*tiff_dump) (tv_screen, filename);
431 }
432 

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