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.h File Reference
#include <stdio.h>
#include <string.h>
#include "vsfilesystem.h"
#include <jconfig.h>
#include <jmorecfg.h>
#include <jpeglib.h>

Go to the source code of this file.

Classes

struct  memory_destination_mgr
 

Macros

#define JPEG_SUPPORT
 
#define __JPEG_MEMORY_H
 

Typedefs

typedef memory_destination_mgrmem_dest_ptr
 

Functions

 init_destination (j_compress_ptr cinfo)
 
 empty_output_buffer (j_compress_ptr cinfo)
 
 term_destination (j_compress_ptr cinfo)
 
 GLOBAL (void) jpeg_memory_dest(j_compress_ptr cinfo
 
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)
 
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)
 

Variables

JOCTET * buffer
 
JOCTET int bufsize
 

Macro Definition Documentation

#define __JPEG_MEMORY_H

Definition at line 5 of file jpeg_memory.h.

#define JPEG_SUPPORT

Definition at line 2 of file jpeg_memory.h.

Typedef Documentation

Definition at line 52 of file jpeg_memory.h.

Function Documentation

empty_output_buffer ( j_compress_ptr  cinfo)

Definition at line 69 of file jpeg_memory.h.

References memory_destination_mgr::buffer, memory_destination_mgr::bufsize, memory_destination_mgr::pub, and TRUE.

Referenced by jpeg_memory_dest().

70 {
71  mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
72  dest->pub.next_output_byte = dest->buffer;
73  dest->pub.free_in_buffer = dest->bufsize;
74 
75  return TRUE;
76 }
GLOBAL ( void  )
init_destination ( j_compress_ptr  cinfo)

Definition at line 58 of file jpeg_memory.h.

References memory_destination_mgr::buffer, memory_destination_mgr::bufsize, memory_destination_mgr::datacount, and memory_destination_mgr::pub.

Referenced by jpeg_memory_dest().

59 {
60  mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
61  dest->pub.next_output_byte = dest->buffer;
62  dest->pub.free_in_buffer = dest->bufsize;
63  dest->datacount = 0;
64 }
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 }
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 }
term_destination ( j_compress_ptr  cinfo)

Definition at line 82 of file jpeg_memory.h.

References memory_destination_mgr::bufsize, memory_destination_mgr::datacount, and memory_destination_mgr::pub.

Referenced by jpeg_memory_dest().

83 {
84  /* expose the finale compressed image size */
85 
86  mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
87  dest->datacount = dest->bufsize-dest->pub.free_in_buffer;
88 }

Variable Documentation