~ubuntu-branches/ubuntu/breezy/tiemu/breezy

« back to all changes in this revision

Viewing changes to src/hid/img/jpg.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien BLACHE
  • Date: 2005-06-02 16:50:15 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050602165015-59ab24414tl2wzol
Tags: 1.99+svn1460-1
* New snapshot.
* debian/control:
  + Updated build-depends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  tilp - a linking program for TI graphing calculators
2
 
 *  Copyright (C) 1999-2002  Romain Lievin
3
 
 *
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.
8
 
 *
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.
13
 
 *
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.
17
 
 */
18
 
/*
19
 
#ifdef __WIN32__
20
 
# define HAVE_LIBJPEG
21
 
# include <stddef.h>
22
 
# include <jmorecfg.h>
23
 
# include <jpeglib.h>
24
 
#endif
25
 
*/
26
 
 
27
 
#ifdef HAVE_CONFIG_H
28
 
#include <config.h>
29
 
#endif
30
 
#include <stdio.h>
31
 
#include <stdlib.h>
32
 
#include <string.h>
33
 
#include <glib.h>
34
 
#ifdef HAVE_LIBJPEG
35
 
# include <jpeglib.h>
36
 
#endif
37
 
 
38
 
#include "img/fmt.h"
39
 
#include "tilibs.h"
40
 
 
41
 
 
42
 
/***********/
43
 
/* Writing */
44
 
/***********/
45
 
 
46
 
int write_jpg_2_colors(FILE *file, Image *img) // tested: OK (14/05)
47
 
{
48
 
#ifdef HAVE_LIBJPEG
49
 
  struct jpeg_compress_struct cinfo;
50
 
  struct jpeg_error_mgr jerr;
51
 
  unsigned char *c;
52
 
 
53
 
  DISPLAY("write_jpg_2_colors\n");
54
 
  convert_bitmap_to_bytemap(img);
55
 
  if(!(img->inverted)) invert_bytemap(img);
56
 
 
57
 
  // Initialize JPEG library
58
 
  cinfo.err = jpeg_std_error(&jerr);
59
 
  jpeg_create_compress(&cinfo);
60
 
  jpeg_stdio_dest(&cinfo, file);
61
 
  
62
 
  cinfo.image_width = img->width;
63
 
  cinfo.image_height = img->height;
64
 
  cinfo.input_components = 1; // # of color components per pixel
65
 
  cinfo.in_color_space = JCS_GRAYSCALE; // gray scales
66
 
 
67
 
  jpeg_set_defaults(&cinfo);
68
 
  jpeg_set_quality(&cinfo, 100, TRUE); // quality = 100% (better)
69
 
  jpeg_start_compress(&cinfo, TRUE);
70
 
  
71
 
  //fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
72
 
  c = img->bytemap;
73
 
  while(cinfo.next_scanline < cinfo.image_height) // save jpeg image
74
 
    {
75
 
      jpeg_write_scanlines(&cinfo, &c, 1);
76
 
      c += (img->width);
77
 
    }
78
 
 
79
 
  jpeg_finish_compress(&cinfo);
80
 
  jpeg_destroy_compress(&cinfo);
81
 
#endif
82
 
    
83
 
  return 0;
84
 
}
85
 
 
86
 
int write_jpg_256_colors(FILE *file, Image *img)
87
 
{
88
 
  //fprintf(stderr, "write_jpg_256_colors\n");
89
 
 
90
 
  convert_bytemap_to_rgbmap(img);
91
 
  write_jpg_true_colors(file, img);
92
 
 
93
 
  return 0;
94
 
}
95
 
 
96
 
int write_jpg_true_colors(FILE *file, Image *img)
97
 
{
98
 
#ifdef HAVE_LIBJPEG
99
 
  struct jpeg_compress_struct cinfo;
100
 
  struct jpeg_error_mgr jerr;
101
 
  unsigned char *c;
102
 
 
103
 
  //fprintf(stderr, "write_jpg_true_colors\n");
104
 
  // Initialize JPEG library
105
 
  cinfo.err = jpeg_std_error(&jerr);
106
 
  jpeg_create_compress(&cinfo);
107
 
  jpeg_stdio_dest(&cinfo, file);
108
 
  
109
 
  cinfo.image_width = img->width;
110
 
  cinfo.image_height = img->height;
111
 
  cinfo.input_components = 3; // # of color components per pixel
112
 
  cinfo.in_color_space = JCS_RGB; // rrggbb
113
 
 
114
 
  jpeg_set_defaults(&cinfo);
115
 
  //jpeg_set_quality(&cinfo, quality, TRUE); // base-line
116
 
  jpeg_start_compress(&cinfo, TRUE);
117
 
  
118
 
  //fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
119
 
  c = img->rgbmap;
120
 
  while(cinfo.next_scanline < cinfo.image_height) // save jpeg image
121
 
    {
122
 
      jpeg_write_scanlines(&cinfo, &c, 1);
123
 
      c += 3*(img->width);
124
 
    }
125
 
 
126
 
  jpeg_finish_compress(&cinfo);
127
 
  jpeg_destroy_compress(&cinfo);
128
 
#endif
129
 
  return 0;
130
 
}
131
 
 
132
 
