Initial import
[experiments/libturbojpeg1.git] / libturbojpeg1-mem-dest.c
1 /*
2  * libturbojpeg1-mem-dest - an example about reusing the memory buffer with libturbojpeg1
3  *
4  * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <turbojpeg.h>
24
25 #define WIDTH 800
26 #define HEIGHT 480
27
28 static unsigned char *new_image(void)
29 {
30         unsigned char *image;
31         int i;
32
33         image = malloc(WIDTH * HEIGHT * 3 * sizeof(*image));
34         if (image == NULL) {
35                 perror("malloc");
36                 exit(EXIT_FAILURE);
37         }
38
39         for (i = 0; i < WIDTH * HEIGHT * 3; i++)
40                 image[i] = rand() % 0xff;
41
42         return image;
43 }
44
45 int main(void)
46 {
47         int i;
48         tjhandle jpeg_compressor;
49         unsigned char *out_buf;
50         unsigned long out_buf_size;
51         unsigned char *input_image;
52         int ret;
53
54         jpeg_compressor = tjInitCompress();
55         if (jpeg_compressor == NULL) {
56                 fprintf(stderr, "tjInitCompress failed: %s\n", tjGetErrorStr());
57                 return -1;
58         }
59
60         out_buf_size = tjBufSize(WIDTH, HEIGHT, TJSAMP_422);
61         ret = (int) out_buf_size;
62         if (ret < 0) {
63                 fprintf(stderr, "tjBufSize failed: %s\n", tjGetErrorStr());
64                 goto out_destroy_compressor;
65         }
66         out_buf = malloc(out_buf_size);
67
68         i = 0;
69         while(i++ < 10) {
70                 input_image = new_image();
71
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);
75                 if (ret < 0) {
76                         tjGetErrorStr();
77                         fprintf(stderr, "tjCompress2 failed: %s\n", tjGetErrorStr());
78                         free(input_image);
79                         goto out_free_buffer;
80                 }
81
82                 free(input_image);
83
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);
87                 {
88                         char filename[256] = { 0 };
89                         FILE *out_file;
90
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);
94                         fclose(out_file);
95                 }
96         }
97
98         ret = 0;
99
100 out_free_buffer:
101         free(out_buf);
102 out_destroy_compressor:
103         ret = tjDestroy(jpeg_compressor);
104         if (ret < 0)
105                 fprintf(stderr, "tjDestroy failed: %s\n", tjGetErrorStr());
106
107         return ret;
108 }