Main Page   Data Structures   File List   Data Fields   Globals  

img2pnm.c

Go to the documentation of this file.
00001 #include "sadie.h"
00002 
00003 #define UCHAR(c) ((unsigned char) (c))
00004 
00005 /*
00006  * The maximum amount of memory to allocate for data read from the
00007  * file.  If we need more than this, we do it in pieces.
00008  */
00009 
00010 #define MAX_MEMORY      10000           /* don't allocate > 10KB */
00011 
00012 /*
00013  * Define PGM and PNM, i.e. gray images and color images.
00014  */
00015 
00016 #define PGM 1
00017 #define PPM 2
00018 
00019 
00020 /*-Copyright Information------------------------------------------------------*/
00021 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00022 /*----------------------------------------------------------------------------*/
00023 /*-General Information--------------------------------------------------------*/
00024 /*                                                                            */
00025 /*   This function writes a SADIE image from main memory to a PNM file.       */
00026 /*                                                                            */
00027 /*----------------------------------------------------------------------------*/
00028 /*-Interface Information------------------------------------------------------*/
00029 void IMG2PNM (in,name)
00030 IMAGE *in;          /*  I   Pointer to the image.                                 */
00031 char *name;         /*  I   String, containing the name of the disk file.         */
00032 /*----------------------------------------------------------------------------*/
00033 {
00034     int i,j,k;
00035     char msg[SLEN];
00036     FILE *fp=NULL;
00037     int nBytes,nbit=8;
00038     unsigned char *pixelPtr=NULL, *data=NULL;
00039     char header[30];
00040     int count;
00041     PIXEL factor;
00042     char type[5];
00043     int nbnd;
00044     
00045 
00046     /* open image file */
00047     if (!(fp=fopen(name,"w"))) {
00048         sprintf(msg," Can't open file %s.",name);
00049         MESSAGE('E',msg);
00050         goto the_end;
00051     }
00052 
00053     if (!CHECKIMG(in)) {
00054         MESSAGE('E'," Can't identify image.");
00055         goto the_end;
00056     }
00057 
00058     if (NAMES) {
00059         MESSAGE('I',"");
00060         MESSAGE('I',"IMG2PNM");
00061         MESSAGE('I',"");
00062         sprintf(msg," Input image:               %s",in->text);
00063         MESSAGE('I',msg);
00064         sprintf(msg," Output disk file:          %s",name);
00065         MESSAGE('I',msg);
00066         sprintf(msg," Output format:             %d-bit PNM",nbit);
00067         MESSAGE('I',msg);
00068         MESSAGE('I'," ...............");
00069     }
00070 
00071     RANGE(in);
00072     if (in->gmax > in->gmin) {
00073         factor = (PIXEL) 255.0 / (in->gmax - in->gmin);
00074     }
00075 
00076     if (in->nbnd == 3) {
00077         nbnd = 3;
00078         sprintf(type,"P6");
00079     } else {
00080         nbnd = 1;
00081         sprintf(type,"P5");
00082     }
00083         
00084     sprintf(header, "%s\n%d %d\n255\n", type, in->npix, in->nlin);
00085     nBytes = strlen(header);
00086     count = fwrite(header, 1, nBytes, fp);
00087     if (count != nBytes) {
00088         MESSAGE('E',"Can't write header to file.");
00089         goto the_end;
00090     }
00091 
00092     nBytes = in->npix * nbnd;
00093     pixelPtr = (unsigned char *) malloc((unsigned) nBytes);
00094     if (!pixelPtr) {
00095         MESSAGE('E',"Could not allocate memory to write image.");
00096         goto the_end;
00097     }
00098 
00099     for (j=0; j<in->nlin; j++) {
00100         data = pixelPtr;
00101 
00102         for (k=0; k<in->npix; k++) {
00103             for (i=0; i<nbnd; i++) {
00104                 *data++ = (unsigned char) rnd(in->data[i][j][k] * factor);
00105             }
00106         }
00107 
00108         count = fwrite(pixelPtr, 1, nBytes, fp);
00109         if (count != nBytes) {
00110             sprintf(msg, "Error writing PNM image data to file");
00111             MESSAGE('E',msg);
00112             goto the_end;
00113         }
00114     }
00115 
00116  the_end:
00117 
00118     if (pixelPtr) free(pixelPtr);
00119     
00120     if (fp) {
00121         if (fclose(fp)) {
00122             sprintf(msg," Can't close file %s.",name);
00123             MESSAGE('W',msg);
00124         }
00125     }
00126 
00127 }
00128 
00129 
00130 
00131 
00132 
00133 
00134 

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