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
|
/* 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
*
*/
#define BS_LOG_PREFIX "SCREENSHOT"
// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------
#include "sword25/gfx/screenshot.h"
#include <png.h>
namespace Sword25 {
// -----------------------------------------------------------------------------
struct RGB_PIXEL {
RGB_PIXEL(unsigned char _Red, unsigned char _Green, unsigned char _Blue) :
Red(_Red),
Green(_Green),
Blue(_Blue)
{};
unsigned char Red;
unsigned char Green;
unsigned char Blue;
};
bool Screenshot::SaveToFile(unsigned int Width, unsigned int Height, const byte *Data, const Common::String &Filename) {
#if 0
BS_ASSERT(Data.size() == Width * Height);
// Buffer f�r Bildschirminhalt in RGB reservieren
vector<RGB_PIXEL> PixelBuffer;
PixelBuffer.reserve(Width * Height);
// Framebufferdaten pixelweise von RGBA nach RGB konvertieren
vector<unsigned int>::const_iterator it = Data.begin();
for (unsigned int y = 0; y < Height; y++) {
for (unsigned int x = 0; x < Width; x++) {
unsigned int SrcPixel = *it++;
PixelBuffer.push_back(RGB_PIXEL((SrcPixel >> 16) & 0xff, (SrcPixel >> 8) & 0xff, SrcPixel & 0xff));
}
}
BS_ASSERT(it == Data.end());
BS_ASSERT(Data.size() == PixelBuffer.size());
// Variablen f�r die PNG-Erstellung
FILE *OutFile = 0;
png_structp png_ptr = 0;
png_infop info_ptr = 0;
try {
OutFile = fopen(Filename.c_str(), "wb");
if (!OutFile) {
BS_LOG_ERRORLN("Could not create screenshot-file \"%s\".", Filename.c_str());
throw(0);
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
BS_LOG_ERRORLN("Could not create PNG write-struct.");
throw(0);
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
BS_LOG_ERRORLN("Could not create PNG info-struct.");
throw(0);
}
// Der Kompressionsbuffer muss gro� genug sein um das gesamte Bild zu beinhalten.
// Dieses stellt sicher, dass nur ein IDAT Chunk erstellt wird.
// Als Buffergr��e wird 110% der Rohdatengr��e verwandt, um sicher zu gehen.
png_set_compression_buffer_size(png_ptr, (Width * Height * 3 * 110) / 100);
// PNG-Info Struktur initialisieren
png_set_IHDR(png_ptr, info_ptr,
Width, // Breite
Height, // H�he
8, // Bittiefe pro Kanal
PNG_COLOR_TYPE_RGB, // Farbformat
PNG_INTERLACE_NONE, // Interlacing-Typ
PNG_COMPRESSION_TYPE_DEFAULT, // Kompressions-Typ
PNG_FILTER_TYPE_DEFAULT); // Filter-Typ
// Rowpointer erstellen
vector<png_bytep> RowPointers;
RowPointers.reserve(Height);
for (unsigned int i = 0; i < Height; i++) {
RowPointers.push_back((png_bytep)(&PixelBuffer[Width * i]));
}
png_set_rows(png_ptr, info_ptr, &RowPointers[0]);
png_init_io(png_ptr, OutFile);
// Bild schreiben
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(OutFile);
}
catch (int) {
// Wenn die Datei bereits erstellt wurde, Datei schlie�en und l�schen.
if (OutFile) {
fclose(OutFile);
remove(Filename.c_str());
}
if (info_ptr) png_destroy_write_struct(0, &info_ptr);
if (png_ptr) png_destroy_write_struct(&png_ptr, (png_infopp) 0);
BS_LOG_ERRORLN("Could not create screenshot (\"%s\").", Filename.c_str());
return false;
}
#else
warning("STUB: BS_Screenshot::SaveToFile(%d, %d, .., %s)", Width, Height, Filename.c_str());
#endif
return true;
}
// -----------------------------------------------------------------------------
bool Screenshot::SaveThumbnailToFile(unsigned int Width, unsigned int Height, const byte *Data, const Common::String &Filename) {
#if 0
//
// Diese Methode nimmt ein Screenshot mit den Ma�en von 800x600 und erzeugt einen Screenshot mit den Ma�en von 200x125.
// Dabei werden je 50 Pixel oben und unten abgeschnitten (die Interface-Leisten im Spiel). Das verbleibende Bild von 800x500 wird auf
// ein 16tel seiner Gr��e reduziert, indem es in 4x4 Pixelbl�cke ausgeteilt wird und der Durchschnitt jedes Blockes einen Pixel des Zielbildes generiert.
// Abschlie�end wird das Ergebnis als PNG-Datei unter dem �bergebenen Dateinamen gespeichert.
//
// Die Ausgangsgr��e muss 800x600 sein.
if (Width != 800 || Height != 600) {
BS_LOG_ERRORLN("The sreenshot dimensions have to be 800x600 in order to be saved as a thumbnail.");
return false;
}
// Buffer f�r die Zieldaten erstellen (RGBA Bild mit den Ma�en 200x125).
vector<unsigned int> ThumbnailData(200 * 125);
// �ber das Zielbild iterieren und einen Pixel zur Zeit berechnen.
unsigned int x, y;
x = y = 0;
for (vector<unsigned int>::iterator Iter = ThumbnailData.begin(); Iter != ThumbnailData.end(); ++Iter) {
// Durchschnitt �ber 4x4 Pixelblock im Quellbild bilden.
unsigned int Alpha, Red, Green, Blue;
Alpha = Red = Green = Blue = 0;
for (unsigned int j = 0; j < 4; ++j) {
for (unsigned int i = 0; i < 4; ++i) {
unsigned int Pixel = Data[((y * 4) + j + 50) * 800 + ((x * 4) + i)];
Alpha += (Pixel >> 24);
Red += (Pixel >> 16) & 0xff;
Green += (Pixel >> 8) & 0xff;
Blue += Pixel & 0xff;
}
}
// Zielpixel schreiben.
*Iter = ((Alpha / 16) << 24) | ((Red / 16) << 16) | ((Green / 16) << 8) | (Blue / 16);
// Mitz�hlen an welcher Stelle im Zielbild wir uns befinden.
++x;
if (x == 200) {
x = 0;
++y;
}
}
// Bild als PNG Speichern.
return SaveToFile(200, 125, ThumbnailData, Filename);
#else
warning("STUB: BS_Screenshot::SaveThumbnailToFile(%d, %d, .., %s)", Width, Height, Filename.c_str());
return true;
#endif
}
} // End of namespace Sword25
|