00001 #include <cstdio> 00002 #include <cstdlib> 00003 #include <cstring> 00004 00005 #include <imagezero/libiz.h> 00006 #include <imagezero/portableimage.h> 00007 #include <imagezero/file.h> 00008 00023 static void decodeIZ(const char *infilename, const char *outfilename) 00024 { 00025 IZ::PortableImage pi; 00026 IZ::InputFile infile(infilename); 00027 if (!infile.isReadable()) { 00028 perror("Cannot open input file"); 00029 exit(EXIT_FAILURE); 00030 } 00031 IZ::OutputFile outfile(outfilename); 00032 if (!outfile.isWritable()) { 00033 perror("Cannot open output file"); 00034 exit(EXIT_FAILURE); 00035 } 00036 const unsigned char *src = infile.data(); 00037 IZ::initDecodeTable(); 00038 IZ::decodeImageSize(pi, src); 00039 pi.setComponents(3); 00040 const unsigned int dataSize = pi.width() * pi.height() * pi.components(); 00041 unsigned char *dest = outfile.prepareData(dataSize + 33); 00042 if (!dest) { 00043 perror("Cannot write output file"); 00044 exit(EXIT_FAILURE); 00045 } 00046 pi.writeHeader(dest); 00047 IZ::decodeImage(pi, src); 00048 outfile.commitData(dest, pi.data() - dest + dataSize); 00049 } 00050 00051 static void encodeIZ(const char *infilename, const char *outfilename) 00052 { 00053 IZ::PortableImage pi; 00054 IZ::InputFile infile(infilename); 00055 if (!infile.isReadable()) { 00056 perror("Cannot open input file"); 00057 exit(EXIT_FAILURE); 00058 } 00059 IZ::OutputFile outfile(outfilename); 00060 if (!outfile.isWritable()) { 00061 perror("Cannot open output file"); 00062 exit(EXIT_FAILURE); 00063 } 00064 if (!pi.readHeader(infile.data())) { 00065 fprintf(stderr, "Cannot handle input file, only 24 bit PPM files supported.\n"); 00066 exit(EXIT_FAILURE); 00067 } 00068 if (pi.components() != 3) { 00069 fprintf(stderr, "Cannot handle 8-bit (grayscale) PGM files, only 24 bit PPM files supported.\n"); 00070 exit(EXIT_FAILURE); 00071 } 00072 if (pi.width() > 16384 || pi.height() > 16384) { 00073 fprintf(stderr, "Cannot handle image size %d x %d, limit is 16384 x 16384.\n", pi.width(), pi.height()); 00074 exit(EXIT_FAILURE); 00075 } 00076 unsigned char *dest = outfile.prepareData(pi.height() * pi.width() * 4 + 33); 00077 if (!dest) { 00078 perror("Cannot write output file"); 00079 exit(EXIT_FAILURE); 00080 } 00081 IZ::initEncodeTable(); 00082 unsigned char *destEnd = IZ::encodeImage(pi, dest); 00083 outfile.commitData(dest, destEnd - dest); 00084 } 00085 00086 int main(int argc, char *argv[]) 00087 { 00088 if (argc == 2 && (!strncmp(argv[1], "-h", 2) || !strncmp(argv[1], "--h", 3))) { 00089 printf("Usage: %s [c|d] INPUTFILE OUTPUTFILE\n" 00090 "\n" 00091 " --help show this help\n" 00092 " --version show version and author information\n" 00093 "\n" 00094 "%s is a high-performance lossless RGB color image codec\n" 00095 "\n" 00096 "To compress a 24 bit binary PPM image to an IZ file, use\n" 00097 " %s c input.ppm output.iz\n" 00098 "To decompress an IZ file to a 24 bit binary PPM image, use\n" 00099 " %s d input.iz output.ppm\n", argv[0], argv[0], argv[0], argv[0]); 00100 exit(EXIT_SUCCESS); 00101 } 00102 if (argc == 2 && (!strncmp(argv[1], "-v", 2) || !strncmp(argv[1], "--v", 3))) { 00103 printf("%s 0.1\n" 00104 "High-performance lossless RGB color image codec\n" 00105 "Copyright (c) 2012, Christoph Feck <christoph@maxiom.de>\n", argv[0]); 00106 exit(EXIT_SUCCESS); 00107 } 00108 if (argc != 4) { 00109 fprintf(stderr, "Usage: %s [c|d] INPUTFILE OUTPUTFILE\n" 00110 "Use \"%s --help\" for more information\n", argv[0], argv[0]); 00111 exit(EXIT_FAILURE); 00112 } 00113 if (!strncmp(argv[1], "c", 1)) { 00114 encodeIZ(argv[2], argv[3]); 00115 } else if (!strncmp(argv[1], "d", 1)) { 00116 decodeIZ(argv[2], argv[3]); 00117 } else { 00118 fprintf(stderr, "Usage: %s [c|d] INPUTFILE OUTPUTFILE\n" 00119 "Use \"%s --help\" for more information\n", argv[0], argv[0]); 00120 exit(EXIT_FAILURE); 00121 } 00122 return 0; 00123 }