Code Search for Developers
 
 
  

raster-jpeg.cxx from Boson at Krugle


Show raster-jpeg.cxx syntax highlighted

/************************************************************************

  JPEG image file format support.

  The I/O code in this file was originally based on the example.c
  skeleton code distributed with the JPEG library (release 6b).

  $Id: raster-jpeg.cxx 7782 2006-10-04 20:49:27Z abmann $

 ************************************************************************/

#ifndef QT_CLEAN_NAMESPACE
#define QT_CLEAN_NAMESPACE
#endif

#include <gfx/gfx.h>
#include <gfx/raster.h>

// Quality factors are expressed on a 0--100 scale
int jpeg_output_quality = 100;

#ifdef HAVE_LIBJPEG

#include <stdio.h>
extern "C" {
#include <jpeglib.h>
}

bool write_jpeg_image(const char *filename, const ByteRaster& img)
{
    FILE *outfile = fopen(filename, "wb");
    if( !outfile ) return false;

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);

    cinfo.image_width = img.width();
    cinfo.image_height = img.height();
    cinfo.input_components = img.channels();

    if(img.channels()==1)	cinfo.in_color_space = JCS_GRAYSCALE;
    else if(img.channels()==3)	cinfo.in_color_space = JCS_RGB;
    else			cinfo.in_color_space = JCS_UNKNOWN;
    
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, jpeg_output_quality, TRUE);

    jpeg_start_compress(&cinfo, TRUE);

    int row_stride = img.width() * img.channels();
    const unsigned char *scanline = img.head();
    while( cinfo.next_scanline < cinfo.image_height )
    {
	(void) jpeg_write_scanlines(&cinfo, (JSAMPLE **)&scanline, 1);
	scanline += row_stride;
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
    fclose(outfile);

    return true;
}

ByteRaster *read_jpeg_image(const char *filename)
{
    FILE *infile = fopen(filename, "rb");
    if( !infile )  return NULL;

    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    cinfo.err = jpeg_std_error(&jerr);

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

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

    ByteRaster *img = new ByteRaster(cinfo.output_width,
				     cinfo.output_height,
				     cinfo.output_components);

    int row_stride = cinfo.output_width * cinfo.output_components;
    unsigned char *scanline = img->head();
    while( cinfo.output_scanline < cinfo.output_height)
    {
	(void) jpeg_read_scanlines(&cinfo, (JSAMPLE **)&scanline, 1);
	scanline += row_stride;
    }

    (void) jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);
    return img;
}

#else

bool write_jpeg_image(const char *, const ByteRaster&) { return false; }
ByteRaster *read_jpeg_image(const char *) { return NULL; }

#endif




See more files for this project here

Boson

Boson is an OpenGL real-time strategy game. It is designed to run on Unix (Linux) computers, and is built on top of the KDE, Qt and kdegames libraries.

Project homepage: http://sourceforge.net/projects/boson
Programming language(s): C,C++
License: other

  gfx/
    arcball.h
    array.h
    baseball.h
    geom3d.h
    geom4d.h
    gfx.h
    gl.h
    glext.h
    gltools.h
    gui.h
    intvec.h
    mat2.h
    mat3.h
    mat4.h
    mfc.h
    quat.h
    raster.h
    script.h
    symmat3.h
    symmat4.h
    trackball.h
    vec2.h
    vec3.h
    vec4.h
    wintools.h
  CMakeLists.txt
  arcball.cxx
  baseball.cxx
  config-libgfx.h.cmake
  geom3d.cxx
  geom4d.cxx
  gltools.cxx
  gui.cxx
  mat2.cxx
  mat3.cxx
  mat4.cxx
  quat.cxx
  raster-jpeg.cxx
  raster-png.cxx
  raster-pnm.cxx
  raster-tiff.cxx
  raster.cxx
  script.cxx
  symmat3.cxx
  symmat4.cxx
  time.cxx
  trackball.cxx
  wintools.cxx