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 SetEntry (e, tag, type, count, value) register entry *e; unsigned short tag; unsigned long type; unsigned long  count; unsigned long  value; {
00004     e->tag   = tag;
00005     e->type  = (unsigned short)type;
00006     e->count = count;
00007     if (type == TSHORT  &&  count == 1) {
00008         *(unsigned short *)&(e->value) = (unsigned short)value;
00009     } else {
00010         e->value = value;
00011     }
00012 }
00013 
00014 /*-Copyright Information------------------------------------------------------*/
00015 /* Copyright (c) 1993 by the University of Arizona Digital Image Analysis Lab */
00016 /*----------------------------------------------------------------------------*/
00017 /*-General Information--------------------------------------------------------*/
00018 /*                                                                            */
00019 /*   This function writes a TIFF image to disk.                               */
00020 /*                                                                            */
00021 /*----------------------------------------------------------------------------*/
00022 /*-Interface Information------------------------------------------------------*/
00023 void IMG2TIFF (
00024 IMAGE *in,           /*  I   Pointer to the image.                            */
00025 char *name,          /*  I   String, containing the name of the disk file.    */
00026 short nbit           /*  I   Bits/pixel in disk file.                         */
00027 /*----------------------------------------------------------------------------*/
00028 ) { static char array[2] = { 0x00, 0x01 };
00029     unsigned char *data=NULL, *ptr=NULL;
00030     unsigned long *mem=NULL;
00031     unsigned long addr[5], i;
00032     char   msg[SLEN];
00033     short  error, j, k;
00034     long   nbytes, total;
00035     double factor;
00036     FILE *fp=NULL;
00037 
00038     if (TIMES) TIMING(T_IMG2TIFF);
00039     if (NAMES) {
00040         MESSAGE('I',"");
00041         MESSAGE('I',"IMG2TIFF");
00042         MESSAGE('I',"");
00043         sprintf(msg," Input image:               %s",in->text);
00044         MESSAGE('I',msg);
00045         sprintf(msg," Output disk file:          %s",name);
00046         MESSAGE('I',msg);
00047         sprintf(msg," Output format:             %d-bit TIFF",nbit);
00048         MESSAGE('I',msg);
00049         MESSAGE('I'," ...............");
00050     }
00051 
00052     /* check input */
00053     if (!CHECKIMG(in)) {
00054         MESSAGE('E'," Can't identify image.");
00055         goto the_end;
00056     }
00057 
00058     /* open image file */
00059     if (!(fp=fopen(name,"w"))) {
00060         sprintf(msg," Can't open file %s.",name);
00061         MESSAGE('E',msg);
00062         goto the_end;
00063     }
00064 
00065     /* write image header to file */
00066     addr[0] = sizeof(unsigned short) + sizeof(unsigned short) + sizeof(unsigned long) + sizeof(unsigned short) + 12*sizeof(entry) + sizeof(unsigned long);
00067     addr[1] = sizeof(unsigned short) + sizeof(unsigned short) + sizeof(unsigned long) + sizeof(unsigned short) + 12*sizeof(entry) + sizeof(unsigned long) + 3*sizeof(unsigned short);
00068     addr[2] = sizeof(unsigned short) + sizeof(unsigned short) + sizeof(unsigned long) + sizeof(unsigned short) + 12*sizeof(entry) + sizeof(unsigned long) + 3*sizeof(unsigned short) +   in->nlin*sizeof(unsigned long);
00069     addr[3] = sizeof(unsigned short) + sizeof(unsigned short) + sizeof(unsigned long) + sizeof(unsigned short) + 12*sizeof(entry) + sizeof(unsigned long) + 3*sizeof(unsigned short) + 2*in->nlin*sizeof(unsigned long);
00070     addr[4] = sizeof(unsigned short) + sizeof(unsigned short) + sizeof(unsigned long) + sizeof(unsigned short) + 12*sizeof(entry) + sizeof(unsigned long) + 3*sizeof(unsigned short) + 2*in->nlin*sizeof(unsigned long) + 2*sizeof(unsigned long);
00071 
00072     total  = sizeof(unsigned short) + sizeof(unsigned short) + sizeof(unsigned long) + sizeof(unsigned short) + 12*sizeof(entry) + sizeof(unsigned long) + 3*sizeof(unsigned short) + 2*in->nlin*sizeof(unsigned long) + 4*sizeof(unsigned long);
00073     nbytes  = sizeof(unsigned short) + sizeof(unsigned short) + sizeof(unsigned long) + sizeof(unsigned short);
00074     if ((mem=(unsigned long *)malloc(nbytes*sizeof(unsigned char))) == NULL) {
00075                 MESSAGE('E'," Memory request failed.");
00076                 goto the_end;
00077     }
00078     ptr=data=(unsigned char *)mem;
00079     *(unsigned short *)ptr = *(short *)array != 1 ? LilEndian : BigEndian;                                   ptr += sizeof(unsigned short);
00080     *(unsigned short *)ptr = MagicNumber;                                                                    ptr += sizeof(unsigned short);
00081     *(unsigned long  *)ptr = 8ul;                                                                            ptr += sizeof(unsigned long);
00082     *(unsigned short *)ptr = 12u;                                                                            ptr += sizeof(unsigned short);
00083     if (fwrite(data,nbytes,1,fp) != 1) {
00084         sprintf(msg," Can't write image header to file %s.",name);
00085         MESSAGE('E',msg);
00086         goto the_end;
00087     }
00088     free(mem);
00089     mem = NULL;
00090 
00091     nbytes  = 12*sizeof(entry) + sizeof(unsigned long) + 3*sizeof(unsigned short);
00092     if ((mem=(unsigned long *)malloc(nbytes*sizeof(unsigned char))) == NULL) {
00093                 MESSAGE('E'," Memory request failed.");
00094                 goto the_end;
00095     }
00096     ptr=data=(unsigned char *)mem;
00097     SetEntry(ptr,ImageWidth               ,TLONG    ,1ul,(unsigned long)in->npix);                           ptr += sizeof(entry);
00098     SetEntry(ptr,ImageLength              ,TLONG    ,1ul,(unsigned long)in->nlin);                           ptr += sizeof(entry);
00099     SetEntry(ptr,BitsPerSample            ,TSHORT   ,(unsigned long)in->nbnd,in->nbnd == 1 ? 8ul : addr[0]); ptr += sizeof(entry);
00100     SetEntry(ptr,Compression              ,TSHORT   ,1ul,1ul);                                               ptr += sizeof(entry);
00101     SetEntry(ptr,PhotometricInterpretation,TSHORT   ,1ul,in->nbnd == 1 ? 1ul : 2ul);                         ptr += sizeof(entry);
00102     SetEntry(ptr,StripOffsets             ,TLONG    ,(unsigned long)in->nlin,addr[1]);                       ptr += sizeof(entry);
00103     SetEntry(ptr,SamplesPerPixel          ,TSHORT   ,1ul,(unsigned long)in->nbnd);                           ptr += sizeof(entry);
00104     SetEntry(ptr,RowsPerStrip             ,TLONG    ,1ul,1ul);                                               ptr += sizeof(entry);
00105     SetEntry(ptr,StripByteCounts          ,TLONG    ,(unsigned long)in->nlin,addr[2]);                       ptr += sizeof(entry);
00106     SetEntry(ptr,XResolution              ,TRATIONAL,1ul,addr[3]);                                           ptr += sizeof(entry);
00107     SetEntry(ptr,YResolution              ,TRATIONAL,1ul,addr[4]);                                           ptr += sizeof(entry);
00108     SetEntry(ptr,ResolutionUnit           ,TSHORT   ,1ul,2ul);                                               ptr += sizeof(entry);
00109     *(unsigned long  *)ptr = 0ul;                                                                            ptr += sizeof(unsigned long);
00110     *(unsigned short *)ptr = (unsigned short)nbit;                                                           ptr += sizeof(unsigned short);
00111     *(unsigned short *)ptr = (unsigned short)nbit;                                                           ptr += sizeof(unsigned short);
00112     *(unsigned short *)ptr = (unsigned short)nbit;                                                           ptr += sizeof(unsigned short);
00113     if (fwrite(data,nbytes,1,fp) != 1) {
00114         sprintf(msg," Can't write image header to file %s.",name);
00115         MESSAGE('E',msg);
00116         goto the_end;
00117     }
00118     free(mem);
00119     mem = NULL;
00120 
00121     nbytes  = 2*in->nlin*sizeof(unsigned long) + 4*sizeof(unsigned long);
00122     if ((mem=(unsigned long *)malloc(nbytes*sizeof(unsigned char))) == NULL) {
00123                 MESSAGE('E'," Memory request failed.");
00124                 goto the_end;
00125     }
00126     ptr=data=(unsigned char *)mem;
00127     for (i=0ul; i<(unsigned long)in->nlin; i+=1ul) {
00128         *(unsigned long *)ptr = (unsigned long)total + i*(unsigned long)in->nbnd*(unsigned long)in->npix;   ptr += sizeof(unsigned long);
00129     }
00130     for (i=0ul; i<(unsigned long)in->nlin; i+=1ul) {
00131         *(unsigned long *)ptr = (unsigned long)in->nbnd*(unsigned long)in->npix;                             ptr += sizeof(unsigned long);
00132     }
00133     *(unsigned long  *)ptr = 72ul;                                                                           ptr += sizeof(unsigned long);
00134     *(unsigned long  *)ptr =  1ul;                                                                           ptr += sizeof(unsigned long);
00135     *(unsigned long  *)ptr = 72ul;                                                                           ptr += sizeof(unsigned long);
00136     *(unsigned long  *)ptr =  1ul;                                                                           ptr += sizeof(unsigned long);
00137     if (fwrite(data,nbytes,1,fp) != 1) {
00138         sprintf(msg," Can't write image header to file %s.",name);
00139         MESSAGE('E',msg);
00140         goto the_end;
00141     }
00142     free(mem);
00143     mem = NULL;
00144 
00145     /* write image data to file */
00146     factor = 255.0 / (in->gmax-in->gmin);
00147     nbytes = (long)in->nbnd*(long)in->npix;
00148     if ((ptr=data=(unsigned char *)malloc(nbytes*sizeof(unsigned char))) == NULL) {
00149                 MESSAGE('E'," Memory request failed.");
00150                 goto the_end;
00151     }
00152     if (in->nbnd == 1) {
00153         for (j=0; j<in->nlin; j++) {
00154             for (ptr=data, k=0; k<in->npix; k+=1, ptr+=1) {
00155                 ptr[0] = (unsigned char)((in->data[0][j][k]-in->gmin)*factor);
00156             }
00157             if (fwrite(data,nbytes,1,fp) != 1) {
00158                 sprintf(msg," Can't write image data to file %s.",name);
00159                 MESSAGE('E',msg);
00160                 goto the_end;
00161             }
00162         }
00163     } else {
00164         for (j=0; j<in->nlin; j++) {
00165             for (ptr=data, k=0; k<in->npix; k+=1, ptr+=3) {
00166                 ptr[0] = (unsigned char)((in->data[0][j][k]-in->gmin)*factor);
00167                 ptr[1] = (unsigned char)((in->data[1][j][k]-in->gmin)*factor);
00168                 ptr[2] = (unsigned char)((in->data[2][j][k]-in->gmin)*factor);
00169             }
00170             if (fwrite(data,nbytes,1,fp) != 1) {
00171                 sprintf(msg," Can't write image data to file %s.",name);
00172                 MESSAGE('E',msg);
00173                 goto the_end;
00174             }
00175         }
00176     }
00177 
00178     /* close image file */
00179     the_end:
00180 
00181     if (data) free(data);
00182     
00183     if (fp) {
00184         if (fclose(fp)) {
00185             sprintf(msg," Can't close file %s.",name);
00186             MESSAGE('W',msg);
00187         }
00188     }
00189 
00190     if (TIMES) TIMING(T_EXIT);
00191 }

Generated on Wed Apr 9 08:56:07 2003 for TREES by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002