00001 #ifndef Impala_Core_Array_LimitCompressedImageSize_h
00002 #define Impala_Core_Array_LimitCompressedImageSize_h
00003
00004 #include "Basis/ILog.h"
00005 #include "Core/Array/Arrays.h"
00006 #include "Core/Array/MakeFromData.h"
00007 #include "Core/Array/ReadImage.h"
00008 #include "Core/Array/Scale.h"
00009 #include "Core/Array/WritePng.h"
00010
00011 namespace Impala
00012 {
00013 namespace Core
00014 {
00015 namespace Array
00016 {
00017
00018
00019 inline void
00020 LimitCompressedImageSize(Array2dScalarUInt8*& bufIm, int maxSize)
00021 {
00022 ILOG_VAR(Impala.Core.Array.LimitCompressedImageSize);
00023
00024 if (maxSize <= 0)
00025 return;
00026
00027 Array2dVec3UInt8* rgbIm = 0;
00028 ReadImageFromMemory(rgbIm, (char*)bufIm->CPB(), bufIm->CW());
00029 if (!rgbIm)
00030 return;
00031
00032 if ((rgbIm->CW() > maxSize) || (rgbIm->CH() > maxSize))
00033 {
00034 double scale = 1.0;
00035 while ((scale * rgbIm->CW() > maxSize) || (scale * rgbIm->CH() > maxSize))
00036 scale *= 0.5;
00037
00038 size_t nrBytes = 0;
00039 Array2dVec3UInt8* sc = 0;
00040 Scale(sc, rgbIm, scale, scale, Core::Geometry::NEAREST, true);
00041 int charBufSize = sc->CW() * sc->CH() * 3 + 1024;
00042 char* charBuf = new char[charBufSize];
00043 WritePngToMemory(sc, charBuf, charBufSize, &nrBytes);
00044 delete sc;
00045
00046 delete bufIm;
00047 bufIm = MakeFromData<Array2dScalarUInt8>((UInt8*) charBuf, nrBytes, 1);
00048 delete charBuf;
00049 }
00050 delete rgbIm;
00051 }
00052
00053 }
00054 }
00055 }
00056
00057 #endif