summaryrefslogtreecommitdiff
path: root/miranda-wine/plugins/png2dib/png2dib.c
blob: 5e531ef8d1832cc0e8d6def87645e26e2f61fb5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Plugin of Miranda IM for reading/writing PNG images.
Copyright (c) 2004-6 George Hazan (ghazan@postman.ru)

Portions of this code are gotten from the libpng codebase.
Copyright 2000, Willem van Schaik.  For conditions of distribution and
use, see the copyright/license/disclaimer notice in png.h

Miranda IM: the free icq client for MS Windows
Copyright (C) 2000-2002 Richard Hughes, Roland Rabien & Tristan Van de Vreede

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

File name      : $Source: /cvsroot/miranda/miranda/plugins/png2dib/png2dib.c,v $
Revision       : $Revision: 3502 $
Last change on : $Date: 2006-08-16 01:21:49 +0400 (Срд, 16 Авг 2006) $
Last change by : $Author: ghazan $

*/

#include <windows.h>
#include <commdlg.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

#include "libpng/png.h"

#include "newpluginapi.h"
#include "version.h"
#include "m_png.h"

DWORD __declspec(dllexport) getver( void )
{
	return __VERSION_DWORD;
}

// PNG image handler functions

typedef struct {
	char*		mBuffer;
	size_t	mBufSize;
	size_t	mBufPtr;
}
	HMemBufInfo;

static void png_read_data( png_structp png_ptr, png_bytep data, png_size_t length )
{
	HMemBufInfo* io = ( HMemBufInfo* )png_ptr->io_ptr;
	if ( length + io->mBufPtr > io->mBufSize )
		length = io->mBufSize - io->mBufPtr;

	if ( length > 0 ) {
		memcpy( data, io->mBuffer + io->mBufPtr, length );
		io->mBufPtr += length;
	}
	else png_error(png_ptr, "Read Error");
}

static void png_write_data( png_structp png_ptr, png_bytep data, png_size_t length )
{
	HMemBufInfo* io = ( HMemBufInfo* )png_ptr->io_ptr;
	if ( io->mBuffer != NULL )
		memcpy( io->mBuffer + io->mBufPtr, data, length );

	io->mBufPtr += length;
}

static void png_flush( png_structp png_ptr )
{
}

/*
 *		Converting a png image into a bitmap
 */

