1
/* tilp - a linking program for TI graphing calculators
2
* Copyright (C) 1999-2002 Romain Lievin
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
# include <jmorecfg.h>
46
int write_jpg_2_colors(FILE *file, Image *img) // tested: OK (14/05)
49
struct jpeg_compress_struct cinfo;
50
struct jpeg_error_mgr jerr;
53
DISPLAY("write_jpg_2_colors\n");
54
convert_bitmap_to_bytemap(img);
55
if(!(img->inverted)) invert_bytemap(img);
56
// Initialize JPEG library
57
cinfo.err = jpeg_std_error(&jerr);
58
jpeg_create_compress(&cinfo);
59
jpeg_stdio_dest(&cinfo, file);
61
cinfo.image_width = img->width;
62
cinfo.image_height = img->height;
63
cinfo.input_components = 1; // # of color components per pixel
64
cinfo.in_color_space = JCS_GRAYSCALE; // gray scales
66
jpeg_set_defaults(&cinfo);
67
jpeg_set_quality(&cinfo, 100, TRUE); // quality = 100% (better)
68
jpeg_start_compress(&cinfo, TRUE);
70
//fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
72
while(cinfo.next_scanline < cinfo.image_height) // save jpeg image
74
jpeg_write_scanlines(&cinfo, &c, 1);
78
jpeg_finish_compress(&cinfo);
79
jpeg_destroy_compress(&cinfo);
85
int write_jpg_256_colors(FILE *file, Image *img)
87
//fprintf(stderr, "write_jpg_256_colors\n");
89
convert_bytemap_to_rgbmap(img);
90
write_jpg_true_colors(file, img);
95
int write_jpg_true_colors(FILE *file, Image *img)
98
struct jpeg_compress_struct cinfo;
99
struct jpeg_error_mgr jerr;
102
//fprintf(stderr, "write_jpg_true_colors\n");
103
// Initialize JPEG library
104
cinfo.err = jpeg_std_error(&jerr);
105
jpeg_create_compress(&cinfo);
106
jpeg_stdio_dest(&cinfo, file);
108
cinfo.image_width = img->width;
109
cinfo.image_height = img->height;
110
cinfo.input_components = 3; // # of color components per pixel
111
cinfo.in_color_space = JCS_RGB; // rrggbb
113
jpeg_set_defaults(&cinfo);
114
//jpeg_set_quality(&cinfo, quality, TRUE); // base-line
115
jpeg_start_compress(&cinfo, TRUE);
117
//fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
119
while(cinfo.next_scanline < cinfo.image_height) // save jpeg image
121
jpeg_write_scanlines(&cinfo, &c, 1);
125
jpeg_finish_compress(&cinfo);
126
jpeg_destroy_compress(&cinfo);
131
int write_jpg_format(FILE *file, Image *img)
133
DISPLAY("write_jpg_format\n");
134
if(img->encoding == IMG_COL_TYPE)
137
return write_jpg_256_colors(file, img);
139
return write_jpg_true_colors(file, img);
142
return write_jpg_2_colors(file, img);
150
int read_jpg_2_colors(FILE *file, Image *img)
152
//fprintf(stderr, "read_jpg_2_colors\n");
157
int read_jpg_256_colors(FILE *file, Image *img) //tested: OK
160
struct jpeg_decompress_struct cinfo;
161
struct jpeg_error_mgr jerr;
166
//fprintf(stderr, "read_jpg_256_colors\n");
167
// Initialize JPEG library
168
cinfo.err = jpeg_std_error(&jerr);
169
jpeg_create_decompress(&cinfo);
170
jpeg_stdio_src(&cinfo, file);
171
jpeg_read_header(&cinfo, TRUE);
174
//fprintf(stdout, "JPEG image_width: %i\n", cinfo.image_width);
175
//fprintf(stdout, "JPEG image_height: %i\n", cinfo.image_height);
176
img->width = cinfo.image_width;
177
img->height = cinfo.image_height;
179
// Initialize color mapping
180
cinfo.quantize_colors = TRUE; // colormapped output wanted
181
cinfo.desired_number_of_colors = 255; // decrease color depth
183
// Read and decompress image
184
jpeg_start_decompress(&cinfo);
186
fprintf(stdout, "JPEG output_width: %i\n", cinfo.output_width);
187
fprintf(stdout, "JPEG output_height: %i\n", cinfo.output_height);
188
fprintf(stdout, "JPEG output_components: %i\n", cinfo.output_components);
189
fprintf(stdout, "JPEG out_color_components: %i\n", cinfo.out_color_components);
191
fprintf(stdout, "JPEG actual_number_of_colors: %i\n", cinfo.actual_number_of_colors);
192
img->depth = cinfo.actual_number_of_colors;
194
// Copy colormap into palette
195
(img->colormap) = (byte *)malloc(3 * 256 * sizeof(byte));
196
for(j=0; j<cinfo.actual_number_of_colors; j++) // copy the colormap
198
(img->colormap)[3*j+0] = cinfo.colormap[0][j];
199
(img->colormap)[3*j+1] = cinfo.colormap[1][j];
200
(img->colormap)[3*j+2] = cinfo.colormap[2][j];
203
if (cinfo.output_components != 1) // 1: palettized, 3: rrggbb
207
img->bytemap = c = (byte *)malloc(img->width * img->height);
208
if(c == NULL) g_error("Malloc error.\n");
210
//fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
211
while(cinfo.output_scanline < cinfo.output_height) // load jpeg image
214
jpeg_read_scanlines(&cinfo, &c, 1);
216
//fprintf(stdout, "jpeg_start, img->bytemap = %p\n", img->bytemap);
217
//fprintf(stdout, "jpeg_end, img->bytemap = %p\n", c);
219
jpeg_finish_decompress(&cinfo);
220
jpeg_destroy_decompress(&cinfo);
227
int read_jpg_format (FILE *file, Image *img)
229
//fprintf(stderr, "read_jpg_format\n");
230
if(img->encoding == IMG_COL_TYPE)
231
return read_jpg_256_colors(file, img);
233
return read_jpg_2_colors(file, img);