Main Page   Data Structures   File List   Data Fields   Globals  

img2tiff.c

Go to the documentation of this file.
00001 #include        "sadie.h"
00002 
00003 static
00004 SetEntry (e, tag, type, count, value)
00005      register entry *e;
00006      unsigned short tag;
00007      unsigned long type;
00008      unsigned long count;
00009      unsigned long value;
00010 {
00011   e->tag = tag;
00012   e->type = (unsigned short) type;
00013   e->count = count;
00014   if (type == TSHORT && count == 1)
00015     {
00016       *(unsigned short *) &(e->value) = (unsigned short) value;
00017     }
00018   else
00019     {
00020       e->value = value;
00021     }
00022 }
00023 
00024 /*-Copyright Information------------------------------------------------------*/
00025 /* Copyright (c) 1993 by the University of Arizona Digital Image Analysis Lab */
00026 /*----------------------------------------------------------------------------*/
00027 /*-General Information--------------------------------------------------------*/
00028 /*                                                                            */
00029 /*   This function writes a TIFF image to disk.                               */
00030 /*                                                                            */
00031 /*----------------------------------------------------------------------------*/
00032 /*-Interface Information------------------------------------------------------*/
00033 void
00034 IMG2TIFF (IMAGE * in,           /*  I   Pointer to the image.                            */
00035           char *name,           /*  I   String, containing the name of the disk file.    */
00036           short nbit            /*  I   Bits/pixel in disk file.                         */
00037 /*----------------------------------------------------------------------------*/
00038   )
00039 {
00040   static char array[2] = { 0x00, 0x01 };
00041   unsigned char *data = NULL, *ptr = NULL;
00042   unsigned long *mem = NULL;
00043   unsigned long addr[5], i;
00044   char msg[SLEN];
00045   short error, j, k;
00046   long nbytes, total;
00047   double factor;
00048   FILE *fp = NULL;
00049 
00050   if (TIMES)
00051     TIMING (T_IMG2TIFF);
00052   if (NAMES)
00053     {
00054       MESSAGE ('I', "");
00055       MESSAGE ('I', "IMG2TIFF");
00056       MESSAGE ('I', "");
00057       sprintf (msg, " Input image:               %s", in->text);
00058       MESSAGE ('I', msg);
00059       sprintf (msg, " Output disk file:          %s", name);
00060       MESSAGE ('I', msg);
00061       sprintf (msg, " Output format:             %d-bit TIFF", nbit);
00062       MESSAGE ('I', msg);
00063       MESSAGE ('I', " ...............");
00064     }
00065 
00066   /* check input */
00067   if (!CHECKIMG (in))
00068     {
00069       MESSAGE ('E', " Can't identify image.");
00070       goto the_end;
00071     }
00072 
00073   /* open image file */
00074   if (!(fp = fopen (name, "w")))
00075     {
00076       sprintf (msg, " Can't open file %s.", name);
00077       MESSAGE ('E', msg);
00078       goto the_end;
00079     }
00080 
00081   /* write image header to file */
00082   addr[0] =
00083     sizeof (unsigned short) + sizeof (unsigned short) +
00084     sizeof (unsigned long) + sizeof (unsigned short) + 12 * sizeof (entry) +
00085     sizeof (unsigned long);
00086   addr[1] =
00087     sizeof (unsigned short) + sizeof (unsigned short) +
00088     sizeof (unsigned long) + sizeof (unsigned short) + 12 * sizeof (entry) +
00089     sizeof (unsigned long) + 3 * sizeof (unsigned short);
00090   addr[2] =
00091     sizeof (unsigned short) + sizeof (unsigned short) +
00092     sizeof (unsigned long) + sizeof (unsigned short) + 12 * sizeof (entry) +
00093     sizeof (unsigned long) + 3 * sizeof (unsigned short) +
00094     in->nlin * sizeof (unsigned long);
00095   addr[3] =
00096     sizeof (unsigned short) + sizeof (unsigned short) +
00097     sizeof (unsigned long) + sizeof (unsigned short) + 12 * sizeof (entry) +
00098     sizeof (unsigned long) + 3 * sizeof (unsigned short) +
00099     2 * in->nlin * sizeof (unsigned long);
00100   addr[4] =
00101     sizeof (unsigned short) + sizeof (unsigned short) +
00102     sizeof (unsigned long) + sizeof (unsigned short) + 12 * sizeof (entry) +
00103     sizeof (unsigned long) + 3 * sizeof (unsigned short) +
00104     2 * in->nlin * sizeof (unsigned long) + 2 * sizeof (unsigned long);
00105 
00106   total =
00107     sizeof (unsigned short) + sizeof (unsigned short) +
00108     sizeof (unsigned long) + sizeof (unsigned short) + 12 * sizeof (entry) +
00109     sizeof (unsigned long) + 3 * sizeof (unsigned short) +
00110     2 * in->nlin * sizeof (unsigned long) + 4 * sizeof (unsigned long);
00111   nbytes =
00112     sizeof (unsigned short) + sizeof (unsigned short) +
00113     sizeof (unsigned long) + sizeof (unsigned short);
00114   if ((mem =
00115        (unsigned long *) malloc (nbytes * sizeof (unsigned char))) == NULL)
00116     {
00117       MESSAGE ('E', " Memory request failed.");
00118       goto the_end;
00119     }
00120   ptr = data = (unsigned char *) mem;
00121   *(unsigned short *) ptr = *(short *) array != 1 ? LilEndian : BigEndian;
00122   ptr += sizeof (unsigned short);
00123   *(unsigned short *) ptr = MagicNumber;
00124   ptr += sizeof (unsigned short);
00125   *(unsigned long *) ptr = 8ul;
00126   ptr += sizeof (unsigned long);
00127   *(unsigned short *) ptr = 12u;
00128   ptr += sizeof (unsigned short);
00129   if (fwrite (data, nbytes, 1, fp) != 1)
00130     {
00131       sprintf (msg, " Can't write image header to file %s.", name);
00132       MESSAGE ('E', msg);
00133       goto the_end;
00134     }
00135   free (mem);
00136   mem = NULL;
00137 
00138   nbytes =
00139     12 * sizeof (entry) + sizeof (unsigned long) +
00140     3 * sizeof (unsigned short);
00141   if ((mem =
00142        (unsigned long *) malloc (nbytes * sizeof (unsigned char))) == NULL)
00143     {
00144       MESSAGE ('E', " Memory request failed.");
00145       goto the_end;
00146     }
00147   ptr = data = (unsigned char *) mem;
00148   SetEntry (ptr, ImageWidth, TLONG, 1ul, (unsigned long) in->npix);
00149   ptr += sizeof (entry);
00150   SetEntry (ptr, ImageLength, TLONG, 1ul, (unsigned long) in->nlin);
00151   ptr += sizeof (entry);
00152   SetEntry (ptr, BitsPerSample, TSHORT, (unsigned long) in->nbnd,
00153             in->nbnd == 1 ? 8ul : addr[0]);
00154   ptr += sizeof (entry);
00155   SetEntry (ptr, Compression, TSHORT, 1ul, 1ul);
00156   ptr += sizeof (entry);
00157   SetEntry (ptr, PhotometricInterpretation, TSHORT, 1ul,
00158             in->nbnd == 1 ? 1ul : 2ul);
00159   ptr += sizeof (entry);
00160   SetEntry (ptr, StripOffsets, TLONG, (unsigned long) in->nlin, addr[1]);
00161   ptr += sizeof (entry);
00162   SetEntry (ptr, SamplesPerPixel, TSHORT, 1ul, (unsigned long) in->nbnd);
00163   ptr += sizeof (entry);
00164   SetEntry (ptr, RowsPerStrip, TLONG, 1ul, 1ul);
00165   ptr += sizeof (entry);
00166   SetEntry (ptr, StripByteCounts, TLONG, (unsigned long) in->nlin, addr[2]);
00167   ptr += sizeof (entry);
00168   SetEntry (ptr, XResolution, TRATIONAL, 1ul, addr[3]);
00169   ptr += sizeof (entry);
00170   SetEntry (ptr, YResolution, TRATIONAL, 1ul, addr[4]);
00171   ptr += sizeof (entry);
00172   SetEntry (ptr, ResolutionUnit, TSHORT, 1ul, 2ul);
00173   ptr += sizeof (entry);
00174   *(unsigned long *) ptr = 0ul;
00175   ptr += sizeof (unsigned long);
00176   *(unsigned short *) ptr = (unsigned short) nbit;
00177   ptr += sizeof (unsigned short);
00178   *(unsigned short *) ptr = (unsigned short) nbit;
00179   ptr += sizeof (unsigned short);
00180   *(unsigned short *) ptr = (unsigned short) nbit;
00181   ptr += sizeof (unsigned short);
00182   if (fwrite (data, nbytes, 1, fp) != 1)
00183     {
00184       sprintf (msg, " Can't write image header to file %s.", name);
00185       MESSAGE ('E', msg);
00186       goto the_end;
00187     }
00188   free (mem);
00189   mem = NULL;
00190 
00191   nbytes = 2 * in->nlin * sizeof (unsigned long) + 4 * sizeof (unsigned long);
00192   if ((mem =
00193        (unsigned long *) malloc (nbytes * sizeof (unsigned char))) == NULL)
00194     {
00195       MESSAGE ('E', " Memory request failed.");
00196       goto the_end;
00197     }
00198   ptr = data = (unsigned char *) mem;
00199   for (i = 0ul; i < (unsigned long) in->nlin; i += 1ul)
00200     {
00201       *(unsigned long *) ptr =
00202         (unsigned long) total +
00203         i * (unsigned long) in->nbnd * (unsigned long) in->npix;
00204       ptr += sizeof (unsigned long);
00205     }
00206   for (i = 0ul; i < (unsigned long) in->nlin; i += 1ul)
00207     {
00208       *(unsigned long *) ptr =
00209         (unsigned long) in->nbnd * (unsigned long) in->npix;
00210       ptr += sizeof (unsigned long);
00211     }
00212   *(unsigned long *) ptr = 72ul;
00213   ptr += sizeof (unsigned long);
00214   *(unsigned long *) ptr = 1ul;
00215   ptr += sizeof (unsigned long);
00216   *(unsigned long *) ptr = 72ul;
00217   ptr += sizeof (unsigned long);
00218   *(unsigned long *) ptr = 1ul;
00219   ptr += sizeof (unsigned long);
00220   if (fwrite (data, nbytes, 1, fp) != 1)
00221     {
00222       sprintf (msg, " Can't write image header to file %s.", name);
00223       MESSAGE ('E', msg);
00224       goto the_end;
00225     }
00226   free (mem);
00227   mem = NULL;
00228 
00229   /* write image data to file */
00230   factor = 255.0 / (in->gmax - in->gmin);
00231   nbytes = (long) in->nbnd * (long) in->npix;
00232   if ((ptr = data =
00233        (unsigned char *) malloc (nbytes * sizeof (unsigned char))) == NULL)
00234     {
00235       MESSAGE ('E', " Memory request failed.");
00236       goto the_end;
00237     }
00238   if (in->nbnd == 1)
00239     {
00240       for (j = 0; j < in->nlin; j++)
00241         {
00242           for (ptr = data, k = 0; k < in->npix; k += 1, ptr += 1)
00243             {
00244               ptr[0] =
00245                 (unsigned char) ((in->data[0][j][k] - in->gmin) * factor);
00246             }
00247           if (fwrite (data, nbytes, 1, fp) != 1)
00248             {
00249               sprintf (msg, " Can't write image data to file %s.", name);
00250               MESSAGE ('E', msg);
00251               goto the_end;
00252             }
00253         }
00254     }
00255   else
00256     {
00257       for (j = 0; j < in->nlin; j++)
00258         {
00259           for (ptr = data, k = 0; k < in->npix; k += 1, ptr += 3)
00260             {
00261               ptr[0] =
00262                 (unsigned char) ((in->data[0][j][k] - in->gmin) * factor);
00263               ptr[1] =
00264                 (unsigned char) ((in->data[1][j][k] - in->gmin) * factor);
00265               ptr[2] =
00266                 (unsigned char) ((in->data[2][j][k] - in->gmin) * factor);
00267             }
00268           if (fwrite (data, nbytes, 1, fp) != 1)
00269             {
00270               sprintf (msg, " Can't write image data to file %s.", name);
00271               MESSAGE ('E', msg);
00272               goto the_end;
00273             }
00274         }
00275     }
00276 
00277   /* close image file */
00278 the_end:
00279 
00280   if (data)
00281     free (data);
00282 
00283   if (fp)
00284     {
00285       if (fclose (fp))
00286         {
00287           sprintf (msg, " Can't close file %s.", name);
00288           MESSAGE ('W', msg);
00289         }
00290     }
00291 
00292   if (TIMES)
00293     TIMING (T_EXIT);
00294 }

Generated on Sun May 18 15:36:11 2003 for tclSadie by doxygen1.3