Go to the documentation of this file.00001
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "vil_jpeg_decompressor.h"
00014 #include "vil_jpeg_source_mgr.h"
00015 #include <vil/vil_stream.h>
00016 #include <vcl_iostream.h>
00017 #include <vxl_config.h>
00018
00019 #define trace if (true) { } else vcl_cerr
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 vil_jpeg_decompressor::vil_jpeg_decompressor(vil_stream *s)
00036 : stream(s)
00037 , ready(false)
00038 , valid(false)
00039 , biffer(0)
00040 {
00041 stream->ref();
00042
00043
00044 jobj.err = jpeg_std_error(&jerr);
00045
00046
00047 jpeg_create_decompress(&jobj);
00048
00049
00050
00051 #if defined(VXL_ADDRESS_BITS) && VXL_ADDRESS_BITS == 32
00052 jobj.mem->max_memory_to_use = 300 * 1024 * 1024;
00053 #elif defined(VXL_ADDRESS_BITS) && VXL_ADDRESS_BITS == 64
00054 jobj.mem->max_memory_to_use = 1024 * 1024 * 1024;
00055 #else
00056
00057 #endif
00058
00059
00060
00061
00062 vil_jpeg_stream_src_set(&jobj, stream);
00063
00064
00065 vil_jpeg_stream_src_rewind(&jobj, stream);
00066
00067
00068 jpeg_read_header(&jobj, TRUE);
00069
00070
00071
00072 #if 1
00073
00074 jpeg_start_decompress(&jobj);
00075
00076
00077 jpeg_abort_decompress(&jobj);
00078 #endif
00079 }
00080
00081
00082 JSAMPLE const *vil_jpeg_decompressor::read_scanline(unsigned line)
00083 {
00084
00085 if (valid && line == jobj.output_scanline-1)
00086 return biffer;
00087
00088 if (ready && line<jobj.output_scanline) {
00089 trace << "...aborting\n";
00090
00091 jpeg_abort_decompress(&jobj);
00092
00093
00094 ready = false;
00095 valid = false;
00096 }
00097
00098 if (!ready) {
00099 trace << "...restarting\n";
00100
00101
00102 vil_jpeg_stream_src_rewind(&jobj, stream);
00103
00104
00105 jpeg_read_header(&jobj, TRUE);
00106
00107
00108 jpeg_start_decompress(&jobj);
00109
00110
00111 ready = true;
00112 valid = false;
00113 }
00114
00115
00116 if (!biffer) {
00117 trace << "...allocate buffer\n";
00118 unsigned row_size = jobj.output_width * jobj.output_components;
00119 biffer = new JSAMPLE[row_size];
00120 }
00121
00122
00123 #if 0
00124 JSAMPLE *buffer[1] = { biffer };
00125 #else
00126 JSAMPARRAY buffer = &biffer;
00127 #endif
00128
00129
00130 while (jobj.output_scanline <= line) {
00131 if (jpeg_read_scanlines(&jobj, buffer, 1) != 1) {
00132 jpeg_abort_decompress(&jobj);
00133 ready = false;
00134 valid = false;
00135 return 0;
00136 }
00137 }
00138
00139
00140 if (jobj.output_scanline >= jobj.image_height) {
00141 trace << "...reached end\n";
00142 jpeg_finish_decompress(&jobj);
00143 ready = false;
00144 }
00145
00146
00147 valid = true;
00148 return biffer;
00149 }
00150
00151
00152 vil_jpeg_decompressor::~vil_jpeg_decompressor()
00153 {
00154
00155 (*jobj.mem->free_pool) ((j_common_ptr) &jobj, JPOOL_IMAGE);
00156
00157
00158 jpeg_destroy_decompress(&jobj);
00159
00160
00161 stream->unref();
00162 stream = 0;
00163
00164
00165 if (biffer)
00166 delete [] biffer;
00167 biffer = 0;
00168 }