~adrian-arroyocalle/freerct/freerct

« back to all changes in this revision

Viewing changes to src/rcdgen/image.cpp

  • Committer: charlespigott at googlemail
  • Date: 2013-07-09 17:30:05 UTC
  • Revision ID: svn-v4:d3bf82d2-0c16-e458-5408-27be57bd1276:trunk:780
-Codechange: Reorganise the project files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id$ */
 
2
 
 
3
/*
 
4
 * This file is part of FreeRCT.
 
5
 * FreeRCT 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, version 2.
 
6
 * FreeRCT 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.
 
7
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with FreeRCT. If not, see <http://www.gnu.org/licenses/>.
 
8
 */
 
9
 
 
10
/** @file image.cpp %Image loading, cutting, and saving the sprites. */
 
11
 
 
12
#include "../../src/stdafx.h"
 
13
#include <cstdio>
 
14
#include <cstdlib>
 
15
#include <cassert>
 
16
#include "image.h"
 
17
 
 
18
#include "mask64.xbm"
 
19
 
 
20
static const size_t HEADER_SIZE = 4; ///< Number of bytes to read to decide whether a provided file is indeed a PNG file.
 
21
static const int TRANSPARENT = 0; ///< Colour index of 'transparent' in the 8bpp image.
 
22
 
 
23
 
 
24
/** Information about available bit masks. */
 
25
struct MaskInformation {
 
26
        int width;  ///< Width of the mask.
 
27
        int height; ///< Height of the mask.
 
28
        const unsigned char *data; ///< Data of the mask.
 
29
        const char *name; ///< Name of the mask.
 
30
};
 
31
 
 
32
/** List of bit masks. */
 
33
static const MaskInformation _masks[] = {
 
34
        {mask64_width, mask64_height, mask64_bits, "voxel64"},
 
35
        {0, 0, NULL, NULL}
 
36
};
 
37
 
 
38
/**
 
39
 * Retrieve a bitmask by its name.
 
40
 * @param name Name of the bitmask to retrieve.
 
41
 * @return The bitmask and its meta-information (static reference, do not free).
 
42
 */
 
43
static const MaskInformation *GetMask(const std::string &name)
 
44
{
 
45
        const MaskInformation *msk = _masks;
 
46
        while (msk->name != NULL) {
 
47
                if (msk->name == name) return msk;
 
48
                msk++;
 
49
        }
 
50
        fprintf(stderr, "Error: Cannot find a bitmask named \"%s\"\n", name.c_str());
 
51
        exit(1);
 
52
        return NULL;
 
53
}
 
54
 
 
55
Image::Image()
 
56
{
 
57
        this->png_initialized = false;
 
58
}
 
59
 
 
60
Image::~Image()
 
61
{
 
62
        if (this->png_initialized) {
 
63
                png_destroy_read_struct(&this->png_ptr, &this->info_ptr, &this->end_info);
 
64
        }
 
65
}
 
66
 
 
67
/**
 
68
 * Load a .png file from the disk.
 
69
 * @param fname Name of the .png file to load.
 
70
 * @param mask Bitmask to apply.
 
71
 * @return An error message if loading failed, or \c NULL if loading succeeded.
 
72
 */
 
73
const char *Image::LoadFile(const char *fname, BitMaskData *mask)
 
