Mercurial > hg > minimon
annotate jpg.c @ 4:5ff6a67e1421
missing
author | Peter Meerwald <pmeerw@pmeerw.net> |
---|---|
date | Mon, 09 May 2011 23:32:51 +0200 |
parents | e8957085fe8a |
children | 3819ecaf0f14 |
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 |
3 | 60 static void 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 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
95 |
3 | 96 |
97 jpg_buf_t build_jpg_from_fb(int fb, int width, int height) { | |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
98 int scanline; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
99 struct jpeg_compress_struct cinfo; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
100 struct jpeg_error_mgr jerr; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
101 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
102 cinfo.err = jpeg_std_error(&jerr); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
103 jpeg_create_compress(&cinfo); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
104 |
3 | 105 jpg_buf_t jpg_buf; |
106 jpg_buf.size = 1 << 20; | |
107 jpg_buf.ptr = NULL; | |
108 | |
109 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
|
110 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
111 cinfo.image_width = width; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
112 cinfo.image_height = height; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
113 cinfo.input_components = 3; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
114 cinfo.in_color_space = JCS_RGB; |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
115 jpeg_set_defaults(&cinfo); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
116 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
117 jpeg_start_compress(&cinfo, TRUE); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
118 |
3 | 119 JSAMPLE *buf = malloc(width * 4); |
120 JSAMPROW row_ptr[1]; | |
121 row_ptr[0] = buf; | |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
122 for (scanline = 0; scanline < height; scanline++) { |
3 | 123 read(fb, buf, width*4); |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
124 jpeg_write_scanlines(&cinfo, row_ptr, 1); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
125 } |
3 | 126 free(buf); |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
127 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
128 jpeg_finish_compress(&cinfo); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
129 |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
130 jpeg_destroy_compress(&cinfo); |
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
131 |
3 | 132 return jpg_buf; |
2
bac8ed8d6eb9
add jpeg coding and test stuff
Peter Meerwald <pmeerw@pmeerw.net>
parents:
diff
changeset
|
133 } |