Main Page   Data Structures   File List   Data Fields   Globals  

img2disk.c

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

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