Main Page   Data Structures   File List   Data Fields   Globals  

mosaic_import.c

Go to the documentation of this file.
00001 #include        <stdio.h>
00002 #include        "sadie.h"
00003 #include        "mosaic.h"
00004 
00005 
00006 /*-Copyright Information------------------------------------------------------*/
00007 /* Copyright (c) 1999 by the University of Arizona Digital Image Analysis Lab */
00008 /*----------------------------------------------------------------------------*/
00009 /*-Programmer Information-----------------------------------------------------*/
00010 /*                                                                            */
00011 /*   This function imports an image subsection from a mosaic disk file into   */
00012 /*   main memory.                                                             */
00013 /*   It is based on import.c                                                  */
00014 /*                                                                            */
00015 /*----------------------------------------------------------------------------*/
00016 /*-General Information--------------------------------------------------------*/
00017 /*                                                                            */
00018 /*   This function reads an image from disk into main memory.                 */
00019 /*                                                                            */
00020 /*----------------------------------------------------------------------------*/
00021 /*-Interface Information------------------------------------------------------*/
00022 void MOSAIC_IMPORT (
00023 MOSAIC_INDEX *index, /*  I   Index information for MOSAIC_IMAGE file.         */
00024 char *name,          /*  I   String, containing the name of the disk file.    */
00025 int jbgn,            /*  I   Index of the first line to be read.              */
00026 int kbgn,            /*  I   Index of the first pixel to be read.             */
00027 int jinc,            /*  I   Line increment during sampling.                  */
00028 int kinc,            /*  I   Pixels/line increment during sampling.           */
00029 int jend,            /*  I   Index of the last line to be read.               */
00030 int kend,            /*  I   Index of the last pixel to be read.              */
00031 IMAGE **out     /*  O   Address of a pointer to the output image.        */
00032 /*----------------------------------------------------------------------------*/
00033 ) { 
00034     register int j, k;
00035     char  msg[SLEN];
00036     int lines, pixels;
00037     int outj, outk, nlin, npix;
00038     int nbytes;
00039     PIXEL_BYTE *column=NULL, *ptr=NULL;
00040     FILE *fp=NULL;
00041 
00042     /* if (TIMES) TIMING(T_MOSAIC_IMPORT); */
00043 
00044 /*    printf("Importing mosaic of image of size %dx%d from %d,%d to %d,%d with increment %d,%d\n",index->npix,index->nlin,kbgn,jbgn,kend,jend,kinc,jinc); */
00045 
00046     /* check input */
00047     if (!CHECKIMG_MOSAIC_INDEX(index)) {
00048         MESSAGE('E'," Can't identify mosaic index.");
00049         goto the_end;
00050     } else if (0 > jbgn  ||  jend >= index->nlin) {
00051         MESSAGE('E'," Image to be sampled must be located completely inside input image in the line dimension.");
00052         goto the_end;
00053     } else if (0 > kbgn  ||  kend >= index->npix) {
00054         MESSAGE('E'," Image to be sampled must be located completely inside input image in the pixel dimension.");
00055         goto the_end;
00056     } else if (jinc <= 0) {
00057         MESSAGE('E'," Line subsample increment must be greater than zero.");
00058         goto the_end;
00059     } else if (kinc <= 0) {
00060         MESSAGE('E'," Pixel subsample increment must be greater than zero.");
00061         goto the_end;
00062     } else if (jbgn > jend) {
00063         MESSAGE('E'," Number of lines to be spanned must be greater than zero.");
00064         goto the_end;
00065     } else if (kbgn > kend) {
00066         MESSAGE('E'," Number of pixels/line to be spanned must be greater than zero.");
00067         goto the_end;
00068     }
00069 
00070     /* open mosaic image file */
00071     if (!(fp=fopen(name,"r"))) {
00072         sprintf(msg," Can't open file %s.",name);
00073         MESSAGE('E',msg);
00074         goto the_end;
00075     }
00076 
00077     /* Set file pointer to first byte after header */
00078     fseek(fp, (long) 512, SEEK_SET);
00079 
00080 
00081     /* create image of appropriate size */
00082     lines  = max(1,(jend-jbgn+1)/jinc);
00083     pixels = max(1,(kend-kbgn+1)/kinc);
00084     if (!CHECKIMG(*out)) GETMEM(1,lines,pixels,out);
00085     if (!*out) goto the_end;
00086     (*out)->nbit = 8;
00087 
00088     /* read the image data from file */
00089     nbytes = (int)index->height_max;
00090     if ((column=(PIXEL_BYTE *)malloc((long)index->height_max * sizeof(PIXEL_BYTE))) == NULL) {
00091         MESSAGE('E'," Memory request failed.");
00092         goto the_end;
00093     }   
00094     if (fseek(fp,(long)(nbytes * (long)kbgn),SEEK_CUR) != 0) {
00095         sprintf(msg," Can't skip to column %d in file %s.",kbgn,name);
00096         MESSAGE('E',msg);
00097         goto the_end;
00098     }
00099     for (k = kbgn + (kinc-1), outk=0; outk<pixels; k += kinc, outk++) {
00100         if (fseek(fp,(long)(nbytes * (long)(kinc - 1)),SEEK_CUR) != 0) {
00101             sprintf(msg," Can't column subsample by %d in file %s.",kinc,name);
00102             MESSAGE('E',msg);
00103             goto the_end;
00104         }
00105         if (fread(column,nbytes,1,fp) != 1) {
00106             sprintf(msg," Can't read image data from file %s.",name);
00107             MESSAGE('E',msg);
00108             goto the_end;
00109         }
00110         for (j = jbgn, ptr=column, outj=0; outj < lines; j+=jinc, outj++) {
00111             if ((j < index->voffset[k]) || (j > index->voffset[k] + index->vheight[k])) {
00112                 (*out)->data[0][outj][outk] = (PIXEL) 0.0;
00113             } else {
00114                 (*out)->data[0][outj][outk] = (PIXEL) *(ptr + (j - index->voffset[k]));
00115             }
00116         }
00117     }
00118 
00119     
00120     if (NAMES) {
00121         RANGE(*out);
00122         MESSAGE('I',"");
00123         MESSAGE('I',"MOSAIC_IMPORT");
00124         MESSAGE('I',"");
00125         sprintf(msg," Input file:                                 %s",name);
00126         MESSAGE('I',msg); 
00127         MESSAGE('I'," Input format:                               8-bit BIP in MOSAIC_IMAGE format");
00128         MESSAGE('I',msg);
00129         sprintf(msg," Number of lines, pixels:                    %d, %d",nlin,npix);
00130         MESSAGE('I',msg);
00131         sprintf(msg," Number of bits/pixel/band:                  %d",8);
00132         MESSAGE('I',msg);
00133         sprintf(msg," Subsampled from line, pixel:                %d, %d",jbgn+1,kbgn+1);
00134         MESSAGE('I',msg);
00135         sprintf(msg," Subsampled to line, pixel:                  %d, %d",jend+1,kend+1);
00136         MESSAGE('I',msg);
00137         sprintf(msg," Subsample increment: lines, pixels:         %d, %d",jinc,kinc);
00138         MESSAGE('I',msg);
00139         MESSAGE('I'," ...............");
00140     }
00141 
00142     /* close image file */
00143     the_end:
00144     if (fp) {
00145         if (fclose(fp)) {
00146             sprintf(msg," Can't close file %s.",name);
00147             MESSAGE('W',msg);
00148         }
00149     }
00150     if (column)  free(column);
00151     if (TIMES) TIMING(T_EXIT);
00152 }

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