-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdk-pixbuf-pvr.cc
551 lines (459 loc) · 14.6 KB
/
gdk-pixbuf-pvr.cc
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* gdk-pixbuf-texture-tool - Play with texture files
*
* Copyright © 2011 Intel Corporation
*
* This software is licensed under the BSD 3-Clause license. See the COPYING
* file for the full text of the license.
*
* Authors: Damien Lespiau <damien.lespiau@intel.com>
*
*/
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#define GDK_PIXBUF_ENABLE_BACKEND
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "gdk-pixbuf-pvr.h"
#include "PVRTexLib.h"
using namespace pvrtexlib;
typedef struct
{
CPVRTexture *decompressed;
} PvrContext;
static const gchar *
standard_pixel_type_to_string (PixelType pixel_type)
{
switch (pixel_type)
{
case eInt8StandardPixelType:
return "R8G8B8A8";
case eInt16StandardPixelType:
return "A16B16G16R16";
case eInt32StandardPixelType:
return "R32G32B32A32";
case eFloatStandardPixelType:
return "R32G32B32A32 (float)";
/* should not happen as we should only be given standard pixel types */
default:
return "Other";
}
}
static void
on_pixbuf_destroyed (guchar *pixels,
gpointer data)
{
PvrContext *context = (PvrContext *) data;
delete context->decompressed;
g_free (context);
}
static GdkPixbuf *
pvrtexlib_gdk_pixbuf_new_from_memory (const guchar *data,
GError **error)
{
CPVRTexture *decompressed;
PvrContext *context;
GdkPixbuf *pixbuf;
PVRTRY
{
PVRTextureUtilities utils;
PixelType pixel_type;
CPVRTexture compressed (data);
decompressed = new CPVRTexture();
utils.DecompressPVR (compressed, *decompressed);
pixel_type = decompressed->getPixelType ();
if (pixel_type != eInt8StandardPixelType)
{
const gchar *type;
type = standard_pixel_type_to_string (pixel_type);
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Image type currently not supported (%s)", type);
return NULL;
}
CPVRTextureData& data = decompressed->getData();
/* FIXME: The data resulting of the decompression will always have alpha.
* might worth repacking the pixbuf to RGB if the original texture did
* not have any alpha before handing the pixbuf back to the user */
context = g_new0 (PvrContext, 1);
context->decompressed = decompressed;
pixbuf = gdk_pixbuf_new_from_data (data.getData(),
GDK_COLORSPACE_RGB,
TRUE,
8,
decompressed->getWidth (),
decompressed->getHeight (),
decompressed->getWidth () * 4,
on_pixbuf_destroyed,
context);
if (compressed.isFlipped ())
{
GdkPixbuf *flipped;
flipped = gdk_pixbuf_flip (pixbuf, FALSE);
g_object_unref (pixbuf);
pixbuf = flipped;
}
}
PVRCATCH(aaaahhh)
{
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
aaaahhh.what());
return NULL;
}
return pixbuf;
}
static GdkPixbuf *
gdk_pixbuf__pvr_image_load (FILE *f,
GError **error)
{
GdkPixbuf *pixbuf;
guchar *content;
GError *decompress_error = NULL;
struct stat st;
int fd;
fd = fileno (f);
if (fd == -1)
{
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Invalid FILE object");
return NULL;
}
if (fstat (fd, &st) == -1)
{
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Failed to get attributes");
return NULL;
}
if (st.st_size == 0 || st.st_size > G_MAXSIZE)
{
content = NULL;
}
else
{
content = (unsigned char *) mmap (NULL, st.st_size, PROT_READ,
MAP_PRIVATE, fd, 0);
}
if (content == NULL || content == MAP_FAILED)
{
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Failed to map file");
return NULL;
}
pixbuf = pvrtexlib_gdk_pixbuf_new_from_memory (content,
&decompress_error);
if (decompress_error)
{
g_propagate_error (error, decompress_error);
return NULL;
}
return pixbuf;
}
static bool
is_p2 (unsigned int x)
{
return ((x != 0) && !(x & (x - 1)));
}
static gboolean
parse_format (const gchar *format,
PixelType *type)
{
if (g_strcmp0 (format, "PVRTC2") == 0)
{
*type = OGL_PVRTC2;
return TRUE;
}
if (g_strcmp0 (format, "PVRTC4") == 0)
{
*type = OGL_PVRTC4;
return TRUE;
}
if (g_strcmp0 (format, "ETC1") == 0)
{
*type = ETC_RGB_4BPP;
return TRUE;
}
*type = ETC_RGB_4BPP;
return FALSE;
}
static gboolean
validate_pixbuf_pvrtc (GdkPixbuf *pixbuf,
GError **error)
{
int width, height;
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
if (!is_p2 (width))
{
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Width needs to be a power of 2");
return FALSE;
}
if (!is_p2 (height))
{
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Height needs to be a power of 2");
return FALSE;
}
return TRUE;
}
static gboolean
gdk_pixbuf__pvr_image_save (FILE *f,
GdkPixbuf *pixbuf,
gchar **param_keys,
gchar **param_values,
GError **error_out)
{
GdkPixbuf *with_alpha = NULL;
PixelType opt_format = ETC_RGB_4BPP;
gboolean valid;
GError *error = NULL;
/* parse the parameters */
if (param_keys)
{
gchar **key_p = param_keys, **value_p = param_values;
while (*key_p)
{
if (g_strcmp0 (*key_p, "format") == 0)
{
if (!parse_format (*value_p, &opt_format))
{
g_set_error (error_out,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Invalid format %s", *value_p);
return FALSE;
}
}
else
{
g_warning ("Unknown option %s", *key_p);
}
key_p++;
value_p++;
}
}
/* validate the pixbuf if needed */
switch (opt_format)
{
case OGL_PVRTC2:
case OGL_PVRTC4:
valid = validate_pixbuf_pvrtc (pixbuf, &error);
break;
default:
valid = TRUE;
}
if (!valid)
{
g_propagate_error (error_out, error);
return FALSE;
}
PVRTRY
{
PVRTextureUtilities utils;
int width, height;
guchar *pixels;
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
/* The standard format that PVRTexLib takes is RGBA 8888 so we need
* to add an alpha channel if the original image does not have one */
if (!gdk_pixbuf_get_has_alpha (pixbuf))
{
with_alpha = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0);
pixbuf = with_alpha;
}
if (gdk_pixbuf_get_rowstride (pixbuf) != 4 * width)
{
g_set_error_literal (error_out,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"A row stride larger than the width is not "
"allowed");
if (with_alpha)
g_object_unref (with_alpha);
return FALSE;
}
/* make a CPVRTexture instance from the GdkPixbuf */
pixels = gdk_pixbuf_get_pixels (pixbuf);
CPVRTexture uncompressed (width,
height,
0, /* u32MipMapCount */
1, /* u32NumSurfaces */
false, /* bBorder */
false, /* bTwiddled */
false, /* bCubeMap */
false, /* bVolume */
false, /* bFalseMips */
true, /* bHasAlpha */
false, /* bFlipped */
eInt8StandardPixelType, /* ePixelType */
0.0f, /* fNormalMap */
pixels); /* pPixelData */
/* create texture to encode to */
CPVRTexture compressed (uncompressed.getHeader());
/* FIXME: Remove the alpha channel from the compressed texture is the
* original GdkPixbuf does not have alpha (But we still need to create
* the uncompressed texture with hasAlpha to TRUE as the pixbuf is 4
* bytes per pixel anyway */
/* TODO: Add support for generating the mipmaps */
/* TODO: Add support for selection with compression we want */
/* set required encoded pixel type */
compressed.setPixelType (opt_format);
/* encode texture */
utils.CompressPVR (uncompressed, compressed);
/* write to file */
compressed.getHeader().writeToFile (f);
compressed.getData().writeToFile (f);
}
PVRCATCH(aaaahhh)
{
g_set_error_literal (error_out,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
aaaahhh.what());
if (with_alpha)
g_object_unref (with_alpha);
return FALSE;
}
if (with_alpha)
g_object_unref (with_alpha);
return TRUE;
}
/*
* Note: The library provided by Imagination does not seem to provide anything
* that would allow us to support "incremental loading". What we can do though
* is to fake incremental loading by accumulating the data inside an array and
* decode it at the end. This allows us to work with GdkPixbufLoader (which a
* lot of apps use) at the very least we can signal the size of the level 0
* mipmap after having read the header.
*/
typedef struct
{
GdkPixbufModuleSizeFunc size_func;
GdkPixbufModulePreparedFunc prepared_func;
GdkPixbufModuleUpdatedFunc updated_func;
gpointer user_data;
GArray *buffer;
guint got_header : 1;
} PvrIncContext;
static gpointer
gdk_pixbuf__pvr_begin_load (GdkPixbufModuleSizeFunc size_func,
GdkPixbufModulePreparedFunc prepared_func,
GdkPixbufModuleUpdatedFunc updated_func,
gpointer user_data,
GError **error)
{
PvrIncContext *context;
context = g_new0 (PvrIncContext, 1);
context->size_func = size_func;
context->prepared_func = prepared_func;
context->updated_func = updated_func;
context->user_data = user_data;
/* initialized so can contain a 512x512 image with 4 bits per pixel without
* the need to reallocate the buffer */
context->buffer = g_array_sized_new (FALSE, FALSE, 1, 512 * 512 / 2);
return context;
}
static gboolean
gdk_pixbuf__pvr_stop_load (gpointer contextp,
GError **error)
{
PvrIncContext *context = (PvrIncContext *) contextp;
GdkPixbuf *pixbuf;
GError *decompress_error = NULL;
pixbuf =
pvrtexlib_gdk_pixbuf_new_from_memory ((guchar *)context->buffer->data,
&decompress_error);
if (decompress_error)
{
g_propagate_error (error, decompress_error);
return FALSE;
}
if (context->prepared_func)
context->prepared_func (pixbuf, NULL, context->user_data);
g_free (context);
return TRUE;
}
static gboolean
gdk_pixbuf__pvr_load_increment (gpointer contextp,
const guchar *buf,
guint size,
GError **error)
{
PvrIncContext *context = (PvrIncContext *) contextp;
g_array_append_vals (context->buffer, buf, size);
if (!context->got_header && context->buffer->len >= sizeof (PVRHeader))
{
context->got_header = TRUE;
if (context->size_func)
{
gint width, height;
PVRHeader *header;
header = (PVRHeader *) context->buffer->data;
width = header->width;
height = header->height;
(*context->size_func) (&width, &height, context->user_data);
if (width == 0 || height == 0)
{
/* used to signal we are going to stop loading the image, return
* an error for good measure and not fall in the case we return
* FALSE without an error set */
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
"Zero width or height requested");
return FALSE;
}
}
}
return TRUE;
}
extern "C" {
G_MODULE_EXPORT void
fill_vtable (GdkPixbufModule *module)
{
module->load = gdk_pixbuf__pvr_image_load;
module->save = gdk_pixbuf__pvr_image_save;
module->begin_load = gdk_pixbuf__pvr_begin_load;
module->stop_load = gdk_pixbuf__pvr_stop_load;
module->load_increment = gdk_pixbuf__pvr_load_increment;
}
G_MODULE_EXPORT void
fill_info (GdkPixbufFormat *info)
{
static GdkPixbufModulePattern signature_new[] = {
{ NULL, NULL, 0 }
};
static gchar *mime_types[] =
{
"image/x-pvr",
NULL
};
static gchar *extensions[] =
{
"pvr",
NULL
};
info->name = "pvr";
info->description = "PVR image";
info->signature = signature_new;
info->mime_types = mime_types;
info->extensions = extensions;
info->flags = GDK_PIXBUF_FORMAT_WRITABLE;
info->license = "BSD";
}
} /* extern "C" */