Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jpeg_memory.cpp File Reference
#include "jpeg_memory.h"
#include <iostream>

Go to the source code of this file.

Functions

 jpeg_memory_dest (j_compress_ptr cinfo, JOCTET *buffer, int bufsize)
 
int jpeg_compress (char *dst, char *src, int width, int height, int dstsize, int quality)
 
int jpeg_compress_to_file (char *src, char *file, int width, int height, int quality)
 
static void init_source (j_decompress_ptr cinfo)
 
static boolean fill_input_buffer (j_decompress_ptr cinfo)
 
static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
 
static void term_source (j_decompress_ptr cinfo)
 
void jpeg_memory_src (j_decompress_ptr cinfo, unsigned char *ptr, size_t size)
 
void jpeg_decompress (unsigned char *dst, unsigned char *src, int size, int *w, int *h)
 
void jpeg_decompress_from_file (unsigned char *dst, char *file, int size, int *w, int *h)
 

Function Documentation

static boolean fill_input_buffer ( j_decompress_ptr  cinfo)
static

Definition at line 111 of file jpeg_memory.cpp.

References FALSE.

Referenced by jpeg_memory_src().

112 {
113  /* can't fill */
114  return FALSE;
115 }
static void init_source ( j_decompress_ptr  cinfo)
static

Definition at line 106 of file jpeg_memory.cpp.

Referenced by jpeg_memory_src().

107 {
108  /* nothing to do */
109 }
int jpeg_compress ( char *  dst,
char *  src,
int  width,
int  height,
int  dstsize,
int  quality 
)

Definition at line 24 of file jpeg_memory.cpp.

References memory_destination_mgr::datacount, height, jpeg_memory_dest(), TRUE, and width.

25 {
26  struct jpeg_compress_struct cinfo;
27  struct jpeg_error_mgr jerr;
28  unsigned char *dataRGB = (unsigned char*) src;
29  JSAMPROW row_pointer = (JSAMPROW) dataRGB;
30  JOCTET *jpgbuff;
31  mem_dest_ptr dest;
32  int csize = 0;
33 
34  /* zero out the compresion info structures and
35  * allocate a new compressor handle */
36  memset( &cinfo, 0, sizeof (cinfo) );
37  cinfo.err = jpeg_std_error( &jerr );
38  jpeg_create_compress( &cinfo );
39 
40  /* Setup JPEG datastructures */
41  cinfo.image_width = width; /* image width and height, in pixels */
42  cinfo.image_height = height;
43  cinfo.input_components = 3; /* # of color components per pixel=3 RGB */
44  cinfo.in_color_space = JCS_RGB;
45  jpgbuff = (JOCTET*) dst;
46 
47  /* Setup compression and do it */
48  jpeg_memory_dest( &cinfo, jpgbuff, dstsize );
49  jpeg_set_defaults( &cinfo );
50  jpeg_set_quality( &cinfo, quality, TRUE );
51  jpeg_start_compress( &cinfo, TRUE );
52  /* compress each scanline one-at-a-time */
53  while (cinfo.next_scanline < cinfo.image_height) {
54  row_pointer = (JSAMPROW) ( dataRGB+(cinfo.next_scanline*3*width) );
55  jpeg_write_scanlines( &cinfo, &row_pointer, 1 );
56  }
57  jpeg_finish_compress( &cinfo );
58  /* Now extract the size of the compressed buffer */
59  dest = (mem_dest_ptr) cinfo.dest;
60  csize = dest->datacount; /* the actual compressed datasize */
61  /* destroy the compressor handle */
62  jpeg_destroy_compress( &cinfo );
63  return csize;
64 }
int jpeg_compress_to_file ( char *  src,
char *  file,
int  width,
int  height,
int  quality 
)

Definition at line 66 of file jpeg_memory.cpp.

References fclose, fopen, height, TRUE, and width.

67 {
68  FILE *outfile;
69  struct jpeg_compress_struct cinfo;
70  struct jpeg_error_mgr jerr;
71  JSAMPROW row_pointer;
72  if ( ( outfile = fopen( file, "wb" ) ) == NULL ) {
73  cerr<<"can't open "<<file<<endl;
74  return -1;
75  }
76  /* zero out the compresion info structures and
77  * allocate a new compressor handle */
78  memset( &cinfo, 0, sizeof (cinfo) );
79  cinfo.err = jpeg_std_error( &jerr );
80  jpeg_create_compress( &cinfo );
81 
82  /* Setup JPEG datastructures */
83  cinfo.image_width = width; /* image width and height, in pixels */
84  cinfo.image_height = height;
85  cinfo.input_components = 3; /* # of color components per pixel=3 RGB */
86  cinfo.in_color_space = JCS_RGB;
87 
88  /* Setup compression and do it */
89  jpeg_stdio_dest( &cinfo, outfile );
90  jpeg_set_defaults( &cinfo );
91  jpeg_set_quality( &cinfo, quality, TRUE );
92  jpeg_start_compress( &cinfo, TRUE );
93  /* compress each scanline one-at-a-time */
94  while (cinfo.next_scanline < cinfo.image_height) {
95  row_pointer = (JSAMPROW) &src[cinfo.next_scanline*3*width];
96  jpeg_write_scanlines( &cinfo, &row_pointer, 1 );
97  }
98  jpeg_finish_compress( &cinfo );
99 
100  /* destroy the compressor handle */
101  jpeg_destroy_compress( &cinfo );
102  fclose( outfile );
103  return 0;
104 }
void jpeg_decompress ( unsigned char *  dst,
unsigned char *  src,
int  size,
int w,
int h 
)

