advaimg (Imgage Service) memory functions reduntant + LoadMem should autodetect format
Adam Strzelecki <
ono@...>
2007-07-04 12:06:53 GMT
Hi Devs,
Since I'm moving some parts of GG plugin to use "advaimg" for Image
sending/reception I found out that there're 2 things that bothers me in
this plugin (service):
1) FI_LoadFromMem & FI_SaveToMem are totally useless as since FreeImage
v3 memory read/write is handled directly by FreeImage_OpenMemory,
FreeImage_LoadFromMemory, and etc. which are exactly the same wrappers
for I/O
2) serviceLoadFromMem should try to autodetect format when FIF_UNKNOWN
or -1 is specified as it often happens we receive only binary chunk from
network without format info, also it often happens too that .gif files
are .jpegs etc.
Basically I really need this in order to GG Image reception work
properly, because I don't get file format info at all.
Therefore here's my commit proposal (see attached patch):
1) Removed all FI_LoadFromMem, FI_SaveToMem functions as they're already
in FreeImage v3 (MemoryIO.cpp). I didn't found anybody using those
anyway in Miranda (correct me if I'm wrong).. anyway I think one should
use: FI_OpenMemory, FI_CloseMemory, FI_LoadFromMemory anyway.
2) Changed serviceLoadFromMem function to use FreeImage_OpenMemory,
FreeImage_LoadFromMemory, FreeImage_CloseMemory and
FreeImage_GetFileTypeFromMemory when needed (FIF_UNKNOWN).
I'm aware that this changes m_imgsrvc.h FI_INTERFACE so AvatarService
and anyone else needs to be recompiled in order to make it run properly.
Anyway anybody else than AVS is using FI_INTERFACE ??
Cheers,
--
: nanoANT Adam Strzelecki :
: nanoant.com :
Index: include/m_imgsrvc.h
===================================================================
--- include/m_imgsrvc.h (wersja 5802)
+++ include/m_imgsrvc.h (kopia robocza)
<at> <at> -40,36 +40,7 <at> <at>
#define FI_IF_VERSION (PLUGIN_MAKE_VERSION(0, 0, 1, 0)) // interface version - must match
-// memory i/o defs
-
/*
- * this struct defines a memio job.
- * datalen and filename must match and must be populated to the size of the memory buffer (caution)
- * data must point to the buffer.
- * curpos is internal and should be initialized to 0
- */
-
-typedef struct fiio_mem_handle_s {
- long filelen,datalen,curpos;
- void *data;
-} fiio_mem_handle;
-
-/* it is up to the user to create a fiio_mem_handle and init datalen and data
- * filelen will be pre-set to 0 by SaveToMem
- * curpos will be pre-set to 0 by SaveToMem and LoadFromMem
- * IMPORTANT: data should be set to NULL and datalen to 0,
- * unless the user wants to manually malloc a larger buffer
- */
-FIBITMAP *FreeImage_LoadFromMem(FREE_IMAGE_FORMAT fif, fiio_mem_handle *handle, int flags);
-BOOL FreeImage_SaveToMem(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, fiio_mem_handle *handle, int flags);
-
-void SetMemIO(FreeImageIO *io);
-unsigned __stdcall fiio_mem_ReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle);
-unsigned __stdcall fiio_mem_WriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle);
-int __stdcall fiio_mem_SeekProc(fi_handle handle, long offset, int origin);
-long __stdcall fiio_mem_TellProc(fi_handle handle);
-
-/*
* this interface directly exports most of FreeImage routines
* you can use them in your plugin after obtaining the interfasce using MS_IMG_GETINTERFACE
*/
<at> <at> -83,7 +54,7 <at> <at>
FIBITMAP *(DLL_CALLCONV *FI_Clone)(FIBITMAP *dib);
void (DLL_CALLCONV *FI_Unload)(FIBITMAP *dib);
- // Load / Save routines -----------------------------------------------------
+// Load / Save routines -----------------------------------------------------
FIBITMAP *(DLL_CALLCONV *FI_Load)(FREE_IMAGE_FORMAT fif, const char *filename, int flags FI_DEFAULT(0));
FIBITMAP *(DLL_CALLCONV *FI_LoadU)(FREE_IMAGE_FORMAT fif, const wchar_t *filename, int flags FI_DEFAULT(0));
<at> <at> -324,10 +295,6 <at> <at>
// own functions
- // memory I/O
- FIBITMAP *(*FI_LoadFromMem)(FREE_IMAGE_FORMAT fif, fiio_mem_handle *handle, int flags);
- BOOL (*FI_SaveToMem)(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, fiio_mem_handle *handle, int flags);
-
// helpers
FIBITMAP *(*FI_CreateDIBFromHBITMAP)(HBITMAP hBmp);
HBITMAP (*FI_CreateHBITMAPFromDIB)(FIBITMAP *dib);
<at> <at> -402,8 +369,8 <at> <at>
typedef struct _tagIMGSRVC_MEMIO {
long iLen; // length of the buffer
void *pBuf; // the buffer itself (you are responsible for allocating and free'ing it)
- FREE_IMAGE_FORMAT fif; // the FIF_* image format constant. Make sure to provide the right one.
- UINT flags; // flags to pass to FreeImage_LoadFromMem() (see freeimage docs)
+ FREE_IMAGE_FORMAT fif; // -1 to detect the format or one of the FIF_* image format constant
+ UINT flags; // flags to pass to FreeImage_LoadFromMemory() (see freeimage docs)
} IMGSRVC_MEMIO;
// load an image from a memory buffer
Index: plugins/freeimage/Miranda/main.cpp
===================================================================
--- plugins/freeimage/Miranda/main.cpp (wersja 5802)
+++ plugins/freeimage/Miranda/main.cpp (kopia robocza)
<at> <at> -351,138 +351,6 <at> <at>
}
}
-
-/*--------------------------------------------------------------------------*\
-|| fiio_mem.cpp by Ryan Rubley <ryan <at> lostreality.org> ||
-|| ||
-|| (v1.02) 4-28-2004 ||
-|| FreeImageIO to memory ||
-|| ||
-\*--------------------------------------------------------------------------*/
-
-FIBITMAP *
-FreeImage_LoadFromMem(FREE_IMAGE_FORMAT fif, fiio_mem_handle *handle, int flags) {
- FreeImageIO io;
- SetMemIO(&io);
-
- if (handle && handle->data) {
- handle->curpos = 0;
- return FreeImage_LoadFromHandle(fif, &io, (fi_handle)handle, flags);
- }
-
- return NULL;
-}
-
-BOOL
-FreeImage_SaveToMem(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, fiio_mem_handle *handle, int flags) {
- FreeImageIO io;
- SetMemIO(&io);
-
- if (handle) {
- handle->filelen = 0;
- handle->curpos = 0;
- return FreeImage_SaveToHandle(fif, dib, &io, (fi_handle)handle, flags);
- }
-
- return FALSE;
-}
-
-// ----------------------------------------------------------
-
-void
-SetMemIO(FreeImageIO *io) {
- io->read_proc = fiio_mem_ReadProc;
- io->seek_proc = fiio_mem_SeekProc;
- io->tell_proc = fiio_mem_TellProc;
- io->write_proc = fiio_mem_WriteProc;
-}
-
-// ----------------------------------------------------------
-
-#define FIIOMEM(member) (((fiio_mem_handle *)handle)->member)
-
-unsigned __stdcall fiio_mem_ReadProc(void *buffer, unsigned size, unsigned count, fi_handle
handle) {
- unsigned x;
- for( x=0; x<count; x++ ) {
- //if there isnt size bytes left to read, set pos to eof and return a short count
- if( FIIOMEM(filelen)-FIIOMEM(curpos) < (long)size ) {
- FIIOMEM(curpos) = FIIOMEM(filelen);
- break;
- }
- //copy size bytes count times
- memcpy( buffer, (char *)FIIOMEM(data) + FIIOMEM(curpos), size );
- FIIOMEM(curpos) += size;
- buffer = (char *)buffer + size;
- }
- return x;
-}
-
-unsigned __stdcall fiio_mem_WriteProc(void *buffer, unsigned size, unsigned count, fi_handle
handle) {
- void *newdata;
- long newdatalen;
- //double the data block size if we need to
- while( FIIOMEM(curpos)+(long)(size*count) >= FIIOMEM(datalen) ) {
- //if we are at or above 1G, we cant double without going negative
- if( FIIOMEM(datalen) & 0x40000000 ) {
- //max 2G
- if( FIIOMEM(datalen) == 0x7FFFFFFF ) {
- return 0;
- }
- newdatalen = 0x7FFFFFFF;
- } else if( FIIOMEM(datalen) == 0 ) {
- //default to 4K if nothing yet
- newdatalen = 4096;
- } else {
- //double size
- newdatalen = FIIOMEM(datalen) << 1;
- }
- newdata = realloc( FIIOMEM(data), newdatalen );
- if( !newdata ) {
- return 0;
- }
- FIIOMEM(data) = newdata;
- FIIOMEM(datalen) = newdatalen;
- }
- memcpy( (char *)FIIOMEM(data) + FIIOMEM(curpos), buffer, size*count );
- FIIOMEM(curpos) += size*count;
- if( FIIOMEM(curpos) > FIIOMEM(filelen) ) {
- FIIOMEM(filelen) = FIIOMEM(curpos);
- }
- return count;
-}
-
-int __stdcall fiio_mem_SeekProc(fi_handle handle, long offset, int origin) {
- switch(origin) { //0 to filelen-1 are 'inside' the file
- default:
- case SEEK_SET: //can fseek() to 0-7FFFFFFF always
- if( offset >= 0 ) {
- FIIOMEM(curpos) = offset;
- return 0;
- }
- break;
-
- case SEEK_CUR:
- if( FIIOMEM(curpos)+offset >= 0 ) {
- FIIOMEM(curpos) += offset;
- return 0;
- }
- break;
-
- case SEEK_END:
- if( FIIOMEM(filelen)+offset >= 0 ) {
- FIIOMEM(curpos) = FIIOMEM(filelen)+offset;
- return 0;
- }
- break;
- }
-
- return -1;
-}
-
-long __stdcall fiio_mem_TellProc(fi_handle handle) {
- return FIIOMEM(curpos);
-}
-
extern "C" DWORD __declspec(dllexport) getver( void )
{
return __VERSION_DWORD;
<at> <at> -649,7 +517,7 <at> <at>
ppbRowPointers = ( png_bytepp )alloca( iHeight * sizeof( png_bytep ));
// set the individual row-pointers to point at the correct offsets
- for ( i = 0; i < iHeight; i++ )
+ for ( i = 0; i < (int)iHeight; i++ )
ppbRowPointers[i] = ( png_bytep )&pImageData[ i*ulRowBytes ];
// now we can go ahead and just read the whole image
<at> <at> -663,7 +531,7 <at> <at>
png_bytep s = ppbRowPointers[i];
BYTE* dest = pbImageData; pbImageData += wDIRowBytes;
- for ( j = 0; j < iWidth; j++ ) {
+ for ( j = 0; j < (int)iWidth; j++ ) {
png_byte r = *s++;
png_byte g = *s++;
png_byte b = *s++;
<at> <at> -893,17 +761,15 <at> <at>
static int serviceLoadFromMem(WPARAM wParam, LPARAM lParam)
{
IMGSRVC_MEMIO *mio = (IMGSRVC_MEMIO *)wParam;
- fiio_mem_handle fiio;
if(mio->iLen == 0 || mio->pBuf == NULL)
return 0;
- fiio.curpos = 0;
- fiio.data = mio->pBuf;
- fiio.datalen = fiio.filelen = mio->iLen;
+ FIMEMORY *hmem = FreeImage_OpenMemory((BYTE *)mio->pBuf, mio->iLen);
+ FREE_IMAGE_FORMAT fif = (mio->fif != FIF_UNKNOWN) ? mio->fif : mio->fif =
FreeImage_GetFileTypeFromMemory(hmem, 0);
+ FIBITMAP *dib = FreeImage_LoadFromMemory(fif, hmem, mio->flags);
+ FreeImage_CloseMemory(hmem);
- FIBITMAP *dib = FreeImage_LoadFromMem(mio->fif, &fiio, mio->flags);
-
if(dib == NULL || (lParam & IMGL_RETURNDIB))
return (int)dib;
<at> <at> -1138,9 +1004,6 <at> <at>
feif.FI_Composite = FreeImage_Composite;
feif.FI_JPEGCrop = FreeImage_JPEGCrop;
- feif.FI_LoadFromMem = FreeImage_LoadFromMem;
- feif.FI_SaveToMem = FreeImage_SaveToMem;
-
feif.FI_CreateDIBFromHBITMAP = FreeImage_CreateDIBFromHBITMAP;
feif.FI_CreateHBITMAPFromDIB = FreeImage_CreateHBITMAPFromDIB;
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Miranda-Develop mailing list
Miranda-Develop@...
https://lists.sourceforge.net/lists/listinfo/miranda-develop