|
degate 0.1.1
|
The JPEGReader parses jpeg images. More...
#include <JPEGReader.h>

Public Member Functions | |
| JPEGReader (std::string const &filename) | |
| ~JPEGReader () | |
| bool | read () |
| Read the image ot at least its meta data. | |
| bool | get_image (std::tr1::shared_ptr< ImageType >) |
| Read the file content into image. | |
Private Attributes | |
| unsigned char * | image_buffer |
| int | depth |
The JPEGReader parses jpeg images.
Definition at line 40 of file JPEGReader.h.
| degate::JPEGReader< ImageType >::JPEGReader | ( | std::string const & | filename | ) | [inline] |
Definition at line 55 of file JPEGReader.h.
:
ImageReaderBase<ImageType>(filename),
image_buffer(NULL) {}
| 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);
}
| bool degate::JPEGReader< ImageType >::get_image | ( | std::tr1::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;
}
| 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.
Implements degate::ImageReaderBase< ImageType >.
Definition at line 71 of file JPEGReader.h.
{
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;
}

int degate::JPEGReader< ImageType >::depth [private] |
Definition at line 45 of file JPEGReader.h.
unsigned char* degate::JPEGReader< ImageType >::image_buffer [private] |
Definition at line 44 of file JPEGReader.h.
Referenced by degate::JPEGReader< ImageType >::~JPEGReader().
1.7.4