Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
//#include "texception.h"
Toshihiro Shimizu 890ddd
//#include "tfilepath.h"
Toshihiro Shimizu 890ddd
//#include "tiio_jpg.h"
Toshihiro Shimizu 890ddd
//#include "../compatibility/tnz4.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_jpg.h"
Toshihiro Shimizu 890ddd
#include "tproperty.h"
Toshihiro Shimizu 890ddd
#include "tpixel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Include file for users of JPEG library.
Toshihiro Shimizu 890ddd
 * You will need to have included system headers that define at least
Toshihiro Shimizu 890ddd
 * the typedefs FILE and size_t before you can include jpeglib.h.
Toshihiro Shimizu 890ddd
 * (stdio.h is sufficient on ANSI-conforming systems.)
Toshihiro Shimizu 890ddd
 * You may also wish to include "jerror.h".
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
const std::string Tiio::JpgWriterProperties::QUALITY("Quality");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
extern "C" {
Campbell Barton 8c6c57
static void tnz_error_exit(j_common_ptr cinfo) {
Shinya Kitaoka 120a6e
  //  throw "merda";
Toshihiro Shimizu 890ddd
}
Campbell Barton 8c6c57
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef CICCIO
Toshihiro Shimizu 890ddd
JMETHOD(void, error_exit, (j_common_ptr cinfo));
Toshihiro Shimizu 890ddd
/* Conditionally emit a trace or warning message */
Toshihiro Shimizu 890ddd
JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level));
Toshihiro Shimizu 890ddd
/* Routine that actually outputs a trace or error message */
Toshihiro Shimizu 890ddd
JMETHOD(void, output_message, (j_common_ptr cinfo));
Toshihiro Shimizu 890ddd
/* Format a message string for the most recent JPEG error or message */
Toshihiro Shimizu 890ddd
JMETHOD(void, format_message, (j_common_ptr cinfo, char *buffer));
Toshihiro Shimizu 890ddd
#define JMSG_LENGTH_MAX 200 /* recommended size of format_message buffer */
Toshihiro Shimizu 890ddd
/* Reset error state variables at start of a new image */
Toshihiro Shimizu 890ddd
JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo));
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace Tiio;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
JpgReader::JpgReader() : m_chan(0), m_isOpen(false) {
Shinya Kitaoka 120a6e
  memset(&m_cinfo, 0, sizeof m_cinfo);
Shinya Kitaoka 120a6e
  memset(&m_jerr, 0, sizeof m_jerr);
Shinya Kitaoka 120a6e
  memset(&m_buffer, 0, sizeof m_buffer);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
JpgReader::~JpgReader() {
Shinya Kitaoka 120a6e
  if (m_isOpen) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      jpeg_finish_decompress(&m_cinfo);
Shinya Kitaoka 120a6e
      jpeg_destroy_decompress(&m_cinfo);
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_chan) {
Shinya Kitaoka 120a6e
    m_chan = 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Tiio::RowOrder JpgReader::getRowOrder() const { return Tiio::TOP2BOTTOM; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void JpgReader::open(FILE *file) {
Shinya Kitaoka 120a6e
  m_cinfo.err             = jpeg_std_error(&m_jerr);
Shinya Kitaoka 120a6e
  m_cinfo.err->error_exit = tnz_error_exit;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  jpeg_create_decompress(&m_cinfo);
Shinya Kitaoka 120a6e
  m_chan = file;
Shinya Kitaoka 120a6e
  jpeg_stdio_src(&m_cinfo, m_chan);
Shinya Kitaoka 120a6e
  bool ret = jpeg_read_header(&m_cinfo, TRUE);
Shinya Kitaoka 120a6e
  ret      = ret && jpeg_start_decompress(&m_cinfo);
Shinya Kitaoka 120a6e
  if (!ret) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int row_stride = m_cinfo.output_width * m_cinfo.output_components;
Shinya Kitaoka 120a6e
  m_buffer = (*m_cinfo.mem->alloc_sarray)((j_common_ptr)&m_cinfo, JPOOL_IMAGE,
Shinya Kitaoka 120a6e
                                          row_stride, 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_info.m_lx             = m_cinfo.output_width;
Shinya Kitaoka 120a6e
  m_info.m_ly             = m_cinfo.output_height;
Shinya Kitaoka 120a6e
  m_info.m_samplePerPixel = 3;
Shinya Kitaoka 120a6e
  m_info.m_valid          = true;
Shinya Kitaoka 120a6e
  m_isOpen                = true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void JpgReader::readLine(char *buffer, int x0, int x1, int shrink) {
Shinya Kitaoka 120a6e
  if (m_cinfo.out_color_space == JCS_RGB && m_cinfo.out_color_components == 3) {
Shinya Kitaoka 120a6e
    int ret = jpeg_read_scanlines(&m_cinfo, m_buffer, 1);
Shinya Kitaoka 120a6e
    assert(ret == 1);
Shinya Kitaoka 120a6e
    unsigned char *src = m_buffer[0];
Shinya Kitaoka 120a6e
    TPixel32 *dst      = (TPixel32 *)buffer;
Shinya Kitaoka 120a6e
    dst += x0;
Shinya Kitaoka 120a6e
    src += 3 * x0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int width           = (m_cinfo.output_width - 1) / shrink + 1;
Shinya Kitaoka 120a6e
    if (x1 >= x0) width = (x1 - x0) / shrink + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (--width >= 0) {
Shinya Kitaoka 120a6e
      dst->r = src[0];
Shinya Kitaoka 120a6e
      dst->g = src[1];
Shinya Kitaoka 120a6e
      dst->b = src[2];
Shinya Kitaoka 120a6e
      dst->m = (char)255;
Shinya Kitaoka 120a6e
      src += 3 * shrink;
Shinya Kitaoka 120a6e
      dst += shrink;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (m_cinfo.out_color_components == 1) {
Shinya Kitaoka 120a6e
    int ret = jpeg_read_scanlines(&m_cinfo, m_buffer, 1);
Shinya Kitaoka 120a6e
    assert(ret == 1);
Shinya Kitaoka 120a6e
    unsigned char *src = m_buffer[0];
Shinya Kitaoka 120a6e
    TPixel32 *dst      = (TPixel32 *)buffer;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    dst += x0;
Shinya Kitaoka 120a6e
    src += x0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int width           = (m_cinfo.output_width - 1) / shrink + 1;
Shinya Kitaoka 120a6e
    if (x1 >= x0) width = (x1 - x0) / shrink + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (--width >= 0) {
Shinya Kitaoka 120a6e
      dst->r = *src;
Shinya Kitaoka 120a6e
      dst->g = *src;
Shinya Kitaoka 120a6e
      dst->b = *src;
Shinya Kitaoka 120a6e
      dst->m = (char)255;
Shinya Kitaoka 120a6e
      src += shrink;
Shinya Kitaoka 120a6e
      dst += shrink;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int JpgReader::skipLines(int lineCount) {
Shinya Kitaoka 120a6e
  for (int i = 0; i < lineCount; i++) {
Shinya Kitaoka 120a6e
    int ret = jpeg_read_scanlines(&m_cinfo, m_buffer, 1);
Shinya Kitaoka 120a6e
    assert(ret == 1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return lineCount;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class JpgWriter final : public Tiio::Writer {
Shinya Kitaoka 120a6e
  struct jpeg_compress_struct m_cinfo;
Shinya Kitaoka 120a6e
  struct jpeg_error_mgr m_jerr;
Shinya Kitaoka 120a6e
  FILE *m_chan;
Shinya Kitaoka 120a6e
  JSAMPARRAY m_buffer;
Shinya Kitaoka 120a6e
  bool m_headerWritten;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  JpgWriter() : m_chan(0), m_headerWritten(false) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void open(FILE *file, const TImageInfo &info) override {
Shinya Kitaoka 120a6e
    m_cinfo.err = jpeg_std_error(&m_jerr);
Shinya Kitaoka 120a6e
    jpeg_create_compress(&m_cinfo);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_cinfo.image_width      = info.m_lx;
Shinya Kitaoka 120a6e
    m_cinfo.image_height     = info.m_ly;
Shinya Kitaoka 120a6e
    m_cinfo.input_components = 3;
Shinya Kitaoka 120a6e
    m_cinfo.in_color_space   = JCS_RGB;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    jpeg_set_defaults(&m_cinfo);
Shinya Kitaoka 120a6e
    if (!m_properties) m_properties = new Tiio::JpgWriterProperties();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    jpeg_set_quality(
Shinya Kitaoka 120a6e
        &m_cinfo,
Shinya Kitaoka 120a6e
        ((TIntProperty *)(m_properties->getProperty("Quality")))->getValue(),
Shinya Kitaoka 120a6e
        TRUE);
Shinya Kitaoka 120a6e
    m_cinfo.smoothing_factor =
Shinya Kitaoka 120a6e
        ((TIntProperty *)(m_properties->getProperty("Smoothing")))->getValue();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int row_stride = m_cinfo.image_width * m_cinfo.input_components;
Shinya Kitaoka 120a6e
    m_buffer = (*m_cinfo.mem->alloc_sarray)((j_common_ptr)&m_cinfo, JPOOL_IMAGE,
Shinya Kitaoka 120a6e
                                            row_stride, 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_chan = file;
Shinya Kitaoka 120a6e
    jpeg_stdio_dest(&m_cinfo, m_chan);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~JpgWriter() {
Shinya Kitaoka 120a6e
    jpeg_finish_compress(&m_cinfo);
Shinya Kitaoka 120a6e
    jpeg_destroy_compress(&m_cinfo);
Shinya Kitaoka 120a6e
    delete m_properties;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void flush() override { fflush(m_chan); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  Tiio::RowOrder getRowOrder() const override { return Tiio::TOP2BOTTOM; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void writeLine(char *buffer) override {
Shinya Kitaoka 120a6e
    if (!m_headerWritten) {
Shinya Kitaoka 120a6e
      m_headerWritten = true;
Shinya Kitaoka 120a6e
      jpeg_start_compress(&m_cinfo, TRUE);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    TPixel32 *src      = (TPixel32 *)buffer;
Shinya Kitaoka 120a6e
    unsigned char *dst = m_buffer[0];
Shinya Kitaoka 120a6e
    int lx             = m_cinfo.image_width;
Shinya Kitaoka 120a6e
    while (--lx >= 0) {
Shinya Kitaoka 120a6e
      dst[0] = src->r;
Shinya Kitaoka 120a6e
      dst[1] = src->g;
Shinya Kitaoka 120a6e
      dst[2] = src->b;
Shinya Kitaoka 120a6e
      dst += 3;
Shinya Kitaoka 120a6e
      ++src;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    jpeg_write_scanlines(&m_cinfo, m_buffer, 1);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----
Toshihiro Shimizu 890ddd
//----
Toshihiro Shimizu 890ddd
//----
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tiio::Reader *Tiio::makeJpgReader() { return new JpgReader(); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tiio::Writer *Tiio::makeJpgWriter() { return new JpgWriter(); }