core/vil1/file_formats/vil1_jpeg_compressor.cxx

Go to the documentation of this file.
00001 // This is core/vil1/file_formats/vil1_jpeg_compressor.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "vil1_jpeg_compressor.h"
00010 #include "vil1_jpeg_destination_mgr.h"
00011 #include <vil1/vil1_stream.h>
00012 #include <vcl_iostream.h>
00013 
00014 vil1_jpeg_compressor::vil1_jpeg_compressor(vil1_stream *s)
00015   : stream(s)
00016   , ready(false)
00017 {
00018   stream->ref();
00019 
00020   // setup the standard error handler in the jpeg library
00021   jobj.err = jpeg_std_error(&jerr);
00022 
00023   // Zero just in case..
00024   jobj.next_scanline = 0;
00025 
00026   // construct the compression object :
00027   jpeg_create_compress(&jobj);
00028 
00029   // set the data destination
00030   vil1_jpeg_stream_dst_set(&jobj, stream);
00031 }
00032 
00033 bool vil1_jpeg_compressor::write_scanline(unsigned line, JSAMPLE const *scanline) {
00034   if (!ready) {
00035     // rewind the stream
00036     vil1_jpeg_stream_dst_rewind(&jobj, stream);
00037 
00038     //
00039     jobj.next_scanline = 0;
00040 
00041     // set colorspace of input image. FIXME.
00042     switch (jobj.input_components) {
00043     case 1:
00044       jobj.in_color_space = JCS_GRAYSCALE;
00045       break;
00046     case 3:
00047       jobj.in_color_space = JCS_RGB;
00048       break;
00049     default:
00050       vcl_cerr << __FILE__ " : urgh!\n";
00051       return false;
00052     }
00053 
00054     jpeg_set_defaults(&jobj);
00055 
00056     // start compression
00057     bool write_all_tables = true;
00058     jpeg_start_compress (&jobj, write_all_tables);
00059 
00060     //
00061     ready = true;
00062   }
00063 
00064   //
00065   if (line != jobj.next_scanline) {
00066     vcl_cerr << "scanlines must be written in order\n";
00067     return false;
00068   }
00069 
00070   // write the scanline
00071   { JSAMPLE *tmp = const_cast<JSAMPLE*>(scanline);
00072   jpeg_write_scanlines(&jobj, &tmp, 1); }
00073 
00074   // finish if the last scanline is written
00075   if (line == jobj.image_height - 1) {
00076     jpeg_finish_compress(&jobj);
00077     ready = false;
00078   }
00079 
00080   return true;
00081 }
00082 
00083 vil1_jpeg_compressor::~vil1_jpeg_compressor() {
00084   // finish compression if necessary
00085   if (ready)
00086     jpeg_finish_compress(&jobj);
00087 
00088   // destroy the compression object
00089   jpeg_destroy_compress(&jobj);
00090 
00091   //
00092   stream->unref();
00093   stream = 0;
00094 }
00095 

Generated on Sat Nov 22 05:08:28 2008 for core/vil1 by  doxygen 1.5.1