Main Page   Data Structures   File List   Data Fields   Globals  

byteimg2disk.c

Go to the documentation of this file.
00001 #include        <stdio.h>
00002 #include        "sadie.h"
00003 #include        "sadie_byte.h"
00004 
00005 
00006 
00007 
00008 
00009 /*-Copyright Information------------------------------------------------------*/
00010 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00011 /*----------------------------------------------------------------------------*/
00012 /*-General Information--------------------------------------------------------*/
00013 /*                                                                            */
00014 /*   This function writes a SADIE byte image from main memory to disk.        */
00015 /*                                                                            */
00016 /*----------------------------------------------------------------------------*/
00017 /*-Interface Information------------------------------------------------------*/
00018 void BYTEIMG2DISK(
00019 IMAGE_BYTE *in,     /*  I   Pointer to the image.                                 */
00020 unsigned char *name /*  I   String, containing the name of the disk file.         */
00021 )
00022 /*----------------------------------------------------------------------------*/
00023 {   
00024     static char array[2] = { 0x00, 0x01 };
00025     char msg[SLEN];
00026     short nbnd, nlin, npix, nbit;
00027     long nbytes;
00028     FILE *fp=NULL;
00029     PIXEL gmin,gmax;
00030 
00031     if (TIMES) TIMING(T_IMG2DISK);
00032 
00033     /* check input */
00034 
00035     if (!CHECKIMG_BYTE(in)) {
00036         MESSAGE('E'," Can't identify image.");
00037         goto the_end;
00038     }
00039 
00040     if (NAMES) {
00041         MESSAGE('I',"");
00042         MESSAGE('I',"BYTEIMG2DISK");
00043         MESSAGE('I',"");
00044         sprintf(msg," Input image:               %s",in->text);
00045         MESSAGE('I',msg);
00046         sprintf(msg," Output disk file:          %s",name);
00047         MESSAGE('I',msg);
00048         MESSAGE('I'," ...............");
00049     }
00050 
00051 
00052 
00053 
00054     /* Change image name to match disk name */
00055     sprintf(in->text,"%s",name);
00056 
00057 
00058 
00059     /* swap bytes, if necessary */
00060     nbnd = in->nbnd;
00061     nlin = in->nlin;
00062     npix = in->npix;
00063     nbit = in->nbit;
00064     if (*(short *)array != 1) {
00065 
00066         //printf("IMG2DISK: Swapping bytes...\n"); 
00067 
00068     
00069         nbnd = SWAP((unsigned char *)&nbnd);
00070         nlin = SWAP((unsigned char *)&nlin);
00071         npix = SWAP((unsigned char *)&npix);
00072         nbit = SWAP((unsigned char *)&nbit);
00073     } 
00074     //else {
00075     //    //printf("IMG2DISK: No need to swap bytes...\n");
00076     //}
00077 
00078     /* open image file */
00079     if (!(fp=fopen(name,"w"))) {
00080         sprintf(msg," Can't open file %s.",name);
00081         MESSAGE('E',msg);
00082         goto the_end;
00083     }
00084 
00085 
00086 
00087     /*
00088      * write image header to file 
00089      */
00090 
00091     /* number of bands */
00092     if (fwrite(&nbnd,sizeof(nbnd),1,fp) != 1) {
00093         sprintf(msg," Can't write number of bands to file %s.",name);
00094         MESSAGE('E',msg);
00095         goto the_end;
00096     }
00097 
00098     /* number of lines/band */
00099     if (fwrite(&nlin,sizeof(nlin),1,fp) != 1) {
00100         sprintf(msg," Can't write number of lines/band to file %s.",name);
00101         MESSAGE('E',msg);
00102         goto the_end;
00103     }
00104 
00105     /* number of pixels/line */
00106     if (fwrite(&npix,sizeof(npix),1,fp) != 1) {
00107         sprintf(msg," Can't write number of pixels/line to file %s.",name);
00108         MESSAGE('E',msg);
00109         goto the_end;
00110     }
00111 
00112     /* number of bits/pixel */
00113     if (fwrite(&nbit,sizeof(nbit),1,fp) != 1) {
00114         sprintf(msg," Can't write number of bits/pixel to file %s.",name);
00115         MESSAGE('E',msg);
00116         goto the_end;
00117     }
00118 
00119 
00120     gmin = (PIXEL)in->gmin;
00121     if (fwrite(&gmin,sizeof(gmin),1,fp) != 1) {
00122         sprintf(msg," Can't write gmin to file %s.",name);
00123         MESSAGE('E',msg);
00124         goto the_end;
00125     }
00126     gmax = (PIXEL)in->gmax;
00127     if (fwrite(&gmax,sizeof(gmax),1,fp) != 1) {
00128         sprintf(msg," Can't write gmax to file %s.",name);
00129         MESSAGE('E',msg);
00130         goto the_end;
00131     }
00132 
00133     /* rest of header */
00134     nbytes = (long)(sizeof(in->pstr)+sizeof(in->text));
00135     if (fwrite(&(in->pstr),nbytes,1,fp) != 1) {
00136         sprintf(msg," Can't write image header to file %s.",name);
00137         MESSAGE('E',msg);
00138         goto the_end;
00139     }
00140 
00141 
00142 
00143     /* write the image data to file */
00144     nbytes = (long)ceil((double)in->nbnd*(double)in->nlin*(double)in->npix);
00145     if (fwrite(in->data[0][0],nbytes,1,fp) != 1) {
00146         sprintf(msg," Can't write image data to file %s.",name);
00147         MESSAGE('E',msg);
00148     }
00149 
00150 
00151     //printf("in(0,0) --> %d\n",in->data[0][0][0]);
00152 
00153     /* close image file */
00154     the_end:
00155 
00156     if (fp) {
00157         if (fclose(fp)) {
00158             sprintf(msg," Can't close file %s.",name);
00159             MESSAGE('W',msg);
00160         }
00161     }
00162 
00163     if (TIMES) TIMING(T_EXIT);
00164 
00165 }

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