Engauge Digitizer  2
Public Member Functions | List of all members
Jpeg2000 Class Reference

Wrapper around OpenJPEG library, in C, for opening jpeg2000 files. More...

#include <Jpeg2000.h>

Collaboration diagram for Jpeg2000:
Collaboration graph

Public Member Functions

 Jpeg2000 ()
 Single constructor. More...
 
bool load (const QString &filename, QImage &image) const
 Load image from jpeg2000 file. More...
 
QStringList supportedImageWildcards () const
 List the supported jpeg2000 file extensions, for filtering import files. More...
 

Detailed Description

Wrapper around OpenJPEG library, in C, for opening jpeg2000 files.

Definition at line 26 of file Jpeg2000.h.

Constructor & Destructor Documentation

◆ Jpeg2000()

Jpeg2000::Jpeg2000 ( )

Single constructor.

Definition at line 21 of file Jpeg2000.cpp.

22 {
23 }

Member Function Documentation

◆ load()

bool Jpeg2000::load ( const QString &  filename,
QImage &  image 
) const

Load image from jpeg2000 file.

Definition at line 192 of file Jpeg2000.cpp.

194 {
195  LOG4CPP_INFO_S ((*mainCat)) << "Jpeg2000::load"
196  << " filename=" << filename.toLatin1().data();
197 
198  if (invalidFileExtension (filename)) {
199  return false;
200  }
201 
202  opj_dparameters_t parameters;
203  initializeParameters (parameters);
204 
205  parameters.decod_format = inputFormat (filename.toLatin1().data());
206 
207  opj_stream_t *inStream = opj_stream_create_default_file_stream (filename.toLatin1().data(), 1);
208  if (!inStream) {
209  LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load encountered error opening stream";
210  return false;
211  }
212 
213  // Create decoder
214  opj_codec_t *inCodec = decode (parameters.decod_format);
215  if (!inCodec) {
216  LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load encountered error creating decoding stream";
217  opj_stream_destroy (inStream);
218  return false;
219  }
220 
221  // Callbacks for local handling of errors
222  opj_set_info_handler (inCodec, infoCallback, 0);
223  opj_set_warning_handler (inCodec, warningCallback, 0);
224  opj_set_error_handler (inCodec, errorCallback, 0);
225 
226  if (!opj_setup_decoder (inCodec,
227  &parameters)) {
228  LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load encountered error decoding stream";
229  opj_stream_destroy (inStream);
230  opj_destroy_codec (inCodec);
231  return false;
232  }
233 
234  // Read header and, if necessary, the JP2 boxes
235  opj_image_t *image;
236  if (!opj_read_header (inStream,
237  inCodec,
238  &image)) {
239  LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load encountered error reading header";
240  opj_stream_destroy (inStream);
241  opj_destroy_codec (inCodec);
242  opj_image_destroy (image);
243  return false;
244  }
245 
246  // Get the decoded image
247  if (!(opj_decode (inCodec,
248  inStream,
249  image) &&
250  opj_end_decompress (inCodec,
251  inStream))) {
252  LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load failed to decode image";
253  opj_destroy_codec (inCodec);
254  opj_stream_destroy (inStream);
255  opj_image_destroy (image);
256  return false;
257  }
258 
259  // Close the byte stream
260  opj_stream_destroy (inStream);
261 
262  applyImageTweaks (image);
263 
264  // Transform into ppm image in memory
265  bool success = true;
266  QBuffer buffer;
267  buffer.open (QBuffer::WriteOnly);
268  if (imagetopnm (image,
269  buffer)) {
270  LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load failed to generate new image";
271  success = false;
272 
273  } else {
274 
275  // Intermediate file for debugging
276 // QFile file ("jpeg2000.ppm");
277 // file.open (QIODevice::WriteOnly);
278 // file.write (buffer.data());
279 // file.close ();
280 
281  // Create output
282  imageResult.loadFromData(buffer.data());
283 
284  }
285 
286  // Deallocate
287  if (inCodec) {
288  opj_destroy_codec (inCodec);
289  }
290  opj_image_destroy (image);
291 
292  return success;
293 }
void infoCallback(const char *msg, void *)
int imagetopnm(opj_image_t *image, QBuffer &buffer)
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
#define LOG4CPP_ERROR_S(logger)
Definition: convenience.h:12
void warningCallback(const char *msg, void *)
void errorCallback(const char *msg, void *)
log4cpp::Category * mainCat
Definition: Logger.cpp:14

◆ supportedImageWildcards()

QStringList Jpeg2000::supportedImageWildcards ( ) const

List the supported jpeg2000 file extensions, for filtering import files.

Definition at line 305 of file Jpeg2000.cpp.

306 {
307  QStringList extensions = supportedFileExtensions();
308  QStringList wildcards;
309 
310  QStringList::iterator itr;
311  for (itr = extensions.begin(); itr != extensions.end(); itr++) {
312  QString extension = *itr;
313  QString wildcard = QString ("*.%1").arg (extension);
314  wildcards << wildcard;
315  }
316 
317  return wildcards;
318 }

The documentation for this class was generated from the following files: