degate  0.1.2
Public Member Functions | Private Attributes
degate::JPEGReader< ImageType > Class Template Reference

The JPEGReader parses jpeg images. More...

#include <JPEGReader.h>

Inheritance diagram for degate::JPEGReader< ImageType >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 JPEGReader (std::string const &filename)
 ~JPEGReader ()
bool read ()
 Read the image ot at least its meta data.
bool get_image (std::shared_ptr< ImageType >)
 Read the file content into image.

Private Attributes

unsigned char * image_buffer
int depth

Detailed Description

template<class ImageType>
class degate::JPEGReader< ImageType >

The JPEGReader parses jpeg images.

Definition at line 40 of file JPEGReader.h.


Constructor & Destructor Documentation

template<class ImageType >
degate::JPEGReader< ImageType >::JPEGReader ( std::string const &  filename) [inline]

Definition at line 55 of file JPEGReader.h.

                                          :
      ImageReaderBase<ImageType>(filename),
      image_buffer(NULL) {}
template<class ImageType >
degate::JPEGReader< ImageType >::~JPEGReader ( ) [inline]

Definition at line 59 of file JPEGReader.h.

References degate::JPEGReader< ImageType >::image_buffer.

                  {
      if(image_buffer != NULL) free(image_buffer);
    }

Member Function Documentation

template<class ImageType >
bool degate::JPEGReader< ImageType >::get_image ( std::shared_ptr< ImageType >  img) [virtual]

Read the file content into image.

Implements degate::ImageReaderBase< ImageType >.

Definition at line 129 of file JPEGReader.h.

References MERGE_CHANNELS.

                                                                    {

    if(img == NULL) return false;

    for(unsigned int y = 0; y < get_height(); y++) {
      for(unsigned int x = 0; x < get_width(); x++) {

        uint8_t v1, v2, v3;
        if(depth == 1) {
          v1 = v2 = v3 = image_buffer[(y * get_width() + x)];
        }
        else if(depth == 3) {
          v1 = image_buffer[depth * (y * get_width() + x)];
          v2 = image_buffer[depth * (y * get_width() + x) + 1];
          v3 = image_buffer[depth * (y * get_width() + x) + 2];
        }
        else throw std::runtime_error("Unexpected number of channels in JPG file.");

        img->set_pixel(x, y, MERGE_CHANNELS(v1, v2, v3, 0xff));

      }
    }

    return true;
  }
template<class ImageType >
bool degate::JPEGReader< ImageType >::read ( ) [virtual]

Read the image ot at least its meta data.

If you derive from class ImageReaderBase, you can implement a full image read operation here. But it is also possible to implement just the read of meta data, such as width and height.

Returns:
The function returns true, if the image file was read. Else false is returned. If read() was successful you can

Implements degate::ImageReaderBase< ImageType >.

Definition at line 71 of file JPEGReader.h.

References debug(), and TM.

                                   {

    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE * infile;     /* source file */
    depth = 0;

    JSAMPARRAY buffer; /* Output row buffer */
    int row_stride;    /* physical row width in output buffer */
    unsigned int p = 0;

    if ((infile = fopen(get_filename().c_str(), "rb")) == NULL) {
      debug(TM, "can't open %s\n", get_filename().c_str());
      return false;
    }

    cinfo.err = jpeg_std_error(&jerr);

    jpeg_create_decompress(&cinfo);
    jpeg_stdio_src(&cinfo, infile);

    jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress(&cinfo);

    set_width(cinfo.output_width);
    set_height(cinfo.output_height);
    depth = cinfo.num_components;

    debug(TM, "Reading image with size: %d x %d", get_width(), get_height());

    image_buffer = (unsigned char *)malloc(depth * get_width() * get_height());

    if(image_buffer != NULL) {

      row_stride = cinfo.output_width * cinfo.output_components;
      buffer = (*cinfo.mem->alloc_sarray)
        ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

      printf("Row stride is %d\n",row_stride);
      while (cinfo.output_scanline < cinfo.output_height) {
        jpeg_read_scanlines(&cinfo, buffer, 1);

        memcpy(&image_buffer[p],buffer[0],row_stride);
        p+=row_stride;
        //        put_scanline_someplace(buffer[0], row_stride);
      }

      printf("Image read\n");
    }

    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);

    return true;
  }

Here is the call graph for this function:


Member Data Documentation

template<class ImageType >
int degate::JPEGReader< ImageType >::depth [private]

Definition at line 45 of file JPEGReader.h.

template<class ImageType >
unsigned char* degate::JPEGReader< ImageType >::image_buffer [private]

Definition at line 44 of file JPEGReader.h.

Referenced by degate::JPEGReader< ImageType >::~JPEGReader().


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