74
{
 
75
        FILE *fp = fopen(fname, "rb");
 
76
        if (fp == NULL) return "Input file does not exist";
 
77
 
 
78
        uint8 header[HEADER_SIZE];
 
79
        if (fread(header, 1, HEADER_SIZE, fp) != HEADER_SIZE) {
 
80
                fclose(fp);
 
81
                return "Failed to read the PNG header";
 
82
        }
 
83
        bool is_png = !png_sig_cmp(header, 0, HEADER_SIZE);
 
84
        if (!is_png) {
 
85
                fclose(fp);
 
86
                return "Header indicates it is not a PNG file";
 
87
        }
 
88
 
 
89
        this->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 
90
        if (!this->png_ptr) {
 
91
                fclose(fp);
 
92
                return "Failed to initialize the png data";
 
93
        }
 
94
 
 
95
        this-> info_ptr = png_create_info_struct(this->png_ptr);
 
96
        if (!this->info_ptr) {
 
97
                png_destroy_read_struct(&this->png_ptr, (png_infopp)NULL, (png_infopp)NULL);
 
98
                fclose(fp);
 
99
                return "Failed to setup a png info structure";
 
100
        }
 
101
 
 
102
        this->end_info = png_create_info_struct(this->png_ptr);
 
103
        if (!this->end_info) {
 
104
                png_destroy_read_struct(&this->png_ptr, &this->info_ptr, (png_infopp)NULL);
 
105
                fclose(fp);
 
106
                return "Failed to setup a png end info structure";
 
107
        }
 
108
 
 
109
        /* Setup callback in case of errors. */
 
110
        if (setjmp(png_jmpbuf(this->png_ptr))) {
 
111
                png_destroy_read_struct(&this->png_ptr, &this->info_ptr, &this->end_info);
 
112
                fclose(fp);
 
113
                return "Error detected while reading PNG file";
 
114
        }
 
115
 
 
116
        /* Initialize for file reading. */
 
117
        png_init_io(this->png_ptr, fp);
 
118
        png_set_sig_bytes(this->png_ptr, HEADER_SIZE);
 
119
 
 
120
        png_read_png(this->png_ptr, this->info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
 
121
 
 
122
        int bit_depth = png_get_bit_depth(this->png_ptr, this->info_ptr);
 
123
        if (bit_depth != 8) {
 
124
                png_destroy_read_struct(&this->png_ptr, &this->info_ptr, &this->end_info);
 
125
                fclose(fp);
 
126
                return "PNG file is not an 8bpp file";
 
127
        }
 
128
 
 
129
        if (mask != NULL) {
 
130
                this->mask = GetMask(mask->type);
 
131
                this->mask_xpos = mask->x_pos;
 
132
                this->mask_ypos = mask->y_pos;
 
133
        } else {
 
134
                this->mask = NULL;
 
135
                this->mask_xpos = 0;
 
136
                this->mask_ypos = 0;
 
137
        }
 
138
 
 
139
        this->png_initialized = true;
 
140
        this->row_pointers = png_get_rows(this->png_ptr, this->info_ptr);
 
141
        fclose(fp);
 
142
 
 
143
        this->width = png_get_image_width(this->png_ptr, this->info_ptr);
 
144
        this->height = png_get_image_height(this->png_ptr, this->info_ptr);
 
145
        return NULL;
 
146
}
 
147
 
 
148
/**
 
149
 * Get the width of the image.
 
150
 * @return Width of the loaded image, or \c -1.
 
151
 */
 
152
int Image::GetWidth()
 
153
{
 
154
        if (!this->png_initialized) return -1;
 
155
        return this->width;
 
156
}
 
157
 
 
158
/**
 
159
 * Get the height of the image.
 
160
 * @return Height of the loaded image, or \c -1.
 
161
 */
 
162
int Image::GetHeight()
 
163
{
 
164
        if (!this->png_initialized) return -1;
 
165
        return this->height;
 
166
}
 
167
 
 
168
/**
 
169
 * Get a pixel from the image.
 
170
 * @param x Horizontal position.
 
171
 * @param y Vertical position.
 
172
 * @return Value of the pixel (palette index, as it is an 8bpp indexed image).
 
173
 */
 
174
uint8 Image::GetPixel(int x, int y)
 
175
{
 
176
        assert(this->png_initialized);
 
177
        assert(x >= 0 && x < this->width);
 
178
        assert(y >= 0 && y < this->height);
 
179
 
 
180
        if (this->mask != NULL) {
 
181
                if (x >= this->mask_xpos && x < this->mask_xpos + this->mask->width &&
 
182
                                y >= this->mask_ypos && y < this->mask_ypos + this->mask->height) {
 
183
                        const unsigned char *p = this->mask->data;
 
184
                        int off = (this->mask_ypos - y) * ((this->mask->width + 7) / 8);
 
185
                        p += off;
 
186
                        off = this->mask_xpos - x;
 
187
                        p += off / 8;
 
188
                        if ((*p & (1 << (off & 7))) == 0) return TRANSPARENT;
 
189
                }
 
190
        }
 
191
 
 
192
        return this->row_pointers[y][x];
 
193
}
 
194
 
 
195
/**
 
196
 * Is the queried set of pixels empty?
 
197
 * @param xpos Horizontal start position.
 
198
 * @param ypos Vertical start position.
 
199
 * @param dx Horizontal stepping size.
 
200
 * @param dy Vertical stepping size.
 
201
 * @param length Number of pixels to examine.
 
202
 * @return All examined pixels are empty (have colour \c 0).
 
203
 */
 
204
bool Image::IsEmpty(int xpos, int ypos, int dx, int dy, int length)
 
205
{
 
206
        while (length > 0) {
 
207
                if (this->GetPixel(xpos, ypos) != TRANSPARENT) return false;
 
208
                xpos += dx;
 
209
                ypos += dy;
 
210
                length--;
 
211
        }
 
212
        return true;
 
213
}
 
214
 
 
215
 
 
216
SpriteImage::SpriteImage()
 
217
{
 
218
        this->data = NULL;
 
219
        this->row_sizes = NULL;
 
220
        this->data_size = 0;
 
221
        this->width = 0;
 
222
        this->height = 0;
 
223
}
 
224
 
 
225
SpriteImage::~SpriteImage()
 
226
{
 
227
        free(this->data);
 
228
        free(this->row_sizes);
 
229
}
 
230
 
 
231
/**
 
232
 * Copy a part of the image as a sprite.
 
233
 * @param img %Image source.
 
234
 * @param xoffset Horizontal offset of the origin to the top-left pixel of the sprite.
 
235
 * @param yoffset Vertical offset of the origin to the top-left pixel of the sprite.
 
236
 * @param xpos Left position of the sprite in the image.
 
237
 * @param ypos Top position of the sprite in the image.
 
238
 * @param xsize Width of the sprite in the image.
 
239
 * @param ysize Height of the sprite in the image.
 
240
 * @return Error message if the conversion failed, else \c NULL.
 
241
 */
 
242
const char *SpriteImage::CopySprite(Image *img, int xoffset, int yoffset, int xpos, int ypos, int xsize, int ysize)
 
243
{
 
244
        assert(img->png_initialized);
 
245
 
 
246
        /* Remove any old data. */
 
247
        free(this->data);
 
248
        free(this->row_sizes);
 
249
        this->data = NULL;
 
250
        this->row_sizes = NULL;
 
251
        this->data_size = 0;
 
252
        this->height = 0;
 
253
 
 
254
        int img_width = img->GetWidth();
 
255
        int img_height = img->GetHeight();
 
256
        if (xpos < 0 || ypos < 0) return "Negative starting position";
 
257
        if (xpos >= img_width || ypos >= img_height) return "Starting position beyond image";
 
258
        if (xsize < 0 || ysize < 0) return "Negative image size";
 
259
        if (xpos + xsize > img_width) return "Sprite too wide";
 
260
        if (ypos + ysize > img_height) return "Sprite too high";
 
261
 
 
262
        /* Perform cropping. */
 
263
 
 
264
        /* Crop left columns. */
 
265
        while (xsize > 0 && img->IsEmpty(xpos, ypos, 0, 1, ysize)) {
 
266
                xpos++;
 
267
                xsize--;
 
268
                xoffset++;
 
269
        }
 
270
 
 
271
        /* Crop top rows. */
 
272
        while (ysize > 0 && img->IsEmpty(xpos, ypos, 1, 0, xsize)) {
 
273
                ypos++;
 
274
                ysize--;
 
275
                yoffset++;
 
276
        }
 
277
 
 
278
        /* Crop right columns. */
 
279
        while (xsize > 0 && img->IsEmpty(xpos + xsize - 1, ypos, 0, 1, ysize)) {
 
280
                xsize--;
 
281
        }
 
282
 
 
283
        /* Crop bottom rows. */
 
284
        while (ysize > 0 && img->IsEmpty(xpos, ypos + ysize - 1, 1, 0, xsize)) {
 
285
                ysize--;
 
286
        }
 
287
 
 
288
        if (xsize == 0 || ysize == 0) {
 
289
                free(this->data);
 
290
                this->data = NULL;
 
291
                this->data_size = 0;
 
292
                this->xoffset = 0;
 
293
                this->yoffset = 0;
 
294
                this->width = 0;
 
295
                this->height = 0;
 
296
                return NULL;
 
297
        }
 
298
 
 
299
        this->xoffset = xoffset;
 
300
        this->yoffset = yoffset;
 
301
        this->width = xsize;
 
302
        this->height = ysize;
 
303
 
 
304
        this->row_sizes = (uint16 *)malloc(this->height * sizeof(uint16));
 
305
        if (this->row_sizes == NULL) return "Cannot allocate row sizes";
 
306
 
 
307
        /* Examine the sprite, and record length of data for each row. */
 
308
        this->data_size = 0;
 
309
        for (int y = 0; y < this->height; y++) {
 
310
                int length = 0;
 
311
                int last_stored = 0; // Upto this position (exclusive), the row was counted.
 
312
                for (int x = 0; x < xsize; x++) {
 
313
                        uint8 col = img->GetPixel(xpos + x, ypos + y);
 
314
                        if (col == TRANSPARENT) continue;
 
315
 
 
316
                        int start = x;
 
317
                        x++;
 
318
                        while (x < xsize) {
 
319
                                uint8 col = img->GetPixel(xpos + x, ypos + y);
 
320
                                if (col == TRANSPARENT) break;
 
321
                                x++;
 
322
                        }
 
323
                        /* from 'start' upto and excluding 'x' are pixels to draw. */
 
324
                        while (last_stored + 127 < start) {
 
325
                                length += 2; // 127 pixels gap, 0 pixels to draw.
 
326
                                last_stored += 127;
 
327
                        }
 
328
                        while (x - start > 255) {
 
329
                                length += 2 + 255; // ((start-last_stored) gap, 255 pixels, and 255 colour indices.
 
330
                                start += 255;
 
331
                                last_stored = start;
 
332
                        }
 
333
                        length += 2 + x - start;
 
334
                        last_stored = x;
 
335
                }
 
336
                assert(length <= 0xffff);
 
337
                this->row_sizes[y] = length;
 
338
                this->data_size += length;
 
339
        }
 
340
        if (this->data_size == 0) { // No pixels -> no need to store any data.
 
341
                this->data = NULL;
 
342
                return NULL;
 
343
        }
 
344
 
 
345
        /* Copy sprite pixels. */
 
346
        this->data = (uint8 *)malloc(this->data_size);
 
347
        if (this->data == NULL) return "Cannot allocate sprite pixel memory";
 
348
 
 
349
        uint8 *ptr = this->data;
 
350
        for (int y = 0; y < this->height; y++) {
 
351
                if (this->row_sizes[y] == 0) continue;
 
352
 
 
353
                uint8 *last_header = NULL;
 
354
                int last_stored = 0; // Upto this position (exclusive), the row was counted.
 
355
                for (int x = 0; x < xsize; x++) {
 
356
                        uint8 col = img->GetPixel(xpos + x, ypos + y);
 
357
                        if (col == TRANSPARENT) continue;
 
358
 
 
359
                        int start = x;
 
360
                        x++;
 
361
                        while (x < xsize) {
 
362
                                uint8 col = img->GetPixel(xpos + x, ypos + y);
 
363
                                if (col == TRANSPARENT) break;
 
364
                                x++;
 
365
                        }
 
366
                        /* from 'start' upto and excluding 'x' are pixels to draw. */
 
367
                        while (last_stored + 127 < start) {
 
368
                                *ptr++ = 127; // 127 pixels gap, 0 pixels to draw.
 
369
                                *ptr++ = 0;
 
370
                                last_stored += 127;
 
371
                        }
 
372
                        while (x - start > 255) {
 
373
                                *ptr++ = start - last_stored;
 
374
                                *ptr++ = 255;
 
375
                                for (int i = 0; i < 255; i++) {
 
376
                                        *ptr++ = img->GetPixel(xpos + start, ypos + y);
 
377
                                        start++;
 
378
                                }
 
379
                                last_stored = start;
 
380
                        }
 
381
                        last_header = ptr;
 
382
                        *ptr++ = start - last_stored;
 
383
                        *ptr++ = x - start;
 
384
                        while (x > start) {
 
385
                                *ptr++ = img->GetPixel(xpos + start, ypos + y);
 
386
                                start++;
 
387
                        }
 
388
                        last_stored = x;
 
389
                }
 
390
                assert(last_header != NULL);
 
391
                *last_header |= 128; // This was the last sequence of pixels.
 
392
        }
 
393
        assert(ptr - this->data == this->data_size);
 
394
        return NULL;
 
395
}