QTrk
Classes | Functions
fastjpg.cpp File Reference
#include "std_incl.h"
#include <cstdio>
#include "jpeglib.h"
#include "utils.h"
#include "TeLibJpeg\jmemdstsrc.h"

Go to the source code of this file.

Classes

struct  my_error_mgr
 

Functions

int ReadJPEGFile (uchar *srcbuf, int srclen, uchar **data, int *width, int *height)
 
void WriteJPEGFile (uchar *data, int w, int h, const char *filename, int quality)
 
void FloatToJPEGFile (const char *name, const float *d, int w, int h)
 

Function Documentation

§ FloatToJPEGFile()

void FloatToJPEGFile ( const char *  name,
const float *  d,
int  w,
int  h 
)

Definition at line 189 of file fastjpg.cpp.

190 {
191  uchar* zlut_bytes = floatToNormalizedInt(d, w,h, (uchar)255);
192  WriteJPEGFile(zlut_bytes, w, h, name, 99);
193  delete[] zlut_bytes;
194 }
void floatToNormalizedInt(T *dst, const float *src, uint w, uint h, T maxValue)
Definition: utils.h:199
void WriteJPEGFile(uchar *data, int w, int h, const char *filename, int quality)
Definition: fastjpg.cpp:89
unsigned char uchar
Definition: std_incl.h:130

§ ReadJPEGFile()

int ReadJPEGFile ( uchar srcbuf,
int  srclen,
uchar **  data,
int *  width,
int *  height 
)

Definition at line 12 of file fastjpg.cpp.

13 {
14  struct jpeg_decompress_struct cinfo;
15 
16  JSAMPARRAY buffer; /* Output row buffer */
17  int row_stride; /* physical row width in output buffer */
18  my_error_mgr jerr;
19  cinfo.err = jpeg_std_error(&jerr.pub);
20  jpeg_create_decompress(&cinfo);
21 
22  j_mem_src(&cinfo, srcbuf, srclen);
23 
24  /* Step 3: read file parameters with jpeg_read_header() */
25  jpeg_read_header(&cinfo, TRUE);
26 
27  jpeg_start_decompress(&cinfo);
28  row_stride = cinfo.output_width * cinfo.output_components;
29  /* Make a one-row-high sample array that will go away when done with image */
30  buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
31 
32  *width = cinfo.output_width;
33  *height = cinfo.output_height;
34  *data = new uchar[cinfo.output_width*cinfo.output_height];
35 
36 // ResizeLVArray2D(output, cinfo.output_height, cinfo.output_width);
37 
38  /* Step 6: while (scan lines remain to be read) */
39  /* jpeg_read_scanlines(...); */
40 
41  /* Here we use the library's state variable cinfo.output_scanline as the
42  * loop counter, so that we don't have to keep track ourselves.
43  */
44  uchar* dst = *data;
45  while (cinfo.output_scanline < cinfo.output_height) {
46  /* jpeg_read_scanlines expects an array of pointers to scanlines.
47  * Here the array is only one element long, but you could ask for
48  * more than one scanline at a time if that's more convenient.
49  */
50  jpeg_read_scanlines(&cinfo, buffer, 1);
51  /* Assume put_scanline_someplace wants a pointer and sample count. */
52 
53  unsigned char* src = buffer[0];
54  if (cinfo.output_components == 1) {
55  memcpy(dst, src, cinfo.output_width);
56  } else {
57  for (uint x=0;x<cinfo.output_width;x++)
58  dst[x] = src[x * cinfo.output_components];
59  }
60  dst += cinfo.output_width;
61  }
62 
63  /* Step 7: Finish decompression */
64  jpeg_finish_decompress(&cinfo);
65  /* We can ignore the return value since suspension is not possible
66  * with the stdio data source.
67  */
68 
69  /* Step 8: Release JPEG decompression object */
70 
71  /* This is an important step since it will release a good deal of memory. */
72  jpeg_destroy_decompress(&cinfo);
73 
74  /* After finish_decompress, we can close the input file.
75  * Here we postpone it until after no more JPEG errors are possible,
76  * so as to simplify the setjmp error logic above. (Actually, I don't
77  * think that jpeg_destroy can do an error exit, but why assume anything...)
78  */
79 // fclose(infile);
80 
81  /* At this point you may want to check to see whether any corrupt-data
82  * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
83  */
84 
85  return 1;
86 }
struct jpeg_error_mgr pub
Definition: fastjpg.cpp:9
unsigned int uint
Definition: std_incl.h:127
unsigned char uchar
Definition: std_incl.h:130