BOOL __declspec(dllexport) mempng2dib(
	BYTE*                pSource,
	DWORD                cbSourceSize,
	BITMAPINFOHEADER**   ppDibData )
{
	png_structp png_ptr	= NULL;
	png_infop info_ptr	= NULL;
	HMemBufInfo				sBuffer = { pSource, cbSourceSize, 8 };

	BOOL						bResult = FALSE;
	int						iWidth;
	int						iHeight;
	png_color				pBkgColor;
	int						iBitDepth;
	int						iColorType;
	double					dGamma;
	png_color_16*			pBackground;
	png_uint_32				ulChannels;
	png_uint_32				ulRowBytes;
	png_byte*				pbImageData;
	png_byte**				ppbRowPointers = NULL;
	int						i;
	int						wDIRowBytes;
	BYTE*                pImageData;

	*ppDibData = NULL;

	if ( pSource == NULL || cbSourceSize == 0 )
		return FALSE;

	if ( !png_check_sig( pSource, 8 ))
		return FALSE;

	// create the two png(-info) structures
	png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
	if (!png_ptr)
		return FALSE;

	info_ptr = png_create_info_struct(png_ptr);
	if ( !info_ptr ) {
		png_destroy_read_struct(&png_ptr, NULL, NULL);
		return FALSE;
	}

	// initialize the png structure
	png_set_read_fn(png_ptr, (png_voidp)&sBuffer, png_read_data);
	png_set_sig_bytes(png_ptr, 8);

	// read all PNG info up to image data
	png_read_info(png_ptr, info_ptr);

	// get width, height, bit-depth and color-type

	png_get_IHDR(png_ptr, info_ptr, &iWidth, &iHeight, &iBitDepth, &iColorType, NULL, NULL, NULL);

	// expand images of all color-type and bit-depth to 3x8 bit RGB images
	// let the library process things like alpha, transparency, background

	if ( iBitDepth == 16 )
		png_set_strip_16( png_ptr );
	if ( iColorType == PNG_COLOR_TYPE_PALETTE )
		png_set_expand( png_ptr );
	if ( iBitDepth < 8 )
		png_set_expand( png_ptr );
	if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ))
		png_set_expand( png_ptr );
	if ( iColorType == PNG_COLOR_TYPE_GRAY || iColorType == PNG_COLOR_TYPE_GRAY_ALPHA )
		png_set_gray_to_rgb( png_ptr );

	// set the background color to draw transparent and alpha images over.
	if (png_get_bKGD( png_ptr, info_ptr, &pBackground )) {
		png_set_background(png_ptr, pBackground, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
		pBkgColor.red   = (byte) pBackground->red;
		pBkgColor.green = (byte) pBackground->green;
		pBkgColor.blue  = (byte) pBackground->blue;
	}

	// if required set gamma conversion
	if ( png_get_gAMA( png_ptr, info_ptr, &dGamma ))
		png_set_gamma( png_ptr, (double) 2.2, dGamma );

	// after the transformations have been registered update info_ptr data
	png_read_update_info(png_ptr, info_ptr);

	// get again width, height and the new bit-depth and color-type
	png_get_IHDR(png_ptr, info_ptr, &iWidth, &iHeight, &iBitDepth, &iColorType, NULL, NULL, NULL);

	// row_bytes is the width x number of channels
	ulRowBytes = png_get_rowbytes(png_ptr, info_ptr);
	ulChannels = png_get_channels(png_ptr, info_ptr);
	wDIRowBytes = (WORD) (( ulChannels * iWidth + 3L) >> 2) << 2;

	// now we can allocate memory to store the image
	{	DWORD cbMemSize = sizeof( BITMAPINFOHEADER );
		cbMemSize += wDIRowBytes * iHeight;
		if (( pbImageData = ( png_byte* )GlobalAlloc( LPTR, cbMemSize )) == NULL ) {
			png_destroy_read_struct( &png_ptr, &info_ptr, NULL );
			return FALSE;
	}	}

	// initialize the dib-structure
	{	BITMAPINFOHEADER* pbmih = ( BITMAPINFOHEADER* )pbImageData;
		*ppDibData = pbmih;

		pbmih->biSize = sizeof( BITMAPINFOHEADER );
		pbmih->biWidth = iWidth;
		pbmih->biHeight = iHeight;
		pbmih->biPlanes = 1;
		pbmih->biBitCount = ulChannels * 8;
		pbmih->biCompression = 0;
		pbmih->biSizeImage = iWidth * iHeight * ulChannels;

		pbImageData += sizeof( BITMAPINFOHEADER );
	}

	pImageData = (BYTE*)malloc( ulRowBytes * iHeight );
	if ( pImageData == NULL ) {
		png_destroy_read_struct(&png_ptr, NULL, NULL);
		return FALSE;
	}

	// and allocate memory for an array of row-pointers
	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++ )
		ppbRowPointers[i] = ( png_bytep )&pImageData[ i*ulRowBytes ];

	// now we can go ahead and just read the whole image
	png_read_image( png_ptr, ppbRowPointers );
	png_read_end(png_ptr, NULL);

	// repack bytes to fill the bitmap
	for ( i = iHeight-1; i >= 0; i-- )
	{
		int j;
		png_byte a;
		png_bytep s = ppbRowPointers[i];
		BYTE* dest = pbImageData; pbImageData += wDIRowBytes;

		for ( j = 0; j < iWidth; j++ ) {
			png_byte r = *s++;
			png_byte g = *s++;
			png_byte b = *s++;
			if ( ulChannels == 4 )
				a = *s++;

			*dest++ = b;
			*dest++ = g;
			*dest++ = r;
			if ( ulChannels == 4 )
				*dest++ = a;
	}	}

	free( pImageData );
	png_destroy_read_struct( &png_ptr, &info_ptr, NULL );
	return TRUE;
}

/*
 *		Converting a bitmap into a png image
 */

