| Bsoft 2.0.5 Bernard's software package

rwJPEG.cpp
Go to the documentation of this file.
1
9 #include "rwJPEG.h"
10 #include "utilities.h"
11
12 #ifdef HAVE_JPEG
13
14 #include "jpeglib.h"
15
16
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19
31 int readJPEG(Bimage* p, int readdata)
32 {
33 struct jpeg_decompress_struct cinfo;
34 struct jpeg_error_mgr jerr;
35
36 FILE* infile; /* source file */
37 JSAMPARRAY buffer; /* Output row buffer */
38 int row_stride; /* physical row width in output buffer */
39
40 if ((infile = fopen(p->file_name().c_str(), "rb")) == NULL) return -1;
41
42 cinfo.err = jpeg_std_error(&jerr);
43 jpeg_create_decompress(&cinfo);
44
45 jpeg_stdio_src(&cinfo, infile);
46
47 jpeg_read_header(&cinfo, TRUE);
48
49 p->images(1);
50 p->size(cinfo.image_width, cinfo.image_height, 1);
51 p->channels(cinfo.num_components);
52 if ( cinfo.out_color_space == JCS_RGB ) {
53 p->compound_type(TRGB);
54 if ( verbose & VERB_DEBUG )
55 cout << "DEBUG readJPEG: Color model: RGB" << endl;
56 } else
57 if ( verbose & VERB_DEBUG )
58 cout << "DEBUG readJPEG: Color model: Gray" << endl;
59 if ( verbose & VERB_DEBUG )
60 cout << "DEBUG readJPEG: Image size: " << p->size() << endl;
61
62
63 if ( readdata ) {
64 unsigned char* data = p->data_alloc();
65
66 jpeg_start_decompress(&cinfo);
67
68 row_stride = cinfo.image_width * cinfo.num_components;
69
70 buffer = (*cinfo.mem->alloc_sarray)
71 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
72
73 while (cinfo.output_scanline < cinfo.image_height) {
74 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
75 memcpy(&data[(p->sizeY()-cinfo.output_scanline) * row_stride], buffer[0], row_stride);
76 }
77
78 jpeg_finish_decompress(&cinfo);
79 }
80
81 jpeg_destroy_decompress(&cinfo);
82
83 fclose(infile);
84
85 return 0;
86 }
87
97 int writeJPEG(Bimage* p)
98 {
99 p->change_type(UCharacter);
100
101 if ( p->sizeZ() > 1 )
102 cerr << "Warning: Only the first slice of the image will be written!" << endl;
103
104 if ( p->images() > 1 )
105 cerr << "Warning: Only the first image will be written!" << endl;
106
107 int quality = 100, err = 0;
108 struct jpeg_compress_struct cinfo;
109 struct jpeg_error_mgr jerr;
110
111 FILE* outfile; /* target file */
112 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
113 int row_stride; /* physical row width in image buffer */
114
115 cinfo.err = jpeg_std_error(&jerr);
116 jpeg_create_compress(&cinfo);
117
118 if ((outfile = fopen(p->file_name().c_str(), "wb")) == NULL) return -1;
119
120 jpeg_stdio_dest(&cinfo, outfile);
121
122 cinfo.image_width = p->sizeX(); /* image width and height, in pixels */
123 cinfo.image_height = p->sizeY();
124 cinfo.input_components = p->channels(); /* # of color components per pixel */
125 cinfo.in_color_space = JCS_GRAYSCALE;
126 if ( p->compound_type() == TRGB )
127 cinfo.in_color_space = JCS_RGB;
128
129 unsigned char* data = p->data_pointer();
130
131 jpeg_set_defaults(&cinfo);
132
133 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
134
135 jpeg_start_compress(&cinfo, TRUE);
136
137 row_stride = p->sizeX()*p->channels(); /* JSAMPLEs per row in image_buffer */
138
139 while (cinfo.next_scanline < cinfo.image_height) {
140 row_pointer[0] = (JSAMPLE *) &data[(p->sizeY()-cinfo.next_scanline-1) * row_stride];
141 jpeg_write_scanlines(&cinfo, row_pointer, 1);
142 }
143
144 jpeg_finish_compress(&cinfo);
145
146 fclose(outfile);
147
148 jpeg_destroy_compress(&cinfo);
149
150 return err;
151 }
152
153 #else
154
155 int readJPEG(Bimage* p, int readdata)
156 {
157 cerr << "Error: JPEG files are not supported!" << endl;
158
159 return -1;
160 }
161
162 int writeJPEG(Bimage* p)
163 {
164 cerr << "Error: JPEG files are not supported!" << endl;
165
166 return -1;
167 }
168
169 #endif
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
verbose
int verbose
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::channels
long channels()
Definition: Bimage.h:371
readJPEG
int readJPEG(Bimage *p, int readdata)
Definition: rwJPEG.cpp:155
TRGB
Definition: Bimage.h:62
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
UCharacter
Definition: utilities.h:149
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Bimage::images
long images()
Definition: Bimage.h:491
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
writeJPEG
int writeJPEG(Bimage *p)
Definition: rwJPEG.cpp:162
rwJPEG.h
Header file for reading and writing JPEG files.