main.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 
5 #include <imagezero/libiz.h>
7 #include <imagezero/file.h>
8 
23 static void decodeIZ(const char *infilename, const char *outfilename)
24 {
26  IZ::InputFile infile(infilename);
27  if (!infile.isReadable()) {
28  perror("Cannot open input file");
29  exit(EXIT_FAILURE);
30  }
31  IZ::OutputFile outfile(outfilename);
32  if (!outfile.isWritable()) {
33  perror("Cannot open output file");
34  exit(EXIT_FAILURE);
35  }
36  const unsigned char *src = infile.data();
38  IZ::decodeImageSize(pi, src);
39  pi.setComponents(3);
40  const unsigned int dataSize = pi.width() * pi.height() * pi.components();
41  unsigned char *dest = outfile.prepareData(dataSize + 33);
42  if (!dest) {
43  perror("Cannot write output file");
44  exit(EXIT_FAILURE);
45  }
46  pi.writeHeader(dest);
47  IZ::decodeImage(pi, src);
48  outfile.commitData(dest, pi.data() - dest + dataSize);
49 }
50 
51 static void encodeIZ(const char *infilename, const char *outfilename)
52 {
54  IZ::InputFile infile(infilename);
55  if (!infile.isReadable()) {
56  perror("Cannot open input file");
57  exit(EXIT_FAILURE);
58  }
59  IZ::OutputFile outfile(outfilename);
60  if (!outfile.isWritable()) {
61  perror("Cannot open output file");
62  exit(EXIT_FAILURE);
63  }
64  if (!pi.readHeader(infile.data())) {
65  fprintf(stderr, "Cannot handle input file, only 24 bit PPM files supported.\n");
66  exit(EXIT_FAILURE);
67  }
68  if (pi.components() != 3) {
69  fprintf(stderr, "Cannot handle 8-bit (grayscale) PGM files, only 24 bit PPM files supported.\n");
70  exit(EXIT_FAILURE);
71  }
72  if (pi.width() > 16384 || pi.height() > 16384) {
73  fprintf(stderr, "Cannot handle image size %d x %d, limit is 16384 x 16384.\n", pi.width(), pi.height());
74  exit(EXIT_FAILURE);
75  }
76  unsigned char *dest = outfile.prepareData(pi.height() * pi.width() * 4 + 33);
77  if (!dest) {
78  perror("Cannot write output file");
79  exit(EXIT_FAILURE);
80  }
82  unsigned char *destEnd = IZ::encodeImage(pi, dest);
83  outfile.commitData(dest, destEnd - dest);
84 }
85 
86 int main(int argc, char *argv[])
87 {
88  if (argc == 2 && (!strncmp(argv[1], "-h", 2) || !strncmp(argv[1], "--h", 3))) {
89  printf("Usage: %s [c|d] INPUTFILE OUTPUTFILE\n"
90  "\n"
91  " --help show this help\n"
92  " --version show version and author information\n"
93  "\n"
94  "%s is a high-performance lossless RGB color image codec\n"
95  "\n"
96  "To compress a 24 bit binary PPM image to an IZ file, use\n"
97  " %s c input.ppm output.iz\n"
98  "To decompress an IZ file to a 24 bit binary PPM image, use\n"
99  " %s d input.iz output.ppm\n", argv[0], argv[0], argv[0], argv[0]);
100  exit(EXIT_SUCCESS);
101  }
102  if (argc == 2 && (!strncmp(argv[1], "-v", 2) || !strncmp(argv[1], "--v", 3))) {
103  printf("%s 0.1\n"
104  "High-performance lossless RGB color image codec\n"
105  "Copyright (c) 2012, Christoph Feck <christoph@maxiom.de>\n", argv[0]);
106  exit(EXIT_SUCCESS);
107  }
108  if (argc != 4) {
109  fprintf(stderr, "Usage: %s [c|d] INPUTFILE OUTPUTFILE\n"
110  "Use \"%s --help\" for more information\n", argv[0], argv[0]);
111  exit(EXIT_FAILURE);
112  }
113  if (!strncmp(argv[1], "c", 1)) {
114  encodeIZ(argv[2], argv[3]);
115  } else if (!strncmp(argv[1], "d", 1)) {
116  decodeIZ(argv[2], argv[3]);
117  } else {
118  fprintf(stderr, "Usage: %s [c|d] INPUTFILE OUTPUTFILE\n"
119  "Use \"%s --help\" for more information\n", argv[0], argv[0]);
120  exit(EXIT_FAILURE);
121  }
122  return 0;
123 }
Sample * data() const
Definition: image.h:18
void setComponents(int components)
Definition: portableimage.h:19
static void decodeIZ(const char *infilename, const char *outfilename)
Definition: main.cpp:23
int width() const
Definition: image.h:15
int height() const
Definition: image.h:16
void commitData(unsigned char *data, size_t size)
bool isWritable() const
unsigned char * writeHeader(unsigned char *data)
bool readHeader(const unsigned char *data)
unsigned char * encodeImage(const Image<> &im, unsigned char *dest)
Definition: libiz.cpp:7
const unsigned char * data() const
int components() const
Definition: portableimage.h:16
const unsigned char * decodeImage(Image<> &im, const unsigned char *src)
Definition: libiz.cpp:16
void initEncodeTable()
Definition: table.cpp:73
void decodeImageSize(Image<> &im, const unsigned char *src)
Definition: libiz.cpp:25
int main(int argc, char *argv[])
Definition: main.cpp:86
bool isReadable() const
void initDecodeTable()
Definition: table.cpp:80
unsigned char * prepareData(size_t maxSize)
static void encodeIZ(const char *infilename, const char *outfilename)
Definition: main.cpp:51


imagezero
Author(s):
autogenerated on Mon Jun 10 2019 13:37:22