Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

jpegio.cpp

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #ifdef EM_JPEG
00037 
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include "jpegio.h"
00041 #include "geometry.h"
00042 #include "util.h"
00043 #include <math.h>
00044 
00045 using namespace EMAN;
00046 
00047 JpegIO::JpegIO(const string & file, IOMode rw): filename(file), rw_mode(rw), jpeg_file(0), initialized(false),rendermin(0),rendermax(0)
00048 {}
00049 
00050 JpegIO::~JpegIO()
00051 {
00052         if (jpeg_file) {
00053                 fclose(jpeg_file);
00054                 jpeg_file = 0;
00055         }
00056 
00057 }
00058 
00059 void JpegIO::init()
00060 {
00061         ENTERFUNC;
00062         if (initialized) {
00063                 return;
00064         }
00065 
00066         initialized = true;
00067 
00068         bool is_new_file = false;
00069         jpeg_file = sfopen(filename, rw_mode, &is_new_file, true);
00070 
00071         if (!is_new_file) {
00072                 throw ImageReadException(filename, "JPEG reading not supported");
00073         }
00074         else {
00075                 cinfo.err = jpeg_std_error(&jerr);
00076                 jpeg_create_compress(&cinfo);
00077                 jpeg_stdio_dest(&cinfo, jpeg_file);
00078         }
00079 
00080         rendermin=rendermax=0;
00081         jpegqual=75;
00082         EXITFUNC;
00083 }
00084 
00085 bool JpegIO::is_valid(const void *)
00086 {
00087 //      ENTERFUNC;
00088         return false;
00089 }
00090 
00091 //int JpegIO::read_header(Dict & dict, int image_index, const Region * area, bool)
00092 int JpegIO::read_header(Dict &, int, const Region *, bool)
00093 {
00094         ENTERFUNC;
00095 
00096         throw ImageReadException(filename, "JPEG reading not supported");
00097 
00098         EXITFUNC;
00099         return 0;
00100 }
00101 
00102 int JpegIO::write_header(const Dict & dict, int image_index, const Region* area,
00103                                                 EMUtil::EMDataType, bool)
00104 {
00105         ENTERFUNC;
00106 
00107         //single image format, index can only be zero
00108         if(image_index == -1) {
00109                 image_index = 0;
00110         }
00111         if(image_index != 0) {
00112                 throw ImageWriteException(filename, "JPEG file does not support stack.");
00113         }
00114         check_write_access(rw_mode, image_index);
00115 
00116         if ((int) dict["nz"] != 1) {
00117                 LOGERR("Only support 2D JPEG file write");
00118                 return 1;
00119         }
00120 
00121         rendermin=(float)dict["render_min"];    // float value representing black in the output
00122         rendermax=(float)dict["render_max"];    // float value representign white in the output
00123         jpegqual=(int)dict["jpeg_quality"];
00124         if (jpegqual==0) jpegqual=75;
00125 
00126         cinfo.image_width = (int) dict["nx"];      /* image width and height, in pixels */
00127         cinfo.image_height = (int) dict["ny"];
00128         if (area) {
00129                 cinfo.image_width = (int) area->size[0];
00130                 cinfo.image_height = (int) area->size[1];
00131         }
00132         cinfo.input_components = 1;     /* # of color components per pixel */
00133         cinfo.in_color_space = JCS_GRAYSCALE; /* colorspace of input image */
00134         jpeg_set_defaults(&cinfo);
00135         jpeg_set_quality(&cinfo,jpegqual,true);
00136 
00137         EXITFUNC;
00138         return 0;
00139 }
00140 
00141 int JpegIO::read_data(float *, int, const Region *, bool)
00142 {
00143         ENTERFUNC;
00144 
00145         EXITFUNC;
00146         return 0;
00147 }
00148 
00149 int JpegIO::write_data(float *data, int image_index, const Region* area,
00150                                           EMUtil::EMDataType, bool)
00151 {
00152         ENTERFUNC;
00153 
00154         if (image_index>0) throw ImageWriteException("N/A", "JPEG files are single-image only");
00155         if (area && (area->size[0]!=cinfo.image_width || area->size[1]!=cinfo.image_height))
00156                  throw ImageWriteException("N/A", "No region writing for JPEG images");
00157         int nx=cinfo.image_width,ny=cinfo.image_height;
00158 
00159         // If we didn't get any parameters in 'render_min' or 'render_max', we need to find some good ones
00160         EMUtil::getRenderMinMax(data, nx, ny, rendermin, rendermax);
00161 
00162         unsigned char *cdata=(unsigned char *)malloc(nx+1);
00163 
00166         // convert and write the data 1 scanline at a time
00167         JSAMPROW rp[1];
00168         rp[0]=cdata;
00169         jpeg_start_compress(&cinfo, TRUE);
00170         for (int i=ny-1; i>=0; i--) {
00171                 for (int j=0; j<nx; j++) {
00172                         if (data[i*nx+j]<=rendermin) cdata[j]=0;
00173                         else if (data[i*nx+j]>=rendermax) cdata[j]=255;
00174                         else cdata[j]=(int)((data[i*nx+j]-rendermin)/(rendermax-rendermin)*256.0);
00175                 }
00176                 jpeg_write_scanlines(&cinfo, rp, 1);
00177         }
00178 
00179         jpeg_finish_compress(&cinfo);
00180         jpeg_destroy_compress(&cinfo);
00181 
00182         free(cdata);
00183         EXITFUNC;
00184 
00185         return 0;
00186 }
00187 
00188 int JpegIO::get_nimg() {
00189         return 0;
00190 }
00191 
00192 void JpegIO::flush()
00193 {
00194 
00195 }
00196 
00197 bool JpegIO::is_complex_mode()
00198 {
00199         return false;
00200 }
00201 
00202 bool JpegIO::is_image_big_endian()
00203 {
00204         return true;
00205 }
00206 
00207 
00208 #endif  //EM_JPEG

Generated on Tue Jun 11 13:40:38 2013 for EMAN2 by  doxygen 1.3.9.1