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

Linux Cross Reference
Tina6/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_tiff_dump.c
 47  *  @brief Dump the image displayed on-screen in a Tv to a TIFF
 48  *
 49  *  Note: the functions in this file are duplicated for each graphics lib (x11, GDK, etc ...).
 50  * 
 51  *  The xview and motif code uses the max of r,g,b in the B&W code and a comparison to the 
 52  *  TINA background color in the 1bpp code. The former gives a problem writing out imcalc
 53  *  histograms as B&W (the red histogram bars disappear) and the latter would require some
 54  *  awkward colormap conversions from TINA colors to GdkRGB.  Therefore, I have changed 
 55  *  both of these.  The B&W code now writes out intesity (i.e. the I of an HSI conversion 
 56  *  of the RGB).  The B&W code splits the intensity at 128: this should work since all TINA
 57  *  Tv's scale the dynamic range of the images, regardless of the highest intensity.
 58  *  PAB 03/12/2007 
 59  */
 60 
 61 
 62 #include <tina/file/fileUtil_io.h>
 63 #include "gphxGdk_tiff_dump.h"
 64 
 65 #if HAVE_CONFIG_H
 66   #include <config.h>
 67 #endif
 68 
 69 #include <stdio.h>
 70 #include <tinatool/gphx/gdk2/gphx_GdkDef.h>
 71 #include <tinatool/gphx/gdk2/gphxGdk_size.h>
 72 #include <tinatool/gphx/gdk2/gphxGdk_picture.h>
 73 
 74 static void (*tiff_dump) (Tv_screen *, char *) = NULL;
 75 
 76 static unsigned char hdr[8] = { 0x4D, 0x4D, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x08};
 77 /* endian
 78     hdr[0] = 0x49;
 79     hdr[1] = 0x49;
 80 */
 81 static unsigned char ifd[2] = {0x00,0x0C};
 82 static unsigned char ifdcol[2] = {0x00,0x0D};
 83 /* data fields defined tags(4), type(4) , and value/offset(4) */
 84 static unsigned char newsubfile[12] = {0x00,0xFE,0x00,0x04,0x00,0x00,0x00,0x01,
 85                                                    0x00,0x00,0x00,0x00};
 86 static unsigned char image_width[12] = {0x01,0x00,0x00,0x04,0x00,0x00,0x00,0x01,
 87                                                    0x00,0x00,0x00,0x00};
 88 static unsigned char image_height[12] = {0x01,0x01,0x00,0x04,0x00,0x00,0x00,0x01,
 89                                                    0x00,0x00,0x00,0x00};
 90 static unsigned char bitspersample[12] = {0x01,0x02,0x00,0x03,0x00,0x00,0x00,0x01,
 91                                                    0x00,0x08,0x00,0x00};
 92 static unsigned char bitspersamcol[12] = {0x01,0x02,0x00,0x03,0x00,0x00,0x00,0x03,
 93                                                    0x00,0x00,0x00,0x00};
 94 static unsigned char coldata[6] = {0x00,0x08,0x00,0x08,0x00,0x08};
 95 static unsigned char compression[12] = {0x01,0x03,0x00,0x03,0x00,0x00,0x00,0x01,
 96                                                    0x00,0x01,0x00,0x00};
 97 static unsigned char photonumeric[12] = {0x01,0x06,0x00,0x03,0x00,0x00,0x00,0x01,
 98                                                    0x00,0x01,0x00,0x00};
 99 static unsigned char photonumecol[12] = {0x01,0x06,0x00,0x03,0x00,0x00,0x00,0x01,
100                                                    0x00,0x02,0x00,0x00};
101 static unsigned char stripoffsets[12] = {0x01,0x11,0x00,0x04,0x00,0x00,0x00,0x01,
102                                                    0x00,0x00,0x00,0x00};
103 static unsigned char rowsperstrip[12] = {0x01,0x16,0x00,0x04,0x00,0x00,0x00,0x01,
104                                                    0x00,0x00,0x00,0x00};
105 static unsigned char stripbytes[12] = {0x01,0x17,0x00,0x04,0x00,0x00,0x00,0x01,
106                                                    0x00,0x00,0x00,0x00};
107 static unsigned char xresolution[12] = {0x01,0x1A,0x00,0x05,0x00,0x00,0x00,0x01,
108                                                    0x00,0x00,0x00,0x00};
109 static unsigned char xdata[8] = {0x00,0x00,0x01,0x2C,0x00,0x00,0x00,0x01};
110 static unsigned char yresolution[12] = {0x01,0x1B,0x00,0x05,0x00,0x00,0x00,0x01,
111                                                    0x00,0x00,0x00,0x00};
112 static unsigned char ydata[8] = {0x00,0x00,0x01,0x2C,0x00,0x00,0x00,0x01};
113 static unsigned char resolutionunit[12] = {0x01,0x28,0x00,0x03,0x00,0x00,0x00,0x01,
114                                                    0x00,0x01,0x00,0x00};
115 static unsigned char nextifd[8] = {0x00,0x00,0x00,0x00};
116 static unsigned char samplesperpix[12] = {0x01,0x15,0x00,0x03,0x00,0x00,0x00,0x01,
117                                                    0x00,0x03,0x00,0x00};
118 
119 
120 static void tv_screen_write_data_1bpp(FILE * fp, Tv_screen * tv_screen)
121 {
122     Tv_picture *picture=NULL;
123     GdkPixbuf   *pbuf;
124     guchar *pixels=NULL, *src=NULL;
125     guint width, height, channel, src_stride;
126     unsigned long grey;
127     char   *imagepix;
128     int x, y, r, g, b, count=0, bit_count, byte;
129 
130     if (tv_screen == NULL || tv_screen->tv == NULL) return;
131 
132     picture = tv_screen_get_picture(tv_screen);
133     pbuf = (GdkPixbuf *)picture->pbuf;
134     if(pbuf==NULL) return;
135 
136     width = gdk_pixbuf_get_width(pbuf);
137     height = gdk_pixbuf_get_height(pbuf);
138     channel = gdk_pixbuf_get_n_channels(pbuf);
139     pixels = gdk_pixbuf_get_pixels(pbuf);
140     src_stride = gdk_pixbuf_get_rowstride (pbuf);
141     imagepix = (char *)nvector_alloc(0,height*(width+7)/8,sizeof(char)); 
142 
143     for (y = 0; y < height; y++, pixels += src_stride)
144     {
145         src = pixels;
146         bit_count = 0;
147         byte = 0xff;
148         for (x = 0; x < width; x++, src += channel)
149         {
150             r = src[0];
151             g = src[1];
152             b = src[2];
153 
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 
173 static void tv_screen_write_data_bw(FILE * fp, Tv_screen * tv_screen)
174 {
175     Tv_picture *picture=NULL;
176     GdkPixbuf   *pbuf;
177     guchar *pixels=NULL, *src=NULL;
178     guint width, height, channel, src_stride;
179     char   *imagepix;
180     int x, y, r, g, b, count=0;
181 
182     if (tv_screen == NULL || tv_screen->tv == NULL) return;
183 
184     picture = tv_screen_get_picture(tv_screen);
185     pbuf = (GdkPixbuf *)picture->pbuf;
186     if(pbuf==NULL) return;
187 
188     width = gdk_pixbuf_get_width(pbuf);
189     height = gdk_pixbuf_get_height(pbuf);
190     channel = gdk_pixbuf_get_n_channels(pbuf);
191     pixels = gdk_pixbuf_get_pixels(pbuf);
192     src_stride = gdk_pixbuf_get_rowstride (pbuf);
193     imagepix = (char *)nvector_alloc(0,height*width,sizeof(char)); 
194 
195     for (y = 0; y < height; y++, pixels += src_stride)
196     {
197         src = pixels;
198         for (x = 0; x < width; x++, src += channel)
199         {
200             r = src[0];
201             g = src[1];
202             b = src[2];
203             imagepix[count++] = (r+g+b)/3;
204         }
205     }
206     fwrite(imagepix,sizeof(char),height*width,fp);
207     nvector_free(imagepix,0,sizeof(char)); 
208     picture_free(picture); 
209 }
210 
211 
212 static void tv_screen_write_data_col(FILE * fp, Tv_screen * tv_screen)
213 {
214     Tv_picture *picture=NULL;
215     GdkPixbuf   *pbuf;
216     guchar *pixels=NULL, *src=NULL;
217     guint width, height, channel, src_stride;
218     char   *imagepix;
219     int x, y, count=0;
220 
221     if (tv_screen == NULL || tv_screen->tv == NULL) return;
222 
223     picture = tv_screen_get_picture(tv_screen);
224     pbuf = (GdkPixbuf *)picture->pbuf;
225     if(pbuf==NULL) return;
226 
227     width = gdk_pixbuf_get_width(pbuf);
228     height = gdk_pixbuf_get_height(pbuf);
229     channel = gdk_pixbuf_get_n_channels(pbuf);
230     pixels = gdk_pixbuf_get_pixels(pbuf);
231     src_stride = gdk_pixbuf_get_rowstride (pbuf);
232     imagepix = (char *)nvector_alloc(0,3*height*width,sizeof(char)); 
233 
234     for (y = 0; y < height; y++, pixels += src_stride)
235     {
236         src = pixels;
237         for (x = 0; x < width; x++, src += channel)
238         {
239             imagepix[count++] = src[0];
240             imagepix[count++] = src[1];
241             imagepix[count++] = src[2];
242         }
243     }
244     fwrite(imagepix,sizeof(char),3*height*width,fp);
245     nvector_free(imagepix,0,sizeof(char)); 
246     picture_free(picture); 
247 }
248 
249 
250 void tv_screen_tiff_dump_1bpp(Tv_screen * tv_screen, char *tiff_dump_file)
251 {
252     FILE   *fp = fopen(tiff_dump_file, "w");
253     int     width, height, bytes;
254 
255     if (fp == NULL)
256     {
257       error("epsf_dump: couldn't open file\n", non_fatal);
258       return;
259     }
260     width = tv_screen_check_width(tv_screen);
261     height = tv_screen_check_height(tv_screen);
262     bytes = height*(width+7)/8;
263 
264     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
265     fwrite_2(ifd,sizeof(char),2,fp,tiff_dump_file);
266     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
267     image_width[11] = width%256;
268     image_width[10] = width/256;
269     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
270     image_height[11] = height%256;
271     image_height[10] = height/256;
272     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
273     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
274     fwrite_2(photonumeric,sizeof(char),12,fp,tiff_dump_file);
275     stripoffsets[11] = 8+2+132+8+8+8;
276     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
277     rowsperstrip[11] = image_height[11];
278     rowsperstrip[10] = image_height[10];
279     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
280     stripbytes[11] = bytes%256;
281     bytes /=256;
282     stripbytes[10] = bytes%256;
283     bytes /=256;
284     stripbytes[9] = bytes%256;
285     bytes /=256;
286     stripbytes[8] = bytes%256;
287     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
288     xresolution[11] = stripoffsets[11]-16;
289     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
290     yresolution[11] = stripoffsets[11]-8;
291     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
292     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
293     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
294     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
295     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
296 
297     tv_screen_write_data_1bpp(fp, tv_screen);
298     fclose(fp);
299 }
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, bytes;
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 
355 void tv_screen_tiff_dump_color(Tv_screen * tv_screen, char *tiff_dump_file)
356 {
357     FILE   *fp = fopen(tiff_dump_file, "w");
358     int     width, height, bytes;
359 
360     if (fp == NULL)
361     {
362       error("epsf_dump: couldn't open file\n", non_fatal);
363       return;
364     }
365     width = tv_screen_check_width(tv_screen);
366     height = tv_screen_check_height(tv_screen);
367     bytes = 3*height*width;
368 
369     fwrite_2(hdr,sizeof(char),8,fp,tiff_dump_file);
370     fwrite_2(ifdcol,sizeof(char),2,fp,tiff_dump_file);
371     fwrite_2(newsubfile,sizeof(char),12,fp,tiff_dump_file);
372     image_width[11] = width%256;
373     image_width[10] = width/256;
374     fwrite_2(image_width,sizeof(char),12,fp,tiff_dump_file);
375     image_height[11] = height%256;
376     image_height[10] = height/256;
377     stripoffsets[11] = 8+2+156+8+6+8+8;
378     fwrite_2(image_height,sizeof(char),12,fp,tiff_dump_file);
379     bitspersamcol[11] = stripoffsets[11]-22;
380     fwrite_2(bitspersamcol,sizeof(char),12,fp,tiff_dump_file);
381     fwrite_2(compression,sizeof(char),12,fp,tiff_dump_file);
382     fwrite_2(photonumecol,sizeof(char),12,fp,tiff_dump_file);
383     fwrite_2(stripoffsets,sizeof(char),12,fp,tiff_dump_file);
384     fwrite_2(samplesperpix,sizeof(char),12,fp,tiff_dump_file);
385     rowsperstrip[11] = image_height[11];
386     rowsperstrip[10] = image_height[10];
387     fwrite_2(rowsperstrip,sizeof(char),12,fp,tiff_dump_file);
388     stripbytes[11] = bytes%256;
389     bytes /=256;
390     stripbytes[10] = bytes%256;
391     bytes /=256;
392     stripbytes[9] = bytes%256;
393     bytes /=256;
394     stripbytes[8] = bytes%256;
395     fwrite_2(stripbytes,sizeof(char),12,fp,tiff_dump_file);
396     xresolution[11] = stripoffsets[11]-16;
397     fwrite_2(xresolution,sizeof(char),12,fp,tiff_dump_file);
398     yresolution[11] = stripoffsets[11]-8;
399     fwrite_2(yresolution,sizeof(char),12,fp,tiff_dump_file);
400     fwrite_2(resolutionunit,sizeof(char),12,fp,tiff_dump_file);
401     fwrite_2(nextifd,sizeof(char),8,fp,tiff_dump_file);
402     fwrite_2(coldata,sizeof(char),6,fp,tiff_dump_file);
403     fwrite_2(xdata,sizeof(char),8,fp,tiff_dump_file);
404     fwrite_2(ydata,sizeof(char),8,fp,tiff_dump_file);
405 
406     tv_screen_write_data_col(fp, tv_screen);
407     fclose(fp);
408 }
409 
410 static void set_tiff_dump_type(Dump_type type)
411 {
412     if (tiff_dump == NULL)
413         tiff_dump = tv_screen_tiff_dump_color;
414 
415     switch (type)
416     {
417         case DT_BW:
418         tiff_dump = tv_screen_tiff_dump_bw;
419         break;
420     case DT_COLOR:
421         tiff_dump = tv_screen_tiff_dump_color;
422         break;
423     case DT_ONE_BIT_PER_PIXEL:
424         tiff_dump = tv_screen_tiff_dump_1bpp;
425         break;
426     }
427 }
428 
429 void    tv_screen_tiff_dump(Dump_type type, Tv_screen * tv_screen, char *filename)
430 {
431     if (tv_screen == NULL || tv_screen->tv == NULL)
432         return;
433     set_tiff_dump_type(type);
434     (*tiff_dump) (tv_screen, filename);
435 }
436 

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