Mercurial > hg > minimon
annotate jpg.c @ 9:c7af696b6221
fix
author | Peter Meerwald <pmeerw@pmeerw.net> |
---|---|
date | Sat, 14 May 2011 23:08:11 +0200 |
parents | 66c77f9ba9b9 |
children | 078dc69945ad |
rev | line source |
---|---|
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
1 #include <stdlib.h> |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
2 #include <stdio.h> |
3 | 3 #include <unistd.h> |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
4 #include <string.h> |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
5 #include "jpeglib.h" |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
6 #include "jerror.h" |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
7 |
3 | 8 #include "common.h" |
9 | |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
10 typedef struct { |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
11 struct jpeg_destination_mgr pub; /* public fields */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
12 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
13 unsigned char ** outbuffer; /* target buffer */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
14 unsigned long * outsize; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
15 unsigned char * newbuffer; /* newly allocated buffer */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
16 JOCTET * buffer; /* start of buffer */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
17 size_t bufsize; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
18 } mem_dest_mgr; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
19 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
20 typedef mem_dest_mgr * mem_dest_mgr_ptr; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
21 |
3 | 22 static void init_mem_destination (j_compress_ptr cinfo) { |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
23 } |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
24 |
3 | 25 static boolean empty_mem_output_buffer(j_compress_ptr cinfo) { |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
26 size_t nextsize; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
27 JOCTET * nextbuffer; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
28 mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
29 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
30 /* Try to allocate new buffer with double size */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
31 nextsize = dest->bufsize * 2; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
32 nextbuffer = malloc(nextsize); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
33 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
34 if (nextbuffer == NULL) |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
35 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
36 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
37 memcpy(nextbuffer, dest->buffer, dest->bufsize); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
38 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
39 if (dest->newbuffer != NULL) |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
40 free(dest->newbuffer); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
41 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
42 dest->newbuffer = nextbuffer; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
43 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
44 dest->pub.next_output_byte = nextbuffer + dest->bufsize; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
45 dest->pub.free_in_buffer = dest->bufsize; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
46 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
47 dest->buffer = nextbuffer; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
48 dest->bufsize = nextsize; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
49 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
50 return TRUE; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
51 } |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
52 |
3 | 53 static void term_mem_destination(j_compress_ptr cinfo) { |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
54 mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
55 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
56 *dest->outbuffer = dest->buffer; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
57 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
58 } |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
59 |
6
3819ecaf0f14
avoiding clash with latest libjpeg
Peter Meerwald <pmeerw@pmeerw.net>
parents:
3
diff
changeset
|
60 static void my_jpeg_mem_dest(j_compress_ptr cinfo, unsigned char ** outbuffer, unsigned long * outsize) { |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
61 mem_dest_mgr_ptr dest; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
62 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
63 if (outbuffer == NULL || outsize == NULL) /* sanity check */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
64 ERREXIT(cinfo, JERR_BUFFER_SIZE); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
65 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
66 /* The destination object is made permanent so that multiple JPEG images |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
67 * can be written to the same buffer without re-executing jpeg_mem_dest. |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
68 */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
69 if (cinfo->dest == NULL) { /* first time for this JPEG object? */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
70 cinfo->dest = (struct jpeg_destination_mgr *) |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
71 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
72 sizeof(mem_dest_mgr)); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
73 } |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
74 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
75 dest = (mem_dest_mgr_ptr) cinfo->dest; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
76 dest->pub.init_destination = init_mem_destination; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
77 dest->pub.empty_output_buffer = empty_mem_output_buffer; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
78 dest->pub.term_destination = term_mem_destination; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
79 dest->outbuffer = outbuffer; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
80 dest->outsize = outsize; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
81 dest->newbuffer = NULL; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
82 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
83 if (*outbuffer == NULL || *outsize == 0) { |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
84 /* Allocate initial buffer */ |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
85 dest->newbuffer = *outbuffer = malloc(65536); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
86 if (dest->newbuffer == NULL) |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
87 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
88 *outsize = 65536; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
89 } |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
90 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
91 dest->pub.next_output_byte = dest->buffer = *outbuffer; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
92 dest->pub.free_in_buffer = dest->bufsize = *outsize; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
93 } |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
94 |
8 | 95 static void convert_rgba_to_rgb(unsigned char *buf, unsigned char *fb_mem, int width) { |
96 int i; | |
97 for (i = 0; i < width; i++) { | |
98 *buf++ = *fb_mem++; | |
99 *buf++ = *fb_mem++; | |
100 *buf++ = *fb_mem++; | |
101 fb_mem++; | |
102 } | |
103 } | |
3 | 104 |
8 | 105 jpg_buf_t build_jpg_from_fb(unsigned char *fb_mem, int width, int height, int bits_per_pixel) { |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
106 int scanline; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
107 struct jpeg_compress_struct cinfo; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
108 struct jpeg_error_mgr jerr; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
109 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
110 cinfo.err = jpeg_std_error(&jerr); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
111 jpeg_create_compress(&cinfo); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
112 |
3 | 113 jpg_buf_t jpg_buf; |
114 jpg_buf.size = 1 << 20; | |
115 jpg_buf.ptr = NULL; | |
116 | |
6
3819ecaf0f14
avoiding clash with latest libjpeg
Peter Meerwald <pmeerw@pmeerw.net>
parents:
3
diff
changeset
|
117 my_jpeg_mem_dest(&cinfo, &jpg_buf.ptr, &jpg_buf.size); |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
118 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
119 cinfo.image_width = width; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
120 cinfo.image_height = height; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
121 cinfo.input_components = 3; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
122 cinfo.in_color_space = JCS_RGB; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
123 jpeg_set_defaults(&cinfo); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
124 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
125 jpeg_start_compress(&cinfo, TRUE); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
126 |
8 | 127 JSAMPLE *buf = (JSAMPLE *) malloc(width * 3); |
128 JSAMPROW row_ptr[1] = {buf}; | |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
129 for (scanline = 0; scanline < height; scanline++) { |
8 | 130 convert_rgba_to_rgb(buf, &fb_mem[scanline * width * 4], width); |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
131 jpeg_write_scanlines(&cinfo, row_ptr, 1); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
132 } |
3 | 133 free(buf); |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
134 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
135 jpeg_finish_compress(&cinfo); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
136 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
137 jpeg_destroy_compress(&cinfo); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
138 |
3 | 139 return jpg_buf; |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
140 } |