/*---------------------------------------------------------------------------*/ /* Program: flip.c */ /* */ /* Purpose: This program flips an image along any axis. */ /* */ /* Author: John Gauch / Chunyen Liu */ /* */ /* Date: November 10, 1996 */ /* */ /* Note: Copyright (C) The University of Kansas, 1996 */ /*---------------------------------------------------------------------------*/ #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; FLOAT_TYPE *Data1; COLOR_TYPE *Data1c; FLOAT_TYPE **Data2; COLOR_TYPE **Data2c; FLOAT_TYPE ***Data3; COLOR_TYPE ***Data3c; int PixType, Xdim, Ydim, Zdim, DimCnt; FLOAT_TYPE **Temp2; COLOR_TYPE **Temp2c; /* Program variables */ int FlipX = FALSE; int FlipY = FALSE; int FlipZ = FALSE; int FlipD = FALSE; int i = 0, x, y, z; float temp; unsigned char tempR, tempG, tempB; /* Interpret program options */ printf("FLIP Program - KUIM Version 2.0\n\n"); while ((++i < argc) && (argv[i][0] == '-')) switch (argv[i][1]) { case 'x': FlipX = TRUE; break; case 'y': FlipY = TRUE; break; case 'z': FlipZ = TRUE; break; case 'd': FlipD = TRUE; break; default: Error("Invalid option encountered"); break; } /* Check number of file names */ if (argc - i != 2) { fprintf(stderr, "Usage: flip [options] infile outfile\n"); fprintf(stderr, " [-x] Flip image along X axis\n"); fprintf(stderr, " [-y] Flip image along Y axis\n"); fprintf(stderr, " [-z] Flip image along Z axis\n"); fprintf(stderr, " [-d] Flip image along X-Y diagonal\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); /* Create output image */ if ((FlipD == TRUE) && (DimCnt == 2)) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) Image2 = im_create(Name2, COLOR, Ydim, Xdim, Zdim); else Image2 = im_create(Name2, PixType, Ydim, Xdim, Zdim); } else { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) Image2 = im_create(Name2, COLOR, Xdim, Ydim, Zdim); else Image2 = im_create(Name2, PixType, Xdim, Ydim, Zdim); } /* Handle 1D images */ if (DimCnt == 1) { /* Read input image */ if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { Data1c = (COLOR_TYPE *) im_alloc1D(Image1, COLOR); im_read(Image1, COLOR, (char *) Data1c); } else { Data1 = (FLOAT_TYPE *) im_alloc1D(Image1, FLOAT); im_read(Image1, FLOAT, (char *) Data1); } /* Flip along X direction */ if (FlipX == TRUE) for (x = 0; x < (Xdim / 2); x++) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { tempR = Data1c[x].r; tempG = Data1c[x].g; tempB = Data1c[x].b; Data1c[x].r = Data1c[Xdim - 1 - x].r; Data1c[x].g = Data1c[Xdim - 1 - x].g; Data1c[x].b = Data1c[Xdim - 1 - x].b; Data1c[Xdim - 1 - x].r = tempR; Data1c[Xdim - 1 - x].g = tempG; Data1c[Xdim - 1 - x].b = tempB; } else { temp = Data1[x]; Data1[x] = Data1[Xdim - 1 - x]; Data1[Xdim - 1 - x] = temp; } } /* Write information to output image */ if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { im_write(Image2, COLOR, (char *) Data1c); im_free1D((char *) Data1c); } else { im_write(Image2, FLOAT, (char *) Data1); im_free1D((char *) Data1); } } /* Handle 2D images */ if (DimCnt == 2) { /* Read input image */ if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { Data2c = (COLOR_TYPE **) im_alloc2D(Image1, COLOR); im_read(Image1, COLOR, (char *) &(Data2c[0][0])); } else { Data2 = (FLOAT_TYPE **) im_alloc2D(Image1, FLOAT); im_read(Image1, FLOAT, (char *) &(Data2[0][0])); } /* Flip along X direction */ if (FlipX == TRUE) for (y = 0; y < Ydim; y++) for (x = 0; x < (Xdim / 2); x++) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { tempR = Data2c[y][x].r; tempG = Data2c[y][x].g; tempB = Data2c[y][x].b; Data2c[y][x].r = Data2c[y][Xdim - 1 - x].r; Data2c[y][x].g = Data2c[y][Xdim - 1 - x].g; Data2c[y][x].b = Data2c[y][Xdim - 1 - x].b; Data2c[y][Xdim - 1 - x].r = tempR; Data2c[y][Xdim - 1 - x].g = tempG; Data2c[y][Xdim - 1 - x].b = tempB; } else { temp = Data2[y][x]; Data2[y][x] = Data2[y][Xdim - 1 - x]; Data2[y][Xdim - 1 - x] = temp; } } /* Flip along Y direction */ if (FlipY == TRUE) for (y = 0; y < (Ydim / 2); y++) for (x = 0; x < Xdim; x++) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { tempR = Data2c[y][x].r; tempG = Data2c[y][x].g; tempB = Data2c[y][x].b; Data2c[y][x].r = Data2c[Ydim - 1 - y][x].r; Data2c[y][x].g = Data2c[Ydim - 1 - y][x].g; Data2c[y][x].b = Data2c[Ydim - 1 - y][x].b; Data2c[Ydim - 1 - y][x].r = tempR; Data2c[Ydim - 1 - y][x].g = tempG; Data2c[Ydim - 1 - y][x].b = tempB; } else { temp = Data2[y][x]; Data2[y][x] = Data2[Ydim - 1 - y][x]; Data2[Ydim - 1 - y][x] = temp; } } /* Flip along X-Y diagonal */ if (FlipD == TRUE) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) Temp2c = (COLOR_TYPE **) im_alloc2D(Image2, COLOR); else Temp2 = (FLOAT_TYPE **) im_alloc2D(Image2, FLOAT); for (y = 0; y < Ydim; y++) for (x = 0; x < Xdim; x++) if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { Temp2c[x][y].r = Data2c[y][x].r; Temp2c[x][y].g = Data2c[y][x].g; Temp2c[x][y].b = Data2c[y][x].b; } else Temp2[x][y] = Data2[y][x]; if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { im_write(Image2, COLOR, (char *) &(Temp2c[0][0])); im_free2D((char **) Temp2c); } else { im_write(Image2, FLOAT, (char *) &(Temp2[0][0])); im_free2D((char **) Temp2); } } /* Write information to output image */ if (FlipD == FALSE) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { im_write(Image2, COLOR, (char *) &(Data2c[0][0])); im_free2D((char **) Data2c); } else { im_write(Image2, FLOAT, (char *) &(Data2[0][0])); im_free2D((char **) Data2); } } } /* Handle 3D images */ if (DimCnt == 3) { /* Read input image */ if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { Data3c = (COLOR_TYPE ***) im_alloc3D(Image1, COLOR); im_read(Image1, COLOR, (char *) &(Data3c[0][0][0])); } else { Data3 = (FLOAT_TYPE ***) im_alloc3D(Image1, FLOAT); im_read(Image1, FLOAT, (char *) &(Data3[0][0][0])); } /* Flip along X direction */ if (FlipX == TRUE) for (z = 0; z < Zdim; z++) for (y = 0; y < Ydim; y++) for (x = 0; x < (Xdim / 2); x++) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { tempR = Data3c[z][y][x].r; tempG = Data3c[z][y][x].g; tempB = Data3c[z][y][x].b; Data3c[z][y][x].r = Data3c[z][y][Xdim - 1 - x].r; Data3c[z][y][x].g = Data3c[z][y][Xdim - 1 - x].g; Data3c[z][y][x].b = Data3c[z][y][Xdim - 1 - x].b; Data3c[z][y][Xdim - 1 - x].r = tempR; Data3c[z][y][Xdim - 1 - x].g = tempG; Data3c[z][y][Xdim - 1 - x].b = tempB; } else { temp = Data3[z][y][x]; Data3[z][y][x] = Data3[z][y][Xdim - 1 - x]; Data3[z][y][Xdim - 1 - x] = temp; } } /* Flip along Y direction */ if (FlipY == TRUE) for (z = 0; z < Zdim; z++) for (y = 0; y < (Ydim / 2); y++) for (x = 0; x < Xdim; x++) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { tempR = Data3c[z][y][x].r; tempG = Data3c[z][y][x].g; tempB = Data3c[z][y][x].b; Data3c[z][y][x].r = Data3c[z][Ydim - 1 - y][x].r; Data3c[z][y][x].g = Data3c[z][Ydim - 1 - y][x].g; Data3c[z][y][x].b = Data3c[z][Ydim - 1 - y][x].b; Data3c[z][Ydim - 1 - y][x].r = tempR; Data3c[z][Ydim - 1 - y][x].g = tempG; Data3c[z][Ydim - 1 - y][x].b = tempB; } else { temp = Data3[z][y][x]; Data3[z][y][x] = Data3[z][Ydim - 1 - y][x]; Data3[z][Ydim - 1 - y][x] = temp; } } /* Flip along Z direction */ if (FlipZ == TRUE) for (z = 0; z < (Zdim / 2); z++) for (y = 0; y < Ydim; y++) for (x = 0; x < Xdim; x++) { if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { tempR = Data3c[z][y][x].r; tempG = Data3c[z][y][x].g; tempB = Data3c[z][y][x].b; Data3c[z][y][x].r = Data3c[Zdim - 1 - z][y][x].r; Data3c[z][y][x].g = Data3c[Zdim - 1 - z][y][x].g; Data3c[z][y][x].b = Data3c[Zdim - 1 - z][y][x].b; Data3c[Zdim - 1 - z][y][x].r = tempR; Data3c[Zdim - 1 - z][y][x].g = tempG; Data3c[Zdim - 1 - z][y][x].b = tempB; } else { temp = Data3[z][y][x]; Data3[z][y][x] = Data3[Zdim - 1 - z][y][x]; Data3[Zdim - 1 - z][y][x] = temp; } } /* Write information to output image */ if ((PixType == COLOR) || (PixType == PSEUDO) || (PixType == JPEG_RGB)) { im_write(Image2, COLOR, (char *) &(Data3c[0][0][0])); im_free3D((char ***) Data3c); } else { im_write(Image2, FLOAT, (char *) &(Data3[0][0][0])); im_free3D((char ***) Data3); } } return (0); }