/*--------------------------------------------------------------------------*/ /* Program: unfold.c */ /* */ /* Purpose: This programs transforms a 3D KUIM format image into a 2D */ /* image for display purposes. The -x -y and -b switches */ /* control the final shape of the output image. */ /* */ /* Author: John Gauch / Chunyen Liu */ /* */ /* Date: April 10, 1996 */ /* */ /* Note: Copyright (C) The University of Kansas, 1996 */ /*--------------------------------------------------------------------------*/ #include #define XDIR 1 #define YDIR 2 #define BOTH 3 #define USER 4 /*---------------------------------------------------------------------------*/ /* 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; BYTE_TYPE ***Data1; BYTE_TYPE **Data2; unsigned char Red[nCMAP]; unsigned char Green[nCMAP]; unsigned char Blue[nCMAP]; int PixType, Xdim, Ydim, Zdim, DimCnt; int NewXdim, NewYdim; /* Program variables */ int Debug = FALSE; int Unfold = BOTH; int Lines = TRUE; int Rows, Cols; int MaxVal = 0; int i = 0, x, y, z, r, c; /* Interpret program options */ printf("UNFOLD Program - KUIM Version 2.0\n\n"); while ((++i < argc) && (argv[i][0] == '-')) switch (argv[i][1]) { case 's': Unfold = USER; if (sscanf(argv[++i], "%d", &Cols) == 0) Error("Could not get Cols argument"); if (sscanf(argv[++i], "%d", &Rows) == 0) Error("Could not get Rows argument"); break; case 'x': Unfold = XDIR; break; case 'y': Unfold = YDIR; break; case 'b': Unfold = BOTH; break; case 'l': Lines = FALSE; break; case 'd': Debug = TRUE; break; default: Error("Invalid option encountered"); break; } /* Check number of file names */ if (argc - i != 2) { fprintf(stderr, "Usage: unfold [options] infile outfile\n"); fprintf(stderr, " [-d] Print debugging information\n"); fprintf(stderr, " [-l] No lines separating images\n"); fprintf(stderr, " [-s # #] Size in X and Y directions\n"); fprintf(stderr, " [-x] Unfold in X direction\n"); fprintf(stderr, " [-y] Unfold in Y direction\n"); fprintf(stderr, " [-b] Unfold in BOTH directions\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 (DimCnt != 3) Error("Expecting 3D image to unfold"); Data1 = (BYTE_TYPE ***) im_alloc3D(Image1, BYTE); /* Read input image */ if ((PixType == PSEUDO) || (PixType == COLOR) || (PixType == JPEG_RGB)) im_read(Image1, PSEUDO, (char *) &(Data1[0][0][0])); else im_read(Image1, BYTE, (char *) &(Data1[0][0][0])); im_get_cmap(Image1, Red, Green, Blue); /* Calculate output dimensions */ if (Unfold == XDIR) { Cols = Zdim; Rows = 1; } if (Unfold == YDIR) { Cols = 1; Rows = Zdim; } if (Unfold == BOTH) { Cols = (int) ceil(sqrt((double) Zdim)); Rows = (int) ceil((double) Zdim / (double) Cols); } NewXdim = Xdim * Cols; NewYdim = Ydim * Rows; /* Create the output image */ if ((PixType == PSEUDO) || (PixType == COLOR) || (PixType == JPEG_RGB)) Image2 = im_create(Name2, PSEUDO, NewXdim, NewYdim, 1); else Image2 = im_create(Name2, PixType, NewXdim, NewYdim, 1); im_put_cmap(Image2, Red, Green, Blue); Data2 = (BYTE_TYPE **) im_alloc2D(Image2, BYTE); /* Preprocess input image to find maximum value */ for (z = 0; z < Zdim; z++) for (y = 0; y < Ydim; y++) for (x = 0; x < Xdim; x++) if (Data1[z][y][x] > MaxVal) MaxVal = Data1[z][y][x]; /* Preprocess input image to add border */ if (Lines == TRUE) { for (z = 0; z < Zdim; z++) for (y = 0; y < Ydim; y++) Data1[z][y][Xdim - 1] = MaxVal; for (z = 0; z < Zdim; z++) for (x = 0; x < Xdim; x++) Data1[z][Ydim - 1][x] = MaxVal; } /* Unfold input image along X direction */ if (Unfold == XDIR) for (y = 0; y < NewYdim; y++) for (x = 0; x < NewXdim; x++) Data2[y][x] = Data1[x / Xdim][y][x % Xdim]; /* Unfold input image along Y direction */ if (Unfold == YDIR) for (y = 0; y < NewYdim; y++) for (x = 0; x < NewXdim; x++) Data2[y][x] = Data1[y / Ydim][y % Ydim][x]; /* Unfold input image along both directions */ if ((Unfold == BOTH) || (Unfold == USER)) for (r = 0; r < Rows; r++) for (c = 0; c < Cols; c++) { z = r * Cols + c; if (z < Zdim) for (y = 0; y < Ydim; y++) for (x = 0; x < Xdim; x++) Data2[y + r * Ydim][x + c * Xdim] = Data1[z][y][x]; } /* Postprocess output image to add border */ if (Lines == TRUE) { for (y = 0; y < NewYdim; y++) Data2[y][0] = Data2[y][NewXdim - 1] = MaxVal; for (x = 0; x < NewXdim; x++) Data2[0][x] = Data2[NewYdim - 1][x] = MaxVal; } /* Write information to output image */ if ((PixType == PSEUDO) || (PixType == COLOR) || (PixType == JPEG_RGB)) im_write(Image2, PSEUDO, (char *) &(Data2[0][0])); else im_write(Image2, BYTE, (char *) &(Data2[0][0])); im_free3D((char ***) Data1); im_free2D((char **) Data2); return (0); }