Mercurial > hg > minimon
comparison jpg.c @ 3:e8957085fe8a
working
author | Peter Meerwald <pmeerw@pmeerw.net> |
---|---|
date | Sun, 08 May 2011 23:08:45 +0200 |
parents | bac8ed8d6eb9 |
children | 3819ecaf0f14 |
comparison
equal
deleted
inserted
replaced
2:bac8ed8d6eb9 | 3:e8957085fe8a |
---|---|
1 #include <stdlib.h> | 1 #include <stdlib.h> |
2 #include <stdio.h> | 2 #include <stdio.h> |
3 #include <unistd.h> | |
3 #include <string.h> | 4 #include <string.h> |
4 #include "jpeglib.h" | 5 #include "jpeglib.h" |
5 #include "jerror.h" | 6 #include "jerror.h" |
7 | |
8 #include "common.h" | |
6 | 9 |
7 typedef struct { | 10 typedef struct { |
8 struct jpeg_destination_mgr pub; /* public fields */ | 11 struct jpeg_destination_mgr pub; /* public fields */ |
9 | 12 |
10 unsigned char ** outbuffer; /* target buffer */ | 13 unsigned char ** outbuffer; /* target buffer */ |
14 size_t bufsize; | 17 size_t bufsize; |
15 } mem_dest_mgr; | 18 } mem_dest_mgr; |
16 | 19 |
17 typedef mem_dest_mgr * mem_dest_mgr_ptr; | 20 typedef mem_dest_mgr * mem_dest_mgr_ptr; |
18 | 21 |
19 void init_mem_destination (j_compress_ptr cinfo) { | 22 static void init_mem_destination (j_compress_ptr cinfo) { |
20 } | 23 } |
21 | 24 |
22 boolean empty_mem_output_buffer(j_compress_ptr cinfo) { | 25 static boolean empty_mem_output_buffer(j_compress_ptr cinfo) { |
23 size_t nextsize; | 26 size_t nextsize; |
24 JOCTET * nextbuffer; | 27 JOCTET * nextbuffer; |
25 mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest; | 28 mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest; |
26 | 29 |
27 /* Try to allocate new buffer with double size */ | 30 /* Try to allocate new buffer with double size */ |
45 dest->bufsize = nextsize; | 48 dest->bufsize = nextsize; |
46 | 49 |
47 return TRUE; | 50 return TRUE; |
48 } | 51 } |
49 | 52 |
50 void term_mem_destination(j_compress_ptr cinfo) { | 53 static void term_mem_destination(j_compress_ptr cinfo) { |
51 mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest; | 54 mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest; |
52 | 55 |
53 *dest->outbuffer = dest->buffer; | 56 *dest->outbuffer = dest->buffer; |
54 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer; | 57 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer; |
55 } | 58 } |
56 | 59 |
57 void jpeg_mem_dest(j_compress_ptr cinfo, unsigned char ** outbuffer, unsigned long * outsize) { | 60 static void jpeg_mem_dest(j_compress_ptr cinfo, unsigned char ** outbuffer, unsigned long * outsize) { |
58 mem_dest_mgr_ptr dest; | 61 mem_dest_mgr_ptr dest; |
59 | 62 |
60 if (outbuffer == NULL || outsize == NULL) /* sanity check */ | 63 if (outbuffer == NULL || outsize == NULL) /* sanity check */ |
61 ERREXIT(cinfo, JERR_BUFFER_SIZE); | 64 ERREXIT(cinfo, JERR_BUFFER_SIZE); |
62 | 65 |
88 dest->pub.next_output_byte = dest->buffer = *outbuffer; | 91 dest->pub.next_output_byte = dest->buffer = *outbuffer; |
89 dest->pub.free_in_buffer = dest->bufsize = *outsize; | 92 dest->pub.free_in_buffer = dest->bufsize = *outsize; |
90 } | 93 } |
91 | 94 |
92 | 95 |
93 int build_jpg(JSAMPLE *image_buf, int width, int height) { | 96 |
97 jpg_buf_t build_jpg_from_fb(int fb, int width, int height) { | |
94 int scanline; | 98 int scanline; |
95 struct jpeg_compress_struct cinfo; | 99 struct jpeg_compress_struct cinfo; |
96 struct jpeg_error_mgr jerr; | 100 struct jpeg_error_mgr jerr; |
97 | 101 |
98 cinfo.err = jpeg_std_error(&jerr); | 102 cinfo.err = jpeg_std_error(&jerr); |
99 jpeg_create_compress(&cinfo); | 103 jpeg_create_compress(&cinfo); |
100 | 104 |
101 unsigned long out_size = 1<<20; | 105 jpg_buf_t jpg_buf; |
102 unsigned char *out_buf = NULL; | 106 jpg_buf.size = 1 << 20; |
103 jpeg_mem_dest(&cinfo, &out_buf, &out_size); | 107 jpg_buf.ptr = NULL; |
108 | |
109 jpeg_mem_dest(&cinfo, &jpg_buf.ptr, &jpg_buf.size); | |
104 | 110 |
105 cinfo.image_width = width; | 111 cinfo.image_width = width; |
106 cinfo.image_height = height; | 112 cinfo.image_height = height; |
107 cinfo.input_components = 3; | 113 cinfo.input_components = 3; |
108 cinfo.in_color_space = JCS_RGB; | 114 cinfo.in_color_space = JCS_RGB; |
109 jpeg_set_defaults(&cinfo); | 115 jpeg_set_defaults(&cinfo); |
110 | 116 |
111 jpeg_start_compress(&cinfo, TRUE); | 117 jpeg_start_compress(&cinfo, TRUE); |
112 | 118 |
119 JSAMPLE *buf = malloc(width * 4); | |
120 JSAMPROW row_ptr[1]; | |
121 row_ptr[0] = buf; | |
113 for (scanline = 0; scanline < height; scanline++) { | 122 for (scanline = 0; scanline < height; scanline++) { |
114 JSAMPROW *row_ptr; | 123 read(fb, buf, width*4); |
115 row_ptr = &image_buf[scanline * width]; | |
116 jpeg_write_scanlines(&cinfo, row_ptr, 1); | 124 jpeg_write_scanlines(&cinfo, row_ptr, 1); |
117 } | 125 } |
126 free(buf); | |
118 | 127 |
119 jpeg_finish_compress(&cinfo); | 128 jpeg_finish_compress(&cinfo); |
120 | 129 |
121 jpeg_destroy_compress(&cinfo); | 130 jpeg_destroy_compress(&cinfo); |
122 | 131 |
123 return 0; | 132 return jpg_buf; |
124 } | 133 } |