/*---------------------------------------------------------------------------*/ /* Program: type.c */ /* */ /* Purpose: This program converts KUIM images from one data type to */ /* another. Automatic type conversion in im_read and im_write */ /* does most of the work. */ /* */ /* Author: John Gauch */ /* */ /* Date: April 1, 1994 */ /* */ /* Note: Copyright (C) The University of Kansas, 1994 */ /*---------------------------------------------------------------------------*/ #include /*---------------------------------------------------------------------------*/ /* Purpose: This is the main program. */ /*---------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { /* Image variables */ char Name1[50]; char Name2[50]; IM_TYPE *Image1; IM_TYPE *Image2; char *Data1; float *Data2; int PixType, Xdim, Ydim, Zdim, DimCnt; unsigned char Red[nCMAP], Green[nCMAP], Blue[nCMAP]; /* Program variables */ int Debug = FALSE; int InType = SHORT; int OutType = SHORT; int Cnt, Max, Min; float Scale; int i = 0; /* Interpret program options */ printf("TYPE Program - KUIM Version 1.0\n\n"); while ((++i < argc) && (argv[i][0] == '-')) switch (argv[i][1]) { case 'b': InType = OutType = BYTE; break; case 's': InType = OutType = SHORT; break; case 'i': InType = OutType = INT; break; case 'f': InType = OutType = FLOAT; break; case 'd': InType = OutType = DOUBLE; break; case 'x': InType = OutType = COMPLEX; break; case 'c': InType = OutType = COLOR; break; case 'p': InType = OutType = PSEUDO; break; case 'j': InType = FLOAT; OutType = JPEG_GRAY; break; default: Error("Invalid option encountered"); break; } /* Check number of file names */ if (argc - i != 2) { fprintf(stderr, "Usage: type [options] infile outfile\n"); fprintf(stderr, " [-b] Convert to BYTE type\n"); fprintf(stderr, " [-s] Convert to SHORT type\n"); fprintf(stderr, " [-i] Convert to INT type\n"); fprintf(stderr, " [-f] Convert to FLOAT type\n"); fprintf(stderr, " [-d] Convert to DOUBLE type\n"); fprintf(stderr, " [-x] Convert to COMPLEX type\n"); fprintf(stderr, " [-c] Convert to COLOR type\n"); fprintf(stderr, " [-p] Convert to PSEUDO type\n"); fprintf(stderr, " [-j] Convert to JPEG\n"); exit(1); } /* Get image file names from argument list */ if (sscanf(argv[i++], "%s", Name1) == 0) Error("Could not get input file name"); if (sscanf(argv[i++], "%s", Name2) == 0) Error("Could not get output file name"); /* Open input image */ Image1 = im_open(Name1, &PixType, &Xdim, &Ydim, &Zdim, &DimCnt); if (PixType == OutType) Error("Images are of the same type"); /* Handle color JPEG output */ if ((OutType == JPEG_GRAY) && ((PixType == JPEG_RGB) || (PixType == PSEUDO) || (PixType == COLOR))) { InType = PSEUDO; OutType = JPEG_RGB; } /* Read (and convert) image data */ Data1 = (char *) im_alloc1D(Image1, InType); im_read(Image1, InType, (char *) Data1); im_get_cmap(Image1, Red, Green, Blue); /* Handle grey JPEG output */ if (OutType == JPEG_GRAY) { Data2 = (float *) Data1; Cnt = Xdim * Ydim * Zdim; /* Find max/min */ Max = Min = Data2[0]; for (i = 0; i < Cnt; i++) { if (Max < Data2[i]) Max = Data2[i]; if (Min > Data2[i]) Min = Data2[i]; } /* Get scale factor */ if (Max != Min) Scale = 255.0 / (Max - Min); else Scale = 1.0; /* Scale the data */ for (i = 0; i < Cnt; i++) Data2[i] = (Data2[i] - Min) * Scale; } /* Write output image */ Image2 = im_create(Name2, OutType, Xdim, Ydim, Zdim); im_put_cmap(Image2, Red, Green, Blue); im_write(Image2, InType, (char *) Data1); im_free1D((char *) Data1); return (0); }