Move to new Build System
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
#include "JpegImage.h"
|
||||
|
||||
|
||||
namespace Image {
|
||||
|
||||
const static JOCTET EOI_BUFFER[1] = { JPEG_EOI };
|
||||
|
||||
JpegImage::JpegImage()
|
||||
{
|
||||
}
|
||||
|
||||
void JpegImage::SetImageData(const std::string& data)
|
||||
{
|
||||
DecompressImage(data.c_str(), data.size());
|
||||
}
|
||||
|
||||
unsigned int JpegImage::Width() const
|
||||
{
|
||||
return m_width;
|
||||
}
|
||||
|
||||
unsigned int JpegImage::Height() const
|
||||
{
|
||||
return m_height;
|
||||
}
|
||||
|
||||
unsigned int JpegImage::StrideX() const
|
||||
{
|
||||
return m_strideX;
|
||||
}
|
||||
|
||||
unsigned int JpegImage::StrideY() const
|
||||
{
|
||||
return m_strideY;
|
||||
}
|
||||
|
||||
const std::vector<char>& JpegImage::Data() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
void JpegImage::InitSource(j_decompress_ptr pCinfo)
|
||||
{
|
||||
}
|
||||
|
||||
boolean JpegImage::FillInputBuffer(j_decompress_ptr pCinfo)
|
||||
{
|
||||
SourceMgrPtr pSrc = reinterpret_cast<SourceMgrPtr>(pCinfo->src);
|
||||
pSrc->pub.next_input_byte = EOI_BUFFER;
|
||||
pSrc->pub.bytes_in_buffer = 1;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void JpegImage::SkipInputData(j_decompress_ptr pCinfo, long numBytes)
|
||||
{
|
||||
SourceMgrPtr pSrc = reinterpret_cast<SourceMgrPtr>(pCinfo->src);
|
||||
if (pSrc->pub.bytes_in_buffer < static_cast<size_t>(numBytes))
|
||||
{
|
||||
pSrc->pub.next_input_byte = EOI_BUFFER;
|
||||
pSrc->pub.bytes_in_buffer = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
pSrc->pub.next_input_byte += numBytes;
|
||||
pSrc->pub.bytes_in_buffer -= numBytes;
|
||||
}
|
||||
}
|
||||
|
||||
void JpegImage::TermSource(j_decompress_ptr pCinfo)
|
||||
{
|
||||
}
|
||||
|
||||
void JpegImage::SetSourceMgr(j_decompress_ptr pCinfo, const char* pData, size_t len) const
|
||||
{
|
||||
SourceMgrPtr pSrc;
|
||||
if (pCinfo->src == 0)
|
||||
pCinfo->src = (struct jpeg_source_mgr *)(*pCinfo->mem->alloc_small)(reinterpret_cast<j_common_ptr>(pCinfo), JPOOL_PERMANENT, sizeof(SourceMgr));
|
||||
|
||||
pSrc = reinterpret_cast<SourceMgrPtr>(pCinfo->src);
|
||||
pSrc->pub.init_source = JpegImage::InitSource;
|
||||
pSrc->pub.fill_input_buffer = JpegImage::FillInputBuffer;
|
||||
pSrc->pub.skip_input_data = JpegImage::SkipInputData;
|
||||
pSrc->pub.resync_to_restart = jpeg_resync_to_restart;
|
||||
pSrc->pub.term_source = JpegImage::TermSource;
|
||||
|
||||
pSrc->data = (const JOCTET *)pData;
|
||||
pSrc->len = len;
|
||||
pSrc->pub.bytes_in_buffer = len;
|
||||
pSrc->pub.next_input_byte = pSrc->data;
|
||||
}
|
||||
|
||||
void JpegImage::DecompressImage(const char* pData, size_t len)
|
||||
{
|
||||
struct jpeg_decompress_struct cinfo;
|
||||
struct jpeg_error_mgr jerr;
|
||||
|
||||
jerr.trace_level = 0;
|
||||
cinfo.err = jpeg_std_error(&jerr);
|
||||
jpeg_create_decompress(&cinfo);
|
||||
SetSourceMgr(&cinfo, pData, len);
|
||||
|
||||
jpeg_read_header(&cinfo, TRUE);
|
||||
jpeg_start_decompress(&cinfo);
|
||||
|
||||
m_width = cinfo.image_width;
|
||||
m_height = cinfo.image_height;
|
||||
m_strideX = cinfo.num_components;
|
||||
m_strideY = m_width * cinfo.num_components;
|
||||
|
||||
m_data.resize(m_height * m_strideY);
|
||||
|
||||
JSAMPROW scanlines[1];
|
||||
while (cinfo.output_scanline < cinfo.image_height)
|
||||
{
|
||||
scanlines[0] = reinterpret_cast<JSAMPROW>(&m_data[cinfo.output_scanline * m_strideY]);
|
||||
jpeg_read_scanlines(&cinfo, scanlines, 1);
|
||||
}
|
||||
|
||||
jpeg_finish_decompress(&cinfo);
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
}
|
||||
|
||||
} // namespace Image
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#ifndef JPEGIMAGE_H
|
||||
#define JPEGIMAGE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <jpeglib.h>
|
||||
|
||||
|
||||
namespace Image {
|
||||
|
||||
class JpegImage
|
||||
{
|
||||
public:
|
||||
JpegImage();
|
||||
|
||||
void SetImageData(const std::string& data);
|
||||
|
||||
unsigned int Width() const;
|
||||
unsigned int Height() const;
|
||||
unsigned int StrideX() const;
|
||||
unsigned int StrideY() const;
|
||||
const std::vector<char>& Data() const;
|
||||
|
||||
private:
|
||||
struct SourceMgr
|
||||
{
|
||||
struct jpeg_source_mgr pub;
|
||||
const JOCTET *data;
|
||||
size_t len;
|
||||
};
|
||||
typedef SourceMgr* SourceMgrPtr;
|
||||
|
||||
private:
|
||||
static void InitSource(j_decompress_ptr pCinfo);
|
||||
static boolean FillInputBuffer(j_decompress_ptr pCinfo);
|
||||
static void SkipInputData(j_decompress_ptr pCinfo, long numBytes);
|
||||
static void TermSource(j_decompress_ptr pCinfo);
|
||||
|
||||
void SetSourceMgr(j_decompress_ptr pCinfo, const char* pData, size_t len) const;
|
||||
|
||||
void DecompressImage(const char* pData, size_t len);
|
||||
|
||||
private:
|
||||
unsigned int m_width;
|
||||
unsigned int m_height;
|
||||
unsigned int m_strideX;
|
||||
unsigned int m_strideY;
|
||||
|
||||
std::vector<char> m_data;
|
||||
};
|
||||
|
||||
} // namespace Image
|
||||
|
||||
#endif // JPEGIMAGE_H
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <stdio.h>
|
||||
#include "JpegImageCollection.h"
|
||||
|
||||
|
||||
namespace Image {
|
||||
|
||||
JpegImageCollection::JpegImageCollection(const std::vector<JpegImage>& images) :
|
||||
m_width(0),
|
||||
m_height(0),
|
||||
m_strideX(0),
|
||||
m_strideY(0)
|
||||
{
|
||||
if (images.size() == 0)
|
||||
return;
|
||||
|
||||
m_width = images[0].Width();
|
||||
m_strideX = images[0].StrideX();
|
||||
m_strideY = images[0].StrideY();
|
||||
|
||||
size_t height = images[0].Height();
|
||||
m_height = height * images.size();
|
||||
m_imageData.resize(m_strideY * m_height);
|
||||
|
||||
std::vector<char>::iterator outIterator = m_imageData.begin();
|
||||
for (std::vector<JpegImage>::const_iterator it = images.begin(); it != images.end(); ++it)
|
||||
outIterator = std::copy(it->Data().begin(), it->Data().end(), outIterator);
|
||||
}
|
||||
|
||||
void JpegImageCollection::Save(const std::string& fileName)
|
||||
{
|
||||
if (m_width == 0 || m_height == 0 || m_strideX == 0 || m_strideY == 0)
|
||||
return;
|
||||
|
||||
struct jpeg_compress_struct cinfo;
|
||||
struct jpeg_error_mgr jerr;
|
||||
|
||||
FILE* outFile = fopen(fileName.c_str(), "wb");
|
||||
|
||||
cinfo.err = jpeg_std_error(&jerr);
|
||||
jpeg_create_compress(&cinfo);
|
||||
jpeg_stdio_dest(&cinfo, outFile);
|
||||
|
||||
cinfo.image_width = m_width;
|
||||
cinfo.image_height = m_height;
|
||||
cinfo.input_components = m_strideX;
|
||||
cinfo.in_color_space = JCS_RGB;
|
||||
|
||||
jpeg_set_defaults( &cinfo );
|
||||
cinfo.num_components = 3;
|
||||
cinfo.dct_method = JDCT_FLOAT;
|
||||
jpeg_set_quality(&cinfo, 80, TRUE);
|
||||
|
||||
jpeg_start_compress( &cinfo, TRUE );
|
||||
|
||||
JSAMPROW scanlines[1];
|
||||
while (cinfo.next_scanline < cinfo.image_height)
|
||||
{
|
||||
scanlines[0] = reinterpret_cast<JSAMPROW>(&m_imageData[cinfo.next_scanline * m_strideY]);
|
||||
jpeg_write_scanlines(&cinfo, scanlines, 1);
|
||||
}
|
||||
|
||||
jpeg_finish_compress(&cinfo);
|
||||
jpeg_destroy_compress(&cinfo);
|
||||
fclose(outFile);
|
||||
}
|
||||
|
||||
} // namespace Image
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef OUTPUTIMAGE_H
|
||||
#define OUTPUTIMAGE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "JpegImage.h"
|
||||
|
||||
|
||||
namespace Image {
|
||||
|
||||
class JpegImageCollection
|
||||
{
|
||||
public:
|
||||
JpegImageCollection(const std::vector<JpegImage>& images);
|
||||
|
||||
void Save(const std::string& fileName);
|
||||
|
||||
private:
|
||||
size_t m_width;
|
||||
size_t m_height;
|
||||
size_t m_strideX;
|
||||
size_t m_strideY;
|
||||
|
||||
std::vector<char> m_imageData;
|
||||
};
|
||||
|
||||
} // namespace Image
|
||||
|
||||
#endif // OUTPUTIMAGE_H
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "MimeImage.h"
|
||||
#include "Util/Base64.h"
|
||||
#include <StringAlgorithm.h>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
namespace Image {
|
||||
|
||||
MimeImage::MimeImage()
|
||||
{
|
||||
}
|
||||
|
||||
MimeImage::MimeImage(const std::string& name) :
|
||||
m_name(name)
|
||||
{
|
||||
}
|
||||
|
||||
void MimeImage::AppendData(const std::string& data)
|
||||
{
|
||||
if (data.empty())
|
||||
return;
|
||||
|
||||
m_imageData.append(data);
|
||||
}
|
||||
|
||||
const JpegImage& MimeImage::DecodeImageData()
|
||||
{
|
||||
StringAlgorithm::erase_all(m_imageData, '\r');
|
||||
m_jpegImage.SetImageData(Util::Base64::Decode(m_imageData));
|
||||
return m_jpegImage;
|
||||
}
|
||||
|
||||
} // namespace Image
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef MIMEIMAGE_H
|
||||
#define MIMEIMAGE_H
|
||||
|
||||
#include "JpegImage.h"
|
||||
|
||||
|
||||
namespace Image {
|
||||
|
||||
class MimeImage
|
||||
{
|
||||
public:
|
||||
MimeImage();
|
||||
MimeImage(const std::string& name);
|
||||
|
||||
void AppendData(const std::string& data);
|
||||
const JpegImage& DecodeImageData();
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
std::string m_imageData;
|
||||
JpegImage m_jpegImage;
|
||||
};
|
||||
|
||||
} // namespace Image
|
||||
|
||||
#endif // MIMEIMAGE_H
|
||||
Reference in New Issue
Block a user