vil_jpeg_compressor.cxx
Go to the documentation of this file.
1 // This is core/vil/file_formats/vil_jpeg_compressor.cxx
2 //:
3 // \file
4 // \author fsm
5 // \verbatim
6 // Modifications
7 // 11 Oct 2002 Ian Scott - converted to vil
8 //\endverbatim
9 
10 #include <iostream>
11 #include "vil_jpeg_compressor.h"
13 #include <vil/vil_stream.h>
14 #ifdef _MSC_VER
15 # include <vcl_msvc_warnings.h>
16 #endif
17 #include <vxl_config.h>
18 
20  : stream(s)
21  , ready(false), quality(75)
22 {
23  stream->ref();
24 
25  // setup the standard error handler in the jpeg library
26  jobj.err = jpeg_std_error(&jerr);
27 
28  // Zero just in case..
29  jobj.next_scanline = 0;
30 
31  // construct the compression object :
32  jpeg_create_compress(&jobj);
33 
34  // Increase the amount of memory that can be used.
35  // Default (1Mb) was too small.
36 #if defined(VXL_ADDRESS_BITS) && VXL_ADDRESS_BITS == 32
37  jobj.mem->max_memory_to_use = 300 * 1024 * 1024;
38 #elif defined(VXL_ADDRESS_BITS) && VXL_ADDRESS_BITS == 64
39  jobj.mem->max_memory_to_use = 1024 * 1024 * 1024;
40 #else
41  /* use the default memory settings */
42 #endif
43 
44  // set the data destination
46 }
47 
48 bool vil_jpeg_compressor::write_scanline(unsigned line, JSAMPLE const *scanline)
49 {
50  if (!ready) {
51  // rewind the stream
53 
54  //
55  jobj.next_scanline = 0;
56 
57  // set colorspace of input image. FIXME.
58  switch (jobj.input_components) {
59  case 1:
60  jobj.in_color_space = JCS_GRAYSCALE;
61  break;
62  case 3:
63  jobj.in_color_space = JCS_RGB;
64  break;
65  default:
66  std::cerr << __FILE__ " : urgh!\n";
67  return false;
68  }
69 
70  jpeg_set_defaults(&jobj);
71  jpeg_set_quality(&jobj, quality, TRUE);
72 
73  // start compression
74  jpeg_boolean write_all_tables = TRUE;
75  jpeg_start_compress (&jobj, write_all_tables);
76 
77  //
78  ready = true;
79  }
80 
81  //
82  if (line != jobj.next_scanline) {
83  std::cerr << "scanlines must be written in order\n";
84  return false;
85  }
86 
87  // write the scanline
88  { auto *tmp = const_cast<JSAMPLE*>(scanline);
89  jpeg_write_scanlines(&jobj, &tmp, 1); }
90 
91  // finish if the last scanline is written
92  if (line == jobj.image_height - 1) {
93  jpeg_finish_compress(&jobj);
94  ready = false;
95  }
96 
97  return true;
98 }
99 
101 {
102  // finish compression if necessary
103  if (ready)
104  jpeg_finish_compress(&jobj);
105 
106  // destroy the compression object
107  jpeg_destroy_compress(&jobj);
108 
109  //
110  stream->unref();
111  stream = nullptr;
112 }
113 
115 {
116  quality = q;
117 }
118 
120 {
121  return quality;
122 }
Stream interface for VIL image loaders.
void vil_jpeg_stream_dst_rewind(j_compress_ptr cinfo, vil_stream *vs)
bool write_scanline(unsigned line, JSAMPLE const *)
struct jpeg_error_mgr jerr
void vil_jpeg_stream_dst_set(j_compress_ptr cinfo, vil_stream *vs)
Prepare for output to a vil_stream.
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
void set_quality(int quality)
vil_jpeg_compressor(vil_stream *s)
void ref()
up/down the reference count.
Definition: vil_stream.h:45
void unref()
Definition: vil_stream.cxx:31
struct jpeg_compress_struct jobj