aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/gfx/image/pngloader.cpp
blob: 693874314f87fdc0ae5dd04987217890f245ee8b (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

/* 
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------

#include "sword25/gfx/image/image.h"
#include "sword25/gfx/image/pngloader.h"
#include <png.h>

namespace Sword25 {

#define BS_LOG_PREFIX "PNGLOADER"

// -----------------------------------------------------------------------------
// Konstruktor / Destruktor
// -----------------------------------------------------------------------------

BS_PNGLoader::BS_PNGLoader()
{
}

// -----------------------------------------------------------------------------
// Laden
// -----------------------------------------------------------------------------

static void png_user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
	memcpy(data, (char*)png_ptr->io_ptr, length);
	png_ptr->io_ptr = (void*)((png_size_t)png_ptr->io_ptr + length);
}

// -----------------------------------------------------------------------------

bool BS_PNGLoader::DoDecodeImage(const char * FileDataPtr, unsigned int FileSize,  BS_GraphicEngine::COLOR_FORMATS ColorFormat, char * & UncompressedDataPtr,
								 int & Width, int & Height, int & Pitch)
{
	png_structp png_ptr = NULL;
	png_infop   info_ptr = NULL;
	png_bytep	RawDataBuffer = NULL;
	png_bytep*	pRowPtr = NULL;

	int			BitDepth;
	int			ColorType;
	int			InterlaceType;
	int			i;

	// Zielfarbformat �berpr�fen
	if (ColorFormat != BS_GraphicEngine::CF_RGB16 &&
		ColorFormat != BS_GraphicEngine::CF_RGB15 &&
		ColorFormat != BS_GraphicEngine::CF_RGB16_INTERLEAVED &&
		ColorFormat != BS_GraphicEngine::CF_RGB15_INTERLEAVED &&
		ColorFormat != BS_GraphicEngine::CF_ARGB32 &&
		ColorFormat != BS_GraphicEngine::CF_ABGR32)
	{
		BS_LOG_ERRORLN("Illegal or unsupported color format.");
		return false;
	}

	try
	{
		// PNG Signatur �berpr�fen
		if (!png_check_sig(reinterpret_cast<png_bytep>(const_cast<char *>(FileDataPtr)), 8))
		{
			throw(0);
		}

		// Die beiden PNG Strukturen erstellen
		png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
		if (!png_ptr)
		{
			BS_LOG_ERRORLN("Could not create libpng read struct.");
			throw(0);
		}

		info_ptr = png_create_info_struct(png_ptr);
		if (!info_ptr)
		{
			BS_LOG_ERRORLN("Could not create libpng info struct.");
			throw(0);
		}

		// R�cksprungpunkt setzen. Wird im Falle eines Fehlers angesprungen.
		if (setjmp(png_jmpbuf(png_ptr))) throw(0);

		// Alternative Lesefunktion benutzen
		png_set_read_fn(png_ptr, (void*)FileDataPtr, png_user_read_data);

		// PNG Header einlesen
		png_read_info(png_ptr, info_ptr);

		// PNG Informationen auslesen
		png_get_IHDR(png_ptr, info_ptr, (unsigned long*)&Width, (unsigned long*)&Height, &BitDepth, &ColorType, &InterlaceType, NULL, NULL);

		// Pitch des Ausgabebildes berechnen
		Pitch = BS_GraphicEngine::CalcPitch(ColorFormat, Width);

		// Speicher f�r die endg�ltigen Bilddaten reservieren
		// Dieses geschieht vor dem reservieren von Speicher f�r tempor�re Bilddaten um die Fragmentierung des Speichers gering zu halten
		UncompressedDataPtr = new char[Pitch * Height];
		if (!UncompressedDataPtr)
		{
			BS_LOG_ERRORLN("Could not allocate memory for output image.");
			throw(0);
		}

		// Bilder jeglicher Farbformate werden zun�chst in ARGB Bilder umgewandelt
		if (BitDepth == 16)
			png_set_strip_16(png_ptr);
		if (ColorType == PNG_COLOR_TYPE_PALETTE)
			png_set_expand(png_ptr);
		if (BitDepth < 8)
			png_set_expand(png_ptr);
		if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
			png_set_expand(png_ptr);
		if (ColorType == PNG_COLOR_TYPE_GRAY ||
			ColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
			png_set_gray_to_rgb(png_ptr);

		png_set_bgr(png_ptr);

		if (ColorType != PNG_COLOR_TYPE_RGB_ALPHA)
			png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);

		// Nachdem die Transformationen registriert wurden, werden die Bilddaten erneut eingelesen
		png_read_update_info(png_ptr, info_ptr);
		png_get_IHDR(png_ptr, info_ptr, (unsigned long*)&Width, (unsigned long*)&Height, &BitDepth, &ColorType, NULL, NULL, NULL);

		// PNGs ohne Interlacing werden Zeilenweise eingelesen
		if (InterlaceType == PNG_INTERLACE_NONE)
		{
			// Speicher f�r eine Bildzeile reservieren
			RawDataBuffer = new png_byte[png_get_rowbytes(png_ptr, info_ptr)];
			if (!RawDataBuffer)
			{
				BS_LOG_ERRORLN("Could not allocate memory for row buffer.");
				throw(0);
			}

			// Bilddaten zeilenweise einlesen und in das gew�nschte Zielformat konvertieren
			for (i = 0; i < Height; i++)
			{
				// Zeile einlesen
				png_read_row(png_ptr, RawDataBuffer, NULL);

				// Zeile konvertieren
				switch (ColorFormat)
				{
				case BS_GraphicEngine::CF_RGB16:
					RowARGB32ToRGB16((unsigned char*)RawDataBuffer,
						(unsigned char*)&UncompressedDataPtr[i * Pitch],
						Width);
					break;

				case BS_GraphicEngine::CF_RGB15:
					RowARGB32ToRGB15((unsigned char*)RawDataBuffer,
						(unsigned char*)&UncompressedDataPtr[i * Pitch],
						Width);
					break;

				case BS_GraphicEngine::CF_RGB16_INTERLEAVED:
					RowARGB32ToRGB16_INTERLEAVED((unsigned char*)RawDataBuffer,
						(unsigned char*)&UncompressedDataPtr[i * Pitch],
						Width);
					break;

				case BS_GraphicEngine::CF_RGB15_INTERLEAVED:
					RowARGB32ToRGB15_INTERLEAVED((unsigned char*)RawDataBuffer,
						(unsigned char*)&UncompressedDataPtr[i * Pitch],
						Width);
					break;

				case BS_GraphicEngine::CF_ARGB32:
					memcpy(&UncompressedDataPtr[i * Pitch],
						RawDataBuffer,
						Pitch);
					break;

				case BS_GraphicEngine::CF_ABGR32:
					RowARGB32ToABGR32((unsigned char*)RawDataBuffer,
						(unsigned char*)&UncompressedDataPtr[i * Pitch],
						Width);
					break;

				default:
					BS_ASSERT(false);
				}
			}
		}
		// PNGs mit Interlacing werden an einem St�ck eingelesen
		else
		{
			// Speicher f�r das komplette Bild reservieren
			RawDataBuffer = new png_byte[png_get_rowbytes(png_ptr, info_ptr) * Height];
			if (!RawDataBuffer)
			{
				BS_LOG_ERRORLN("Could not allocate memory for raw image buffer.");
				throw(0);
			}

			// Speicher f�r die Rowpointer reservieren
			pRowPtr = new png_bytep[Height];
			if (!pRowPtr)
			{
				BS_LOG_ERRORLN("Could not allocate memory for row pointers.");
				throw(0);
			}

			// Alle Rowpointer mit den richtigen Offsets initialisieren
			for (i = 0; i < Height; i++)
				pRowPtr[i] = RawDataBuffer + i * png_get_rowbytes(png_ptr, info_ptr);

			// Bild einlesen
			png_read_image(png_ptr, pRowPtr);

			// Bilddaten zeilenweise in das gew�nschte Ausgabeformat konvertieren
			switch (ColorFormat)
			{
			case BS_GraphicEngine::CF_RGB16:
				for (i = 0; i < Height; i++)
					RowARGB32ToRGB16((unsigned char*)(&RawDataBuffer[i * png_get_rowbytes(png_ptr, info_ptr)]),
					(unsigned char*)&UncompressedDataPtr[i * Pitch],
					Width);
				break;

			case BS_GraphicEngine::CF_RGB15:
				for (i = 0; i < Height; i++)
					RowARGB32ToRGB15((unsigned char*)(&RawDataBuffer[i * png_get_rowbytes(png_ptr, info_ptr)]),
					(unsigned char*)&UncompressedDataPtr[i * Pitch],
					Width);
				break;

			case BS_GraphicEngine::CF_RGB16_INTERLEAVED:
				for (i = 0; i < Height; i++)
					RowARGB32ToRGB16_INTERLEAVED((unsigned char*)(&RawDataBuffer[i * png_get_rowbytes(png_ptr, info_ptr)]),
					(unsigned char*)&UncompressedDataPtr[i * Pitch],
					Width);
				break;

			case BS_GraphicEngine::CF_RGB15_INTERLEAVED:
				for (i = 0; i < Height; i++)
					RowARGB32ToRGB15_INTERLEAVED((unsigned char*)(&RawDataBuffer[i * png_get_rowbytes(png_ptr, info_ptr)]),
					(unsigned char*)&UncompressedDataPtr[i * Pitch],
					Width);
				break;

			case BS_GraphicEngine::CF_ARGB32:
				for (i = 0; i < Height; i++)
					memcpy(&UncompressedDataPtr[i * Pitch],
					&RawDataBuffer[i * png_get_rowbytes(png_ptr, info_ptr)],
					Pitch);
				break;
			}
		}

		// Die zus�tzlichen Daten am Ende des Bildes lesen
		png_read_end(png_ptr, NULL);

		// Die Strukturen freigeben
		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

		// Tempor�re Buffer freigeben
		delete[] pRowPtr;
		delete[] RawDataBuffer;
	}

	catch(int)
	{
		delete[] pRowPtr;
		delete[] RawDataBuffer;
		if (png_ptr) png_destroy_read_struct(&png_ptr, NULL, NULL);
		if (info_ptr) png_destroy_read_struct(NULL, &info_ptr, NULL);

		// Der Funktionsaufruf war nicht erfolgreich
		return false;
	}

	// Der Funktionsaufruf war erfolgreich
	return true;
}

// -----------------------------------------------------------------------------

bool BS_PNGLoader::DecodeImage(const char * FileDataPtr, unsigned int FileSize,  BS_GraphicEngine::COLOR_FORMATS ColorFormat, char * & UncompressedDataPtr,
							   int & Width, int & Height, int & Pitch)
{
	return DoDecodeImage(FileDataPtr, FileSize, ColorFormat, UncompressedDataPtr, Width, Height, Pitch);
}

// -----------------------------------------------------------------------------

bool BS_PNGLoader::DoImageProperties(const char * FileDataPtr, unsigned int FileSize, BS_GraphicEngine::COLOR_FORMATS & ColorFormat, int & Width, int & Height)
{
	// PNG Signatur �berpr�fen
	if (!DoIsCorrectImageFormat(FileDataPtr, FileSize)) return false;

	png_structp png_ptr = NULL;
	png_infop info_ptr = NULL;
	try
	{
		// Die beiden PNG Strukturen erstellen
		png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
		if (!png_ptr)
		{
			BS_LOG_ERRORLN("Could not create libpng read struct.");
			throw(0);
		}

		info_ptr = png_create_info_struct(png_ptr);
		if (!info_ptr)
		{
			BS_LOG_ERRORLN("Could not create libpng info struct.");
			throw(0);
		}

		// Alternative Lesefunktion benutzen
		png_set_read_fn(png_ptr, (void*)FileDataPtr, png_user_read_data);

		// PNG Header einlesen
		png_read_info(png_ptr, info_ptr);

		// PNG Informationen auslesen
		int BitDepth;
		int ColorType;
		png_get_IHDR(png_ptr, info_ptr, (unsigned long*)&Width, (unsigned long*)&Height, &BitDepth, &ColorType, NULL, NULL, NULL);

		// PNG-ColorType in BS ColorFormat konvertieren.
		if (ColorType & PNG_COLOR_MASK_ALPHA || png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
			ColorFormat = BS_GraphicEngine::CF_ARGB32;
		else
			ColorFormat = BS_GraphicEngine::CF_RGB24;

		// Die Strukturen freigeben
		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
	}

	catch (int)
	{
		if (png_ptr) png_destroy_read_struct(&png_ptr, NULL, NULL);
		if (info_ptr) png_destroy_read_struct(NULL, &info_ptr, NULL);

		// Der Funktionsaufruf war nicht erfolgreich
		return false;
	}

	return true;

}

// -----------------------------------------------------------------------------

bool BS_PNGLoader::ImageProperties(const char* FileDataPtr, unsigned int FileSize,  BS_GraphicEngine::COLOR_FORMATS & ColorFormat, int & Width, int & Height)
{
	return DoImageProperties(FileDataPtr, FileSize, ColorFormat, Width, Height);
}

// -----------------------------------------------------------------------------
// Header �berpr�fen
// -----------------------------------------------------------------------------

bool BS_PNGLoader::DoIsCorrectImageFormat(const char * FileDataPtr, unsigned int FileSize)
{
	if (FileSize > 8)
		return png_check_sig((unsigned char*)FileDataPtr, 8) ? true : false;
	else
		return false;
}

// -----------------------------------------------------------------------------

bool BS_PNGLoader::IsCorrectImageFormat(const char* FileDataPtr, unsigned int FileSize)
{
	return DoIsCorrectImageFormat(FileDataPtr, FileSize);
}

} // End of namespace Sword25