§ WriteJPEGFile()

void WriteJPEGFile ( uchar data,
int  w,
int  h,
const char *  filename,
int  quality 
)

Definition at line 89 of file fastjpg.cpp.

90 {
91  /* This struct contains the JPEG compression parameters and pointers to
92  * working space (which is allocated as needed by the JPEG library).
93  * It is possible to have several such structures, representing multiple
94  * compression/decompression processes, in existence at once. We refer
95  * to any one struct (and its associated working data) as a "JPEG object".
96  */
97  struct jpeg_compress_struct cinfo;
98  /* This struct represents a JPEG error handler. It is declared separately
99  * because applications often want to supply a specialized error handler
100  * (see the second half of this file for an example). But here we just
101  * take the easy way out and use the standard error handler, which will
102  * print a message on stderr and call exit() if compression fails.
103  * Note that this struct must live as long as the main JPEG parameter
104  * struct, to avoid dangling-pointer problems.
105  */
106  struct jpeg_error_mgr jerr;
107  /* More stuff */
108  JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
109  int row_stride; /* physical row width in image buffer */
110 
111  /* Step 1: allocate and initialize JPEG compression object */
112 
113  /* We have to set up the error handler first, in case the initialization
114  * step fails. (Unlikely, but it could happen if you are out of memory.)
115  * This routine fills in the contents of struct jerr, and returns jerr's
116  * address which we place into the link field in cinfo.
117  */
118  cinfo.err = jpeg_std_error(&jerr);
119  /* Now we can initialize the JPEG compression object. */
120  jpeg_create_compress(&cinfo);
121 
122  /* Step 2: specify data destination (eg, a file) */
123  /* Note: steps 2 and 3 can be done in either order. */
124 
125  uint len = w*h * 2;
126  uchar* memBuf = new uchar[len];
127  j_mem_dest(&cinfo, (void**)&memBuf, &len);
128  //jpeg_stdio_dest(&cinfo, outfile);
129 
130  /* Step 3: set parameters for compression */
131 
132  /* First we supply a description of the input image.
133  * Four fields of the cinfo struct must be filled in:
134  */
135  cinfo.image_width = w; /* image width and height, in pixels */
136  cinfo.image_height = h;
137  cinfo.input_components = 1; /* # of color components per pixel */
138  cinfo.in_color_space = JCS_GRAYSCALE; /* colorspace of input image */
139  /* Now use the library's routine to set default compression parameters.
140  * (You must set at least cinfo.in_color_space before calling this,
141  * since the defaults depend on the source color space.)
142  */
143  jpeg_set_defaults(&cinfo);
144  /* Now you can set any non-default parameters you wish to.
145  * Here we just illustrate the use of quality (quantization table) scaling:
146  */
147  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
148 
149  /* Step 4: Start compressor */
150 
151  /* TRUE ensures that we will write a complete interchange-JPEG file.
152  * Pass TRUE unless you are very sure of what you're doing.
153  */
154  jpeg_start_compress(&cinfo, TRUE);
155 
156  /* Step 5: while (scan lines remain to be written) */
157  /* jpeg_write_scanlines(...); */
158 
159  /* Here we use the library's state variable cinfo.next_scanline as the
160  * loop counter, so that we don't have to keep track ourselves.
161  * To keep things simple, we pass one scanline per call; you can pass
162  * more if you wish, though.
163  */
164  row_stride = w; /* JSAMPLEs per row in image_buffer */
165 
166  while (cinfo.next_scanline < cinfo.image_height) {
167  /* jpeg_write_scanlines expects an array of pointers to scanlines.
168  * Here the array is only one element long, but you could pass
169  * more than one scanline at a time if that's more convenient.
170  */
171  row_pointer[0] = &data[cinfo.next_scanline * row_stride];
172  (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
173  }
174  jpeg_finish_compress(&cinfo);
175  jpeg_destroy_compress(&cinfo);
176 
177 
178  FILE *f = fopen(filename, "wb");
179  if (f) {
180  fwrite(memBuf, 1, len, f);
181  fclose(f);
182  } else
183  dbgprintf("Failed to open file %s for writing\n", filename);
184 
185  delete[] memBuf;
186 }
unsigned int uint
Definition: std_incl.h:127
void dbgprintf(const char *fmt,...)
Definition: utils.cpp:149
unsigned char uchar
Definition: std_incl.h:130