2 * libturbojpeg1-mem-dest - an example about reusing the memory buffer with libturbojpeg1
4 * Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <turbojpeg.h>
28 static unsigned char *new_image(void)
33 image = malloc(WIDTH * HEIGHT * 3 * sizeof(*image));
39 for (i = 0; i < WIDTH * HEIGHT * 3; i++)
40 image[i] = rand() % 0xff;
48 tjhandle jpeg_compressor;
49 unsigned char *out_buf;
50 unsigned long out_buf_size;
51 unsigned char *input_image;
54 jpeg_compressor = tjInitCompress();
55 if (jpeg_compressor == NULL) {
56 fprintf(stderr, "tjInitCompress failed: %s\n", tjGetErrorStr());
60 out_buf_size = tjBufSize(WIDTH, HEIGHT, TJSAMP_422);
61 ret = (int) out_buf_size;
63 fprintf(stderr, "tjBufSize failed: %s\n", tjGetErrorStr());
64 goto out_destroy_compressor;
66 out_buf = malloc(out_buf_size);
70 input_image = new_image();
72 ret = tjCompress2(jpeg_compressor, input_image, WIDTH, 0, HEIGHT,
73 TJPF_RGB, &out_buf, &out_buf_size, TJSAMP_422,
74 80, TJFLAG_NOREALLOC | TJFLAG_FASTDCT);
77 fprintf(stderr, "tjCompress2 failed: %s\n", tjGetErrorStr());
84 /* Is out_buf_size the number of bytes of the jpeg image, or the
85 * size of the allocated buffer? */
86 fprintf(stderr, "out_buf: %p out_buf_size: %ld\n", out_buf, out_buf_size);
88 char filename[256] = { 0 };
91 snprintf(filename, sizeof(filename), "out%03d.jpg", i);
92 out_file = fopen(filename, "wb");
93 fwrite(out_buf, out_buf_size, 1, out_file);
102 out_destroy_compressor:
103 ret = tjDestroy(jpeg_compressor);
105 fprintf(stderr, "tjDestroy failed: %s\n", tjGetErrorStr());