BOOL __declspec(dllexport) dib2mempng( BITMAPINFO* pbmi, png_byte* pDiData, BYTE* pResult, long* pResultLen )
{
	int ciBitDepth = 8;
	int ciChannels = pbmi->bmiHeader.biBitCount / 8;

	png_uint_32 ulSrcRowBytes, ulDstRowBytes;
	int         i;
	png_structp png_ptr = NULL;
	png_infop   info_ptr = NULL;
	png_bytepp  ppbRowPointers;
	png_bytep	pTempBuffer;

	HMemBufInfo sBuffer = { pResult, 0, 0 };

	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr)NULL, (png_error_ptr)NULL);
	if (!png_ptr)
		return FALSE;

	info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
		png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
		return FALSE;
	}

	// initialize the png structure
	png_set_write_fn(png_ptr, (png_voidp)&sBuffer, png_write_data, png_flush);

	png_set_IHDR(png_ptr, info_ptr, pbmi->bmiHeader.biWidth, pbmi->bmiHeader.biHeight, ciBitDepth,
		PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

	// write the file header information
	png_write_info(png_ptr, info_ptr);

	// swap the BGR pixels in the DiData structure to RGB
	png_set_bgr(png_ptr);

	// row_bytes is the width x number of channels
	ulSrcRowBytes = ((( pbmi->bmiHeader.biWidth * ciChannels + 3 ) >> 2 ) << 2 );
	ulDstRowBytes = ((( pbmi->bmiHeader.biWidth * 3 + 3 ) >> 2 ) << 2 );

	ppbRowPointers = (png_bytepp)alloca( pbmi->bmiHeader.biHeight * sizeof(png_bytep));

	pTempBuffer = ( png_bytep )malloc( pbmi->bmiHeader.biHeight * ulDstRowBytes );
	if ( pTempBuffer != NULL ) {
		png_bytep pDest = pTempBuffer;
		for ( i = pbmi->bmiHeader.biHeight-1; i >= 0; i--) {
			BYTE *s, *d;
			int j;
			s = pDiData; pDiData += ulSrcRowBytes;
			d = ppbRowPointers[i] = pDest; pDest += ulDstRowBytes;

			if ( ciChannels >= 3 ) {
				for ( j = 0; j < pbmi->bmiHeader.biWidth; j++ ) {
					png_byte b = *s++;
					png_byte g = *s++;
					png_byte r = *s++;
					png_byte a = 0;

					if ( ciChannels == 4 )
						a = *s++;

					*d++ = b;
					*d++ = g;
					*d++ = r;
			}	}
			else {
				for ( j = 0; j < pbmi->bmiHeader.biWidth; j++ ) {
					DWORD point;
					if ( ciChannels == 1 ) {
						*d++ = ( BYTE )( point & 0x03 ) << 6;
						*d++ = ( BYTE )(( point & 0x0C ) >> 2 ) << 6;
						*d++ = ( BYTE )(( point & 0x30 ) >> 4 ) << 6;
						point = *s++;
					}
					else {
						point = *( WORD* )s;
						s += sizeof( WORD );
						*d++ = ( BYTE )(( point & 0x001F ) << 3 );
						*d++ = ( BYTE )((( point & 0x07e0 ) >> 6 ) << 3 );
						*d++ = ( BYTE )((( point & 0xF800 ) >> 11 ) << 3 );
		}	}	}	}

		png_write_image (png_ptr, ppbRowPointers);
		png_write_end(png_ptr, info_ptr);

		if ( pResultLen != NULL )
			*pResultLen = sBuffer.mBufPtr;

		free( pTempBuffer );
	}

	png_destroy_write_struct(&png_ptr, &info_ptr );
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Standard Miranda structures & functions

HINSTANCE hInst = NULL;
PLUGINLINK *pluginLink;

static HANDLE hDib2mempng = NULL;
static HANDLE hMempng2Dib = NULL;

PLUGININFO pluginInfo = {
	sizeof( PLUGININFO ),
	"PNG images processor",
	__VERSION_DWORD,
	"png2dib plugin for Miranda IM ( "__DATE__" )",
	"George Hazan",
	"ghazan@miranda-im.org",
	"(c) 2004-06 George Hazan",
	"http://addons.miranda-im.org/details.php?action=viewfile&id=1420",
	0,
	0
};

__declspec( dllexport ) PLUGININFO* MirandaPluginInfo( DWORD mirandaVersion )
{
	if ( mirandaVersion < PLUGIN_MAKE_VERSION( 0,4,3,0 )) {
		MessageBox( NULL, "The png2dib plugin cannot be loaded. It requires Miranda IM 0.5 or later.", "png2dib Plugin", MB_OK|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST );
		return NULL;
	}

	return &pluginInfo;
}

///////////////////////////////////////////////////////////////////////////////
// Load - initializes the plugin instance

static int serviceDib2Png( WPARAM wParam, LPARAM lParam )
{
	DIB2PNG* param = ( DIB2PNG* )lParam;
	return dib2mempng( param->pbmi, param->pDiData, param->pResult, param->pResultLen );
}

static int servicePng2Dib( WPARAM wParam, LPARAM lParam )
{
	PNG2DIB* param = ( PNG2DIB* )lParam;
	return mempng2dib( param->pSource, param->cbSourceSize, param->pResult );
}

int __declspec( dllexport ) Load( PLUGINLINK *link )
{
	pluginLink = link;

	hDib2mempng = CreateServiceFunction( MS_DIB2PNG, serviceDib2Png );
	hMempng2Dib = CreateServiceFunction( MS_PNG2DIB, servicePng2Dib );
	return 0;
}

///////////////////////////////////////////////////////////////////////////////
// Unload - destroys the plugin instance

int __declspec( dllexport ) Unload( void )
{
	DestroyServiceFunction( hDib2mempng );
	DestroyServiceFunction( hMempng2Dib );
	return 0;
}