int write_jpg_format(FILE *file, Image *img)
133
 
{
134
 
  DISPLAY("write_jpg_format\n");
135
 
  if(img->encoding == IMG_COL_TYPE)
136
 
    {
137
 
      if(img->depth < 257)
138
 
        return write_jpg_256_colors(file, img);
139
 
      else
140
 
        return write_jpg_true_colors(file, img);
141
 
    }
142
 
  else
143
 
    {
144
 
      
145
 
      return write_jpg_2_colors(file, img);
146
 
    }
147
 
}
148
 
 
149
 
 
150
 
/***********/
151
 
/* Reading */
152
 
/***********/
153
 
 
154
 
int read_jpg_2_colors(FILE *file, Image *img)
155
 
{
156
 
  //fprintf(stderr, "read_jpg_2_colors\n");
157
 
 
158
 
  return 0;
159
 
}
160
 
 
161
 
int read_jpg_256_colors(FILE *file, Image *img) //tested: OK
162
 
{
163
 
#ifdef HAVE_LIBJPEG
164
 
  struct jpeg_decompress_struct cinfo;
165
 
  struct jpeg_error_mgr jerr;
166
 
  unsigned char *c;
167
 
  //int x, y;
168
 
  int j; 
169
 
 
170
 
  //fprintf(stderr, "read_jpg_256_colors\n");
171
 
  // Initialize JPEG library
172
 
  cinfo.err = jpeg_std_error(&jerr);
173
 
  jpeg_create_decompress(&cinfo);
174
 
  jpeg_stdio_src(&cinfo, file);
175
 
  jpeg_read_header(&cinfo, TRUE);
176
 
 
177
 
  // Get image size
178
 
  //fprintf(stdout, "JPEG image_width: %i\n", cinfo.image_width);
179
 
  //fprintf(stdout, "JPEG image_height: %i\n", cinfo.image_height);
180
 
  img->width = cinfo.image_width;
181
 
  img->height = cinfo.image_height;
182
 
 
183
 
  // Initialize color mapping
184
 
  cinfo.quantize_colors = TRUE; // colormapped output wanted
185
 
  cinfo.desired_number_of_colors = 255; // decrease color depth
186
 
 
187
 
  // Read and decompress image
188
 
  jpeg_start_decompress(&cinfo);
189
 
 
190
 
  fprintf(stdout, "JPEG output_width: %i\n", cinfo.output_width);
191
 
  fprintf(stdout, "JPEG output_height: %i\n", cinfo.output_height);
192
 
  fprintf(stdout, "JPEG output_components: %i\n", cinfo.output_components);
193
 
  fprintf(stdout, "JPEG out_color_components: %i\n", cinfo.out_color_components);
194
 
 
195
 
  fprintf(stdout, "JPEG actual_number_of_colors: %i\n", cinfo.actual_number_of_colors);
196
 
  img->depth = cinfo.actual_number_of_colors;
197
 
 
198
 
  // Copy colormap into palette
199
 
  (img->colormap) = (uint8_t *)malloc(3 * 256 * sizeof(uint8_t));
200
 
  for(j=0; j<cinfo.actual_number_of_colors; j++) // copy the colormap
201
 
    {
202
 
      (img->colormap)[3*j+0] = cinfo.colormap[0][j];
203
 
      (img->colormap)[3*j+1] = cinfo.colormap[1][j];
204
 
      (img->colormap)[3*j+2] = cinfo.colormap[2][j];
205
 
    }
206
 
 
207
 
  if (cinfo.output_components != 1) // 1: palettized, 3: rrggbb
208
 
    return -1;
209
 
 
210
 
  // Allocate image
211
 
  img->bytemap = c = (uint8_t *)malloc(img->width * img->height);
212
 
  if(c == NULL) g_error("Malloc error.\n");
213
 
 
214
 
  //fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
215
 
  while(cinfo.output_scanline < cinfo.output_height) // load jpeg image
216
 
    {
217
 
      c += img->width;
218
 
      jpeg_read_scanlines(&cinfo, &c, 1);
219
 
    }
220
 
  //fprintf(stdout, "jpeg_start, img->bytemap = %p\n", img->bytemap);
221
 
  //fprintf(stdout, "jpeg_end, img->bytemap = %p\n", c);
222
 
 
223
 
  jpeg_finish_decompress(&cinfo);
224
 
  jpeg_destroy_decompress(&cinfo);
225
 
 
226
 
 
227
 
#endif
228
 
  return 0;
229
 
}
230
 
 
231
 
int read_jpg_format (FILE *file, Image *img)
232
 
{
233
 
  clear_image(img);
234
 
 
235
 
  //fprintf(stderr, "read_jpg_format\n");
236
 
  if(img->encoding == IMG_COL_TYPE)
237
 
    return read_jpg_256_colors(file, img);
238
 
  else
239
 
    return read_jpg_2_colors(file, img);
240
 
}