00001 #ifndef Impala_Core_Array_WriteJpg_h
00002 #define Impala_Core_Array_WriteJpg_h
00003
00004 #include "Core/Array/Arrays.h"
00005 #include "Persistency/File.h"
00006 #ifdef JPG_USED
00007 #include "Link/Jpeg/CxJpegLink.h"
00008 #endif
00009
00010 namespace Impala
00011 {
00012 namespace Core
00013 {
00014 namespace Array
00015 {
00016
00017
00018 static int gWriteJpgQuality = 90;
00019
00020 #ifdef JPG_USED
00021 static jmp_buf gWriteJpgJmpbuf;
00022
00023 static inline void
00024 errorFuncWriteJpg(j_common_ptr cinfo)
00025 {
00026 (*cinfo->err->output_message) (cinfo);
00027
00028 longjmp(gWriteJpgJmpbuf, 1);
00029 }
00030
00031
00032 template <class ArrayT>
00033 inline bool
00034 WriteJpgToMemory(ArrayT* src, char* buf, size_t memBufSize, size_t* outSize)
00035 {
00036 ILOG_VAR(Impala.Core.Array.WriteJpg);
00037 struct jpeg_compress_struct cinfo;
00038 struct jpeg_error_mgr pub;
00039 cinfo.err = jpeg_std_error(&pub);
00040 pub.error_exit = errorFuncWriteJpg;
00041 if (setjmp(gWriteJpgJmpbuf))
00042 {
00043 ILOG_ERROR("setjmp");
00044 jpeg_destroy_compress(&cinfo);
00045 return false;
00046 }
00047 jpeg_create_compress(&cinfo);
00048 jpeg_memory_dest(&cinfo, &buf, outSize);
00049
00050 int elemSize = ArrayT::ElemSize();
00051 if (elemSize == 1)
00052 {
00053 cinfo.input_components = 1;
00054 cinfo.in_color_space = JCS_GRAYSCALE;
00055 }
00056 else if (elemSize == 3)
00057 {
00058 cinfo.input_components = 3;
00059 cinfo.in_color_space = JCS_RGB;
00060 }
00061
00062 cinfo.image_width = src->CW();
00063 cinfo.image_height = src->CH();
00064
00065 jpeg_set_defaults(&cinfo);
00066 jpeg_set_quality(&cinfo, gWriteJpgQuality,
00067 TRUE);
00068
00069 jpeg_start_compress(&cinfo, TRUE);
00070 JSAMPROW row_pointer[1];
00071 while (cinfo.next_scanline < cinfo.image_height)
00072 {
00073 row_pointer[0] = src->CPB(0, cinfo.next_scanline);
00074 jpeg_write_scanlines(&cinfo, row_pointer, 1);
00075 }
00076
00077 jpeg_finish_compress(&cinfo);
00078 jpeg_destroy_compress(&cinfo);
00079 return true;
00080 }
00081
00082 #else
00083
00084 template <class ArrayT>
00085 inline bool
00086 WriteJpgToMemory(ArrayT* src, char* buf, size_t memBufSize, size_t* outSize)
00087 {
00088 ILOG_VAR(Impala.Core.Array.WriteJpgToMemory);
00089 ILOG_ERROR("WriteJpg not available.");
00090 return false;
00091 }
00092
00093 #endif // JPG_USED
00094
00095 template <class ArrayT>
00096 inline bool
00097 WriteJpg(ArrayT* src, Util::IOBuffer* buffer, size_t memBufSize = 10000000)
00098 {
00099 char* buf = new char[memBufSize];
00100 size_t outSize;
00101 if (!WriteJpgToMemory(src, buf, memBufSize, &outSize))
00102 {
00103 delete buf;
00104 return false;
00105 }
00106 buffer->Write(buf, outSize);
00107 delete buf;
00108 return true;
00109 }
00110
00111 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00112 template <class ArrayT>
00113 inline bool
00114 WriteJpg(ArrayT* src, String fileName, Util::Database* db,
00115 size_t memBufSize = 10000000)
00116 {
00117 Util::IOBuffer* buf = db->GetIOBuffer(fileName, false, false, "tmp");
00118 if (!buf)
00119 return false;
00120 bool res = WriteJpg(src, buf, memBufSize);
00121 delete buf;
00122 return res;
00123 }
00124 #endif // REPOSITORY_USED
00125
00126
00127
00128 template <class ArrayT>
00129 inline bool
00130 WriteJpg(ArrayT* src, Persistency::File file, size_t memBufSize = 10000000)
00131 {
00132 Util::IOBuffer* buf = file.GetWriteBuffer();
00133 if (!buf)
00134 return false;
00135 bool res = WriteJpg(src, buf, memBufSize);
00136 delete buf;
00137 return res;
00138 }
00139
00140 }
00141 }
00142 }
00143
00144 #endif