Definition at line 155 of file jpeg_memory.cpp.

References jpeg_memory_src(), TRUE, and y.

156 {
157  struct jpeg_decompress_struct cinfo;
158  struct jpeg_error_mgr jerr;
159  size_t line_size, y;
160  unsigned char *dstcur;
161 
162  cinfo.err = jpeg_std_error( &jerr );
163  jpeg_create_decompress( &cinfo );
164  jpeg_memory_src( &cinfo, src, size );
165  jpeg_read_header( &cinfo, TRUE );
166  jpeg_start_decompress( &cinfo );
167 
168  *w = cinfo.output_width;
169  *h = cinfo.output_height;
170  line_size = cinfo.output_width*cinfo.output_components;
171 
172  dstcur = dst;
173  for (y = 0; y < cinfo.output_height; y++) {
174  jpeg_read_scanlines( &cinfo, (JSAMPARRAY) &dstcur, 1 );
175  dstcur += line_size;
176  }
177  jpeg_finish_decompress( &cinfo );
178  jpeg_destroy_decompress( &cinfo );
179 }
void jpeg_decompress_from_file ( unsigned char *  dst,
char *  file,
int  size,
int w,
int h 
)

Definition at line 181 of file jpeg_memory.cpp.

References fclose, fopen, TRUE, and y.

182 {
183  struct jpeg_decompress_struct cinfo;
184  struct jpeg_error_mgr jerr;
185  int line_size;
186  unsigned char *dstcur;
187  FILE *infile;
188  if ( ( infile = fopen( file, "rb" ) ) == NULL ) {
189  cerr<<"can't open "<<file<<endl;
190  return;
191  }
192  cinfo.err = jpeg_std_error( &jerr );
193  jpeg_create_decompress( &cinfo );
194  jpeg_stdio_src( &cinfo, infile );
195  jpeg_read_header( &cinfo, TRUE );
196  jpeg_start_decompress( &cinfo );
197 
198  *w = cinfo.output_width;
199  *h = cinfo.output_height;
200  line_size = cinfo.output_width*cinfo.output_components;
201 
202  dstcur = dst;
203  for (size_t y = 0; y < cinfo.output_height; y++) {
204  jpeg_read_scanlines( &cinfo, (JSAMPARRAY) &dstcur, 1 );
205  dstcur += line_size;
206  }
207  jpeg_finish_decompress( &cinfo );
208  jpeg_destroy_decompress( &cinfo );
209  fclose( infile );
210 }
jpeg_memory_dest ( j_compress_ptr  cinfo,
JOCTET *  buffer,
int  bufsize 
)

Definition at line 9 of file jpeg_memory.cpp.

References memory_destination_mgr::buffer, memory_destination_mgr::bufsize, empty_output_buffer(), init_destination(), memory_destination_mgr::pub, and term_destination().

Referenced by jpeg_compress().

10 {
11  mem_dest_ptr dest;
12  if (cinfo->dest == NULL) /* first time for this JPEG object? */
13  cinfo->dest = (struct jpeg_destination_mgr*)
14  (*cinfo->mem->alloc_small)( (j_common_ptr) cinfo, JPOOL_PERMANENT,
15  sizeof (memory_destination_mgr) );
16  dest = (mem_dest_ptr) cinfo->dest;
17  dest->bufsize = bufsize;
18  dest->buffer = buffer;
19  dest->pub.init_destination = init_destination;
20  dest->pub.empty_output_buffer = empty_output_buffer;
21  dest->pub.term_destination = term_destination;
22 }
void jpeg_memory_src ( j_decompress_ptr  cinfo,
unsigned char *  ptr,
size_t  size 
)

set momory-jpeg image to JPEG lib Info struct

Parameters
cinfoJPEG lib decompress infomation structure
ptrJPEG image
sizeJPEG image size

Definition at line 139 of file jpeg_memory.cpp.

References fill_input_buffer(), init_source(), size, skip_input_data(), and term_source().

Referenced by jpeg_decompress().

140 {
141  struct jpeg_source_mgr *src;
142  src = cinfo->src = (struct jpeg_source_mgr*)
143  (*cinfo->mem->alloc_small)( (j_common_ptr) cinfo,
144  JPOOL_PERMANENT,
145  sizeof (*src) );
146  src->init_source = init_source;
147  src->fill_input_buffer = fill_input_buffer;
148  src->skip_input_data = skip_input_data;
149  src->resync_to_restart = jpeg_resync_to_restart;
150  src->term_source = term_source;
151  src->next_input_byte = ptr;
152  src->bytes_in_buffer = size;
153 }
static void skip_input_data ( j_decompress_ptr  cinfo,
long  num_bytes 
)
static

Definition at line 117 of file jpeg_memory.cpp.

Referenced by jpeg_memory_src().

118 {
119  if ( (size_t) num_bytes > cinfo->src->bytes_in_buffer ) {
120  cinfo->src->next_input_byte = NULL;
121  cinfo->src->bytes_in_buffer = 0;
122  } else {
123  cinfo->src->next_input_byte += (size_t) num_bytes;
124  cinfo->src->bytes_in_buffer -= (size_t) num_bytes;
125  }
126 }
static void term_source ( j_decompress_ptr  cinfo)
static

Definition at line 128 of file jpeg_memory.cpp.

Referenced by jpeg_memory_src().

129 {
130  /* nothing to do */
131 }