Initial import
[experiments/libjpeg8.git] / libjpeg8-mem-dest.c
1 /*
2  * libjpeg8-mem-dest - an example about reusing the memory buffer with libjpeg8
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 <jpeglib.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         struct jpeg_compress_struct cinfo;
49         struct jpeg_error_mgr jerr;
50         JSAMPROW row_pointer[1];
51         int row_stride;
52
53         unsigned char *out_buf;
54         unsigned long out_buf_size;
55
56         unsigned char *input_image;
57
58         cinfo.err = jpeg_std_error(&jerr);
59         jpeg_create_compress(&cinfo);
60
61         out_buf = NULL;
62         out_buf_size = 0;
63         jpeg_mem_dest(&cinfo, &out_buf, &out_buf_size);
64
65         cinfo.image_width = WIDTH;
66         cinfo.image_height = HEIGHT;
67         cinfo.input_components = 3;
68         cinfo.in_color_space = JCS_RGB;
69
70         jpeg_set_defaults(&cinfo);
71         jpeg_set_quality(&cinfo, 80, TRUE);
72
73         row_stride = WIDTH * 3;
74
75         srand(time(NULL));
76
77         i = 0;
78         while(i++ < 10) {
79                 input_image = new_image();
80
81                 jpeg_start_compress(&cinfo, TRUE);
82                 while(cinfo.next_scanline < cinfo.image_height) {
83                         row_pointer[0] = &input_image[cinfo.next_scanline * row_stride];
84                         (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
85                 }
86                 jpeg_finish_compress(&cinfo);
87
88                 free(input_image);
89
90                 /* Is out_buf_size the number of bytes of the jpeg image, or the
91                  * size of the allocated buffer? */
92                 fprintf(stderr, "out_buf: %p out_buf_size: %ld\n", out_buf, out_buf_size);
93                 {
94                         char filename[256] = { 0 };
95                         FILE *out_file;
96
97                         snprintf(filename, sizeof(filename), "out%03d.jpg", i);
98                         out_file = fopen(filename, "wb");
99                         fwrite(out_buf, out_buf_size, 1, out_file);
100                         fclose(out_file);
101                 }
102
103 #if 1
104                 /*
105                  * Rewind the buffer, taken from:
106                  * http://sourceforge.net/p/libjpeg-turbo/discussion/1086868/thread/b797ffaf/
107                  */
108                 cinfo.dest->free_in_buffer += cinfo.dest->next_output_byte - out_buf;
109                 cinfo.dest->next_output_byte = out_buf;
110 #else
111                 /* The code above avoids freeing and allocating a NEW buffer
112                  * Isn't there a more straightforward way to recycle the old
113                  * buffer after we consumed its content?
114                  */
115                 free(out_buf);
116                 out_buf = NULL;
117                 out_buf_size = 0;
118                 jpeg_mem_dest(&cinfo, &out_buf, &out_buf_size);
119 #endif
120         }
121
122         jpeg_destroy_compress(&cinfo);
123
124         free(out_buf);
125
126         return 0;
127 }