47JpegIO::JpegIO(
const string & fname, IOMode rw)
49 rendermin(0.0), rendermax(0.0), renderbits(16)
71 bool is_new_file =
false;
72 file = sfopen(filename, rw_mode, &is_new_file,
true);
78 cinfo.err = jpeg_std_error(&jerr);
79 jpeg_create_compress(&cinfo);
80 jpeg_stdio_dest(&cinfo, file);
91bool JpegIO::is_valid(
const void *)
101int JpegIO::read_header(
Dict &,
int,
const Region *,
bool)
112int JpegIO::write_header(
const Dict & dict,
int image_index,
119 if (image_index == -1) {
123 if (image_index != 0) {
125 "JPEG file does not support stack.");
128 check_write_access(rw_mode, image_index);
130 if ((
int) dict[
"nz"] != 1) {
131 LOGERR(
"Only support 2D JPEG file write");
139 EMUtil::getRenderLimits(dict, rendermin, rendermax, renderbits);
141 jpegqual = (int)dict[
"jpeg_quality"];
142 if (jpegqual == 0) jpegqual = 75;
144 cinfo.image_width = (int) dict[
"nx"];
145 cinfo.image_height = (int) dict[
"ny"];
148 cinfo.image_width = (int) area->
size[0];
149 cinfo.image_height = (
int) area->
size[1];
152 cinfo.input_components = 1;
153 cinfo.in_color_space = JCS_GRAYSCALE;
155 jpeg_set_defaults(&cinfo);
156 jpeg_set_quality(&cinfo, jpegqual, TRUE);
180 if (area && (area->
size[0] != cinfo.image_width ||
181 area->
size[1] != cinfo.image_height))
184 int nx = cinfo.image_width, ny = cinfo.image_height;
189 if (renderbits==0 || renderbits>8) renderbits=8;
190 EMUtil::getRenderMinMax(data, nx, ny, rendermin, rendermax, renderbits);
192 unsigned char *cdata = (
unsigned char *)malloc(nx+1);
201 jpeg_start_compress(&cinfo, TRUE);
203 for (
int i=ny-1; i>=0; i--) {
204 for (
int j=0; j<nx; j++) {
205 if (data[i*nx+j] <= rendermin) cdata[j] = 0;
206 else if (data[i*nx+j] >= rendermax) cdata[j] = 255;
207 else cdata[j]= (int)((data[i*nx+j]-rendermin)/
208 (rendermax-rendermin)*256.0);
211 jpeg_write_scanlines(&cinfo, rp, 1);
214 jpeg_finish_compress(&cinfo);
215 jpeg_destroy_compress(&cinfo);
224int JpegIO::get_nimg() {
233bool JpegIO::is_complex_mode()
238bool JpegIO::is_image_big_endian()
Dict is a dictionary to store <string, EMObject> pair.
EMDataType
Image pixel data type used in EMAN.
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
#define ImageReadException(filename, desc)
#define ImageWriteException(imagename, desc)