-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathimage.c
2412 lines (2014 loc) · 64.9 KB
/
image.c
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1996-2003 A Nourai, Id Software, Inc.
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 included (GNU.txt) 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.
*/
#ifdef __FreeBSD__
#include <dlfcn.h>
#endif
#include "quakedef.h"
#include "image.h"
#ifdef WITH_PNG
#include "png.h"
/*#ifdef _WIN32
#pragma comment(lib, "libs/libpng.lib")
#endif*/
#endif
#ifdef WITH_JPEG
#include "jpeglib.h"
#include "jerror.h"
/*#ifdef _WIN32
#pragma comment(lib, "libs/libjpeg.lib")
#endif*/
#endif
#define IMAGE_MAX_DIMENSIONS 4096
cvar_t image_png_compression_level = {"image_png_compression_level", "1"};
cvar_t image_jpeg_quality_level = {"image_jpeg_quality_level", "75"};
/***************************** IMAGE RESAMPLING ******************************/
static void Image_Resample32LerpLine (byte *in, byte *out, int inwidth, int outwidth)
{
int j, xi, oldx = 0, f, fstep, endx, lerp;
fstep = (int) (inwidth * 65536.0f / outwidth);
endx = (inwidth - 1);
for (j = 0, f = 0; j < outwidth; j++, f += fstep) {
xi = (int) f >> 16;
if (xi != oldx) {
in += (xi - oldx) * 4;
oldx = xi;
}
if (xi < endx) {
lerp = f & 0xFFFF;
*out++ = (byte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
*out++ = (byte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
*out++ = (byte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
*out++ = (byte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
} else {
*out++ = in[0];
*out++ = in[1];
*out++ = in[2];
*out++ = in[3];
}
}
}
static void Image_Resample24LerpLine (byte *in, byte *out, int inwidth, int outwidth)
{
int j, xi, oldx = 0, f, fstep, endx, lerp;
fstep = (int) (inwidth * 65536.0f / outwidth);
endx = (inwidth - 1);
for (j = 0, f = 0; j < outwidth; j++, f += fstep) {
xi = (int) f >> 16;
if (xi != oldx) {
in += (xi - oldx) * 3;
oldx = xi;
}
if (xi < endx) {
lerp = f & 0xFFFF;
*out++ = (byte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
*out++ = (byte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
*out++ = (byte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
} else {
*out++ = in[0];
*out++ = in[1];
*out++ = in[2];
}
}
}
#define LERPBYTE(i) r = row1[i]; out[i] = (byte) ((((row2[i] - r) * lerp) >> 16) + r)
#define NOLERPBYTE(i) *out++ = inrow[f + i]
static void Image_Resample32 (void *indata, int inwidth, int inheight,
void *outdata, int outwidth, int outheight, int quality)
{
if (quality)
{
int i, j, r, yi, oldy, f, fstep, endy = (inheight - 1), lerp;
int inwidth4 = inwidth * 4, outwidth4 = outwidth * 4;
byte *inrow, *out, *row1, *row2, *memalloc;
out = outdata;
fstep = (int) (inheight * 65536.0f / outheight);
memalloc = (byte *) Q_malloc(2 * outwidth4);
row1 = memalloc;
row2 = memalloc + outwidth4;
inrow = (byte *) indata;
oldy = 0;
Image_Resample32LerpLine (inrow, row1, inwidth, outwidth);
Image_Resample32LerpLine (inrow + inwidth4, row2, inwidth, outwidth);
for (i = 0, f = 0; i < outheight; i++, f += fstep)
{
yi = f >> 16;
if (yi < endy)
{
lerp = f & 0xFFFF;
if (yi != oldy)
{
inrow = (byte *) indata + inwidth4 * yi;
if (yi == oldy + 1)
memcpy(row1, row2, outwidth4);
else
Image_Resample32LerpLine (inrow, row1, inwidth, outwidth);
Image_Resample32LerpLine (inrow + inwidth4, row2, inwidth, outwidth);
oldy = yi;
}
j = outwidth - 4;
while(j >= 0)
{
LERPBYTE(0); LERPBYTE(1); LERPBYTE(2); LERPBYTE(3);
LERPBYTE(4); LERPBYTE(5); LERPBYTE(6); LERPBYTE(7);
LERPBYTE(8); LERPBYTE(9); LERPBYTE(10); LERPBYTE(11);
LERPBYTE(12); LERPBYTE(13); LERPBYTE(14); LERPBYTE(15);
out += 16;
row1 += 16;
row2 += 16;
j -= 4;
}
if (j & 2)
{
LERPBYTE(0); LERPBYTE(1); LERPBYTE(2); LERPBYTE(3);
LERPBYTE(4); LERPBYTE(5); LERPBYTE(6); LERPBYTE(7);
out += 8;
row1 += 8;
row2 += 8;
}
if (j & 1)
{
LERPBYTE(0); LERPBYTE(1); LERPBYTE(2); LERPBYTE(3);
out += 4;
row1 += 4;
row2 += 4;
}
row1 -= outwidth4;
row2 -= outwidth4;
}
else
{
if (yi != oldy)
{
inrow = (byte *) indata + inwidth4 * yi;
if (yi == oldy+1)
memcpy(row1, row2, outwidth4);
else
Image_Resample32LerpLine (inrow, row1, inwidth, outwidth);
oldy = yi;
}
memcpy(out, row1, outwidth4);
}
}
Q_free(memalloc);
}
else
{
int i, j;
unsigned int frac, fracstep, *inrow, *out;
out = outdata;
fracstep = inwidth * 0x10000 / outwidth;
for (i = 0; i < outheight; i++)
{
inrow = (unsigned int *) indata + inwidth * (i * inheight / outheight);
frac = fracstep >> 1;
j = outwidth - 4;
while (j >= 0)
{
out[0] = inrow[frac >> 16]; frac += fracstep;
out[1] = inrow[frac >> 16]; frac += fracstep;
out[2] = inrow[frac >> 16]; frac += fracstep;
out[3] = inrow[frac >> 16]; frac += fracstep;
out += 4;
j -= 4;
}
if (j & 2)
{
out[0] = inrow[frac >> 16]; frac += fracstep;
out[1] = inrow[frac >> 16]; frac += fracstep;
out += 2;
}
if (j & 1)
{
out[0] = inrow[frac >> 16]; frac += fracstep;
out += 1;
}
}
}
}
static void Image_Resample24 (void *indata, int inwidth, int inheight,
void *outdata, int outwidth, int outheight, int quality)
{
if (quality)
{
int i, j, r, yi, oldy, f, fstep, endy = (inheight - 1), lerp;
int inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
byte *inrow, *out, *row1, *row2, *memalloc;
out = outdata;
fstep = (int) (inheight * 65536.0f / outheight);
memalloc = (byte *) Q_malloc(2 * outwidth3);
row1 = memalloc;
row2 = memalloc + outwidth3;
inrow = (byte *) indata;
oldy = 0;
Image_Resample24LerpLine (inrow, row1, inwidth, outwidth);
Image_Resample24LerpLine (inrow + inwidth3, row2, inwidth, outwidth);
for (i = 0, f = 0; i < outheight; i++, f += fstep)
{
yi = f >> 16;
if (yi < endy) {
lerp = f & 0xFFFF;
if (yi != oldy)
{
inrow = (byte *) indata + inwidth3 * yi;
if (yi == oldy + 1)
memcpy(row1, row2, outwidth3);
else
Image_Resample24LerpLine (inrow, row1, inwidth, outwidth);
Image_Resample24LerpLine (inrow + inwidth3, row2, inwidth, outwidth);
oldy = yi;
}
j = outwidth - 4;
while(j >= 0)
{
LERPBYTE(0); LERPBYTE(1); LERPBYTE(2);
LERPBYTE(3); LERPBYTE(4); LERPBYTE(5);
LERPBYTE(6); LERPBYTE(7); LERPBYTE(8);
LERPBYTE(9); LERPBYTE(10); LERPBYTE(11);
out += 12;
row1 += 12;
row2 += 12;
j -= 4;
}
if (j & 2)
{
LERPBYTE(0); LERPBYTE(1); LERPBYTE(2);
LERPBYTE(3); LERPBYTE(4); LERPBYTE(5);
out += 6;
row1 += 6;
row2 += 6;
}
if (j & 1)
{
LERPBYTE(0); LERPBYTE(1); LERPBYTE(2);
out += 3;
row1 += 3;
row2 += 3;
}
row1 -= outwidth3;
row2 -= outwidth3;
}
else
{
if (yi != oldy)
{
inrow = (byte *) indata + inwidth3 * yi;
if (yi == oldy+1)
memcpy(row1, row2, outwidth3);
else
Image_Resample24LerpLine (inrow, row1, inwidth, outwidth);
oldy = yi;
}
memcpy(out, row1, outwidth3);
}
}
Q_free(memalloc);
}
else
{
int i, j, f;
unsigned int frac, fracstep, inwidth3 = inwidth * 3;
byte *inrow, *out;
out = outdata;
fracstep = inwidth * 0x10000 / outwidth;
for (i = 0; i < outheight; i++)
{
inrow = (byte *) indata + inwidth3 * (i * inheight / outheight);
frac = fracstep >> 1;
j = outwidth - 4;
while (j >= 0)
{
f = (frac >> 16) * 3; NOLERPBYTE(0); NOLERPBYTE(1); NOLERPBYTE(2); frac += fracstep;
f = (frac >> 16) * 3; NOLERPBYTE(0); NOLERPBYTE(1); NOLERPBYTE(2); frac += fracstep;
f = (frac >> 16) * 3; NOLERPBYTE(0); NOLERPBYTE(1); NOLERPBYTE(2); frac += fracstep;
f = (frac >> 16) * 3; NOLERPBYTE(0); NOLERPBYTE(1); NOLERPBYTE(2); frac += fracstep;
j -= 4;
}
if (j & 2)
{
f = (frac >> 16) * 3; NOLERPBYTE(0); NOLERPBYTE(1); NOLERPBYTE(2); frac += fracstep;
f = (frac >> 16) * 3; NOLERPBYTE(0); NOLERPBYTE(1); NOLERPBYTE(2); frac += fracstep;
out += 2;
}
if (j & 1)
{
f = (frac >> 16) * 3; NOLERPBYTE(0); NOLERPBYTE(1); NOLERPBYTE(2); frac += fracstep;
out += 1;
}
}
}
}
void Image_Resample (void *indata, int inwidth, int inheight,
void *outdata, int outwidth, int outheight, int bpp, int quality)
{
if (bpp == 4)
Image_Resample32(indata, inwidth, inheight, outdata, outwidth, outheight, quality);
else if (bpp == 3)
Image_Resample24(indata, inwidth, inheight, outdata, outwidth, outheight, quality);
else
Sys_Error("Image_Resample: unsupported bpp (%d)", bpp);
}
void Image_MipReduce (const byte *in, byte *out, int *width, int *height, int bpp)
{
const byte *inrow;
int x, y, nextrow;
inrow = in;
nextrow = *width * bpp;
if (*width > 1)
{
*width >>= 1;
if (*height > 1)
{
// reduce both (width and height)
*height >>= 1;
if (bpp == 4)
{
for (y = 0; y < *height; y++, inrow += nextrow * 2)
{
for (in = inrow, x = 0; x < *width; x++)
{
out[0] = (byte) ((in[0] + in[4] + in[nextrow] + in[nextrow + 4]) >> 2);
out[1] = (byte) ((in[1] + in[5] + in[nextrow + 1] + in[nextrow + 5]) >> 2);
out[2] = (byte) ((in[2] + in[6] + in[nextrow + 2] + in[nextrow + 6]) >> 2);
out[3] = (byte) ((in[3] + in[7] + in[nextrow + 3] + in[nextrow + 7]) >> 2);
out += 4;
in += 8;
}
}
}
else if (bpp == 3)
{
for (y = 0; y < *height; y++, inrow += nextrow * 2)
{
for (in = inrow, x = 0; x < *width; x++)
{
out[0] = (byte) ((in[0] + in[3] + in[nextrow] + in[nextrow + 3]) >> 2);
out[1] = (byte) ((in[1] + in[4] + in[nextrow + 1] + in[nextrow + 4]) >> 2);
out[2] = (byte) ((in[2] + in[5] + in[nextrow + 2] + in[nextrow + 5]) >> 2);
out += 3;
in += 6;
}
}
}
else
{
Sys_Error("Image_MipReduce: unsupported bpp (%d)", bpp);
}
}
else
{
// reduce width
if (bpp == 4)
{
for (y = 0; y < *height; y++, inrow += nextrow)
{
for (in = inrow, x = 0; x < *width; x++)
{
out[0] = (byte) ((in[0] + in[4]) >> 1);
out[1] = (byte) ((in[1] + in[5]) >> 1);
out[2] = (byte) ((in[2] + in[6]) >> 1);
out[3] = (byte) ((in[3] + in[7]) >> 1);
out += 4;
in += 8;
}
}
}
else if (bpp == 3)
{
for (y = 0; y < *height; y++, inrow += nextrow)
{
for (in = inrow, x = 0; x < *width; x++)
{
out[0] = (byte) ((in[0] + in[3]) >> 1);
out[1] = (byte) ((in[1] + in[4]) >> 1);
out[2] = (byte) ((in[2] + in[5]) >> 1);
out += 3;
in += 6;
}
}
}
else
{
Sys_Error("Image_MipReduce: unsupported bpp (%d)", bpp);
}
}
}
else if (*height > 1)
{
// reduce height
*height >>= 1;
if (bpp == 4)
{
for (y = 0; y < *height; y++, inrow += nextrow * 2)
{
for (in = inrow, x = 0; x < *width; x++)
{
out[0] = (byte) ((in[0] + in[nextrow]) >> 1);
out[1] = (byte) ((in[1] + in[nextrow + 1]) >> 1);
out[2] = (byte) ((in[2] + in[nextrow + 2]) >> 1);
out[3] = (byte) ((in[3] + in[nextrow + 3]) >> 1);
out += 4;
in += 4;
}
}
}
else if (bpp == 3)
{
for (y = 0; y < *height; y++, inrow += nextrow * 2)
{
for (in = inrow, x = 0; x < *width; x++)
{
out[0] = (byte) ((in[0] + in[nextrow]) >> 1);
out[1] = (byte) ((in[1] + in[nextrow + 1]) >> 1);
out[2] = (byte) ((in[2] + in[nextrow + 2]) >> 1);
out += 3;
in += 3;
}
}
}
else
{
Sys_Error("Image_MipReduce: unsupported bpp (%d)", bpp);
}
}
else
{
Sys_Error("Image_MipReduce: Input texture has dimensions %dx%d", width, height);
}
}
/************************************ PNG ************************************/
#ifdef WITH_PNG
// FIXME: If we still want code for dynamically loading pnglib we should make it use the same function for loading, so that stuff doesn't have to be changed in two places like now...
#ifndef WITH_PNG_STATIC
static QLIB_HANDLETYPE_T png_handle = NULL;
static QLIB_HANDLETYPE_T zlib_handle = NULL;
static void (*qpng_set_sig_bytes)(png_structp, int);
static int (*qpng_sig_cmp)(png_bytep, png_size_t, png_size_t);
static png_structp (*qpng_create_read_struct)(png_const_charp, png_voidp, png_error_ptr, png_error_ptr);
static png_structp (*qpng_create_write_struct)(png_const_charp, png_voidp, png_error_ptr, png_error_ptr);
static png_infop (*qpng_create_info_struct)(png_structp);
static void (*qpng_write_info)(png_structp, png_infop);
static void (*qpng_read_info)(png_structp, png_infop);
static void (*qpng_set_expand)(png_structp);
#ifdef __Q_PNG14__
static void (*qpng_set_expand_gray_1_2_4_to_8)(png_structp);
#else
static void (*qpng_set_gray_1_2_4_to_8)(png_structp);
#endif
static void (*qpng_set_palette_to_rgb)(png_structp);
static void (*qpng_set_tRNS_to_alpha)(png_structp);
static void (*qpng_set_gray_to_rgb)(png_structp);
static void (*qpng_set_filler)(png_structp, png_uint_32, int);
static void (*qpng_set_strip_16)(png_structp);
static void (*qpng_read_update_info)(png_structp, png_infop);
static void (*qpng_read_image)(png_structp, png_bytepp);
static void (*qpng_write_image)(png_structp, png_bytepp);
static void (*qpng_write_end)(png_structp, png_infop);
static void (*qpng_read_end)(png_structp, png_infop);
static void (*qpng_destroy_read_struct)(png_structpp, png_infopp, png_infopp);
static void (*qpng_destroy_write_struct)(png_structpp, png_infopp);
static void (*qpng_set_compression_level)(png_structp, int);
static void (*qpng_set_write_fn)(png_structp, png_voidp, png_rw_ptr, png_flush_ptr);
static void (*qpng_set_read_fn)(png_structp, png_voidp, png_rw_ptr);
static png_voidp (*qpng_get_io_ptr)(png_structp);
static png_uint_32 (*qpng_get_valid)(png_structp, png_infop, png_uint_32);
static png_uint_32 (*qpng_get_rowbytes)(png_structp, png_infop);
static png_byte (*qpng_get_channels)(png_structp, png_infop);
static png_byte (*qpng_get_bit_depth)(png_structp, png_infop);
static png_uint_32 (*qpng_get_IHDR)(png_structp, png_infop, png_uint_32 *, png_uint_32 *, int *, int *, int *, int *, int *);
static void (*qpng_set_IHDR)(png_structp, png_infop, png_uint_32, png_uint_32, int, int, int, int, int);
static void (*qpng_set_PLTE)(png_structp, png_infop, png_colorp, int);
#define NUM_PNGPROCS (sizeof(pngprocs)/sizeof(pngprocs[0]))
qlib_dllfunction_t pngprocs[] = {
{"png_set_sig_bytes", (void **) &qpng_set_sig_bytes},
{"png_sig_cmp", (void **) &qpng_sig_cmp},
{"png_create_read_struct", (void **) &qpng_create_read_struct},
{"png_create_write_struct", (void **) &qpng_create_write_struct},
{"png_create_info_struct", (void **) &qpng_create_info_struct},
{"png_write_info", (void **) &qpng_write_info},
{"png_read_info", (void **) &qpng_read_info},
{"png_set_expand", (void **) &qpng_set_expand},
#ifdef __Q_PNG14__
{"png_set_expand_gray_1_2_4_to_8", (void **) &qpng_set_expand_gray_1_2_4_to_8},
#else
{"png_set_gray_1_2_4_to_8", (void **) &qpng_set_gray_1_2_4_to_8},
#endif
{"png_set_palette_to_rgb", (void **) &qpng_set_palette_to_rgb},
{"png_set_tRNS_to_alpha", (void **) &qpng_set_tRNS_to_alpha},
{"png_set_gray_to_rgb", (void **) &qpng_set_gray_to_rgb},
{"png_set_filler", (void **) &qpng_set_filler},
{"png_set_strip_16", (void **) &qpng_set_strip_16},
{"png_read_update_info", (void **) &qpng_read_update_info},
{"png_read_image", (void **) &qpng_read_image},
{"png_write_image", (void **) &qpng_write_image},
{"png_write_end", (void **) &qpng_write_end},
{"png_read_end", (void **) &qpng_read_end},
{"png_destroy_read_struct", (void **) &qpng_destroy_read_struct},
{"png_destroy_write_struct", (void **) &qpng_destroy_write_struct},
{"png_set_compression_level", (void **) &qpng_set_compression_level},
{"png_set_write_fn", (void **) &qpng_set_write_fn},
{"png_set_read_fn", (void **) &qpng_set_read_fn},
{"png_get_io_ptr", (void **) &qpng_get_io_ptr},
{"png_get_valid", (void **) &qpng_get_valid},
{"png_get_rowbytes", (void **) &qpng_get_rowbytes},
{"png_get_channels", (void **) &qpng_get_channels},
{"png_get_bit_depth", (void **) &qpng_get_bit_depth},
{"png_get_IHDR", (void **) &qpng_get_IHDR},
{"png_set_IHDR", (void **) &qpng_set_IHDR},
{"png_set_PLTE", (void **) &qpng_set_PLTE},
};
static void PNG_FreeLibrary(void) {
if (png_handle)
QLIB_FREELIBRARY(png_handle);
if (zlib_handle)
QLIB_FREELIBRARY(zlib_handle);
}
static qbool PNG_LoadLibrary(void) {
if (COM_CheckParm("-nolibpng"))
return false;
#ifdef _WIN32
if (!(png_handle = LoadLibrary("libpng.dll"))) {
#else
#ifdef __APPLE__
if (!(png_handle = dlopen("libpng12.dylib", RTLD_NOW))) {
if (!(zlib_handle = dlopen("libz.dylib", RTLD_NOW))) {
#else
if (!(png_handle = dlopen("libpng12.so.0", RTLD_NOW)) && !(png_handle = dlopen("libpng.so", RTLD_NOW))) {
if (!(zlib_handle = dlopen("libz.so", RTLD_NOW | RTLD_GLOBAL))) {
#endif
QLib_MissingModuleError(QLIB_ERROR_MODULE_NOT_FOUND, "libz", "-nolibpng", "png image features");
png_handle = zlib_handle = NULL;
return false;
}
#ifdef __APPLE__
if (!(png_handle = dlopen("libpng12.dylib", RTLD_NOW)))
#else
if (!(png_handle = dlopen("libpng12.so.0", RTLD_NOW)) && !(png_handle = dlopen("libpng.so", RTLD_NOW)))
#endif
#endif
{
PNG_FreeLibrary();
QLib_MissingModuleError(QLIB_ERROR_MODULE_NOT_FOUND, "libpng", "-nolibpng", "png image features");
png_handle = zlib_handle = NULL;
return false;
}
}
if (!QLib_ProcessProcdef(png_handle, pngprocs, NUM_PNGPROCS)) {
PNG_FreeLibrary();
QLib_MissingModuleError(QLIB_ERROR_MODULE_MISSING_PROC, "libpng", "-nolibpng", "png image features");
png_handle = zlib_handle = NULL;
return false;
}
return true;
}
static void PNG_IO_user_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
vfsfile_t *v = (vfsfile_t *) qpng_get_io_ptr(png_ptr);
vfserrno_t err;
VFS_READ(v, data, length, &err);
}
static void PNG_IO_user_write_data(png_structp png_ptr, png_bytep data, png_size_t length) {
vfsfile_t *v = (vfsfile_t *) qpng_get_io_ptr(png_ptr);
VFS_WRITE(v, data, length);
}
static void PNG_IO_user_flush_data(png_structp png_ptr) {
vfsfile_t *v = (vfsfile_t *) qpng_get_io_ptr(png_ptr);
VFS_FLUSH(v);
}
byte *Image_LoadPNG (vfsfile_t *fin, const char *filename, int matchwidth, int matchheight, int *real_width, *real_height)
{
byte header[8], **rowpointers, *data;
png_structp png_ptr;
png_infop pnginfo;
int y, width, height, bitdepth, colortype, interlace, compression, filter, bytesperpixel;
unsigned long rowbytes;
if (!png_handle)
return NULL;
if (!fin && !(fin = FS_OpenVFS(filename, "rb", FS_ANY)))
return NULL;
if (fread(header, 1, 8, fin) != 8) {
VFS_CLOSE(fin);
return NULL;
}
if (qpng_sig_cmp(header, 0, 8)) {
Com_DPrintf ("Invalid PNG image %s\n", COM_SkipPath(filename));
VFS_CLOSE(fin);
return NULL;
}
if (!(png_ptr = qpng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
VFS_CLOSE(fin);
return NULL;
}
if (!(pnginfo = qpng_create_info_struct(png_ptr))) {
qpng_destroy_read_struct(&png_ptr, &pnginfo, NULL);
VFS_CLOSE(fin);
return NULL;
}
if (setjmp(png_ptr->jmpbuf)) {
qpng_destroy_read_struct(&png_ptr, &pnginfo, NULL);
VFS_CLOSE(fin);
return NULL;
}
qpng_set_read_fn(png_ptr, fin, PNG_IO_user_read_data);
qpng_set_sig_bytes(png_ptr, 8);
qpng_read_info(png_ptr, pnginfo);
qpng_get_IHDR(png_ptr, pnginfo, (png_uint_32 *) &width, (png_uint_32 *) &height, &bitdepth,
&colortype, &interlace, &compression, &filter);
// Return the width and height.
if (real_width)
(*real_width) = width;
if (real_height)
(*real_height) = height;
if (width > IMAGE_MAX_DIMENSIONS || height > IMAGE_MAX_DIMENSIONS) {
Com_DPrintf ("PNG image %s exceeds maximum supported dimensions\n", COM_SkipPath(filename));
qpng_destroy_read_struct(&png_ptr, &pnginfo, NULL);
VFS_CLOSE(fin);
return NULL;
}
if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight)) {
qpng_destroy_read_struct(&png_ptr, &pnginfo, NULL);
VFS_CLOSE(fin);
return NULL;
}
if (colortype == PNG_COLOR_TYPE_PALETTE) {
qpng_set_palette_to_rgb(png_ptr);
qpng_set_filler(png_ptr, 255, PNG_FILLER_AFTER);
}
if (colortype == PNG_COLOR_TYPE_GRAY && bitdepth < 8)
#ifdef __Q_PNG14__
qpng_set_expand_gray_1_2_4_to_8(png_ptr);
#else
qpng_set_gray_1_2_4_to_8(png_ptr);
#endif
if (qpng_get_valid(png_ptr, pnginfo, PNG_INFO_tRNS))
qpng_set_tRNS_to_alpha(png_ptr);
if (colortype == PNG_COLOR_TYPE_GRAY || colortype == PNG_COLOR_TYPE_GRAY_ALPHA)
qpng_set_gray_to_rgb(png_ptr);
if (colortype != PNG_COLOR_TYPE_RGBA)
qpng_set_filler(png_ptr, 255, PNG_FILLER_AFTER);
if (bitdepth < 8)
qpng_set_expand (png_ptr);
else if (bitdepth == 16)
qpng_set_strip_16(png_ptr);
qpng_read_update_info(png_ptr, pnginfo);
rowbytes = qpng_get_rowbytes(png_ptr, pnginfo);
bytesperpixel = qpng_get_channels(png_ptr, pnginfo);
bitdepth = qpng_get_bit_depth(png_ptr, pnginfo);
if (bitdepth != 8 || bytesperpixel != 4) {
Com_DPrintf ("Unsupported PNG image %s: Bad color depth and/or bpp\n", COM_SkipPath(filename));
qpng_destroy_read_struct(&png_ptr, &pnginfo, NULL);
VFS_CLOSE(fin);
return NULL;
}
data = (byte *) Q_malloc(height * rowbytes );
rowpointers = (byte **) Q_malloc(height * sizeof(*rowpointers));
for (y = 0; y < height; y++)
rowpointers[y] = data + y * rowbytes;
qpng_read_image(png_ptr, rowpointers);
qpng_read_end(png_ptr, NULL);
qpng_destroy_read_struct(&png_ptr, &pnginfo, NULL);
Q_free(rowpointers);
VFS_CLOSE(fin);
return data;
}
int Image_WritePNG (char *filename, int compression, byte *pixels, int width, int height) {
char name[MAX_PATH];
int i, bpp = 3, pngformat, width_sign;
vfsfile_t *fp;
png_structp png_ptr;
png_infop info_ptr;
png_byte **rowpointers;
snprintf (name, sizeof(name), "%s", filename);
if (!png_handle)
return false;
width_sign = (width < 0) ? -1 : 1;
width = abs(width);
if (!(fp = FS_OpenVFS(name, "wb", FS_NONE_OS))) {
COM_CreatePath (name);
if (!(fp = FS_OpenVFS(name, "wb", FS_NONE_OS)))
return false;
}
if (!(png_ptr = qpng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
VFS_CLOSE(fp);
return false;
}
if (!(info_ptr = qpng_create_info_struct(png_ptr))) {
qpng_destroy_write_struct(&png_ptr, (png_infopp) NULL);
VFS_CLOSE(fp);
return false;
}
if (setjmp(png_ptr->jmpbuf)) {
qpng_destroy_write_struct(&png_ptr, &info_ptr);
VFS_CLOSE(fp);
return false;
}
qpng_set_write_fn(png_ptr, fp, PNG_IO_user_write_data, PNG_IO_user_flush_data);
qpng_set_compression_level(png_ptr, bound(Z_NO_COMPRESSION, compression, Z_BEST_COMPRESSION));
pngformat = (bpp == 4) ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB;
qpng_set_IHDR(png_ptr, info_ptr, width, height, 8, pngformat,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
qpng_write_info(png_ptr, info_ptr);
rowpointers = (png_byte **) Q_malloc (height * sizeof(*rowpointers));
for (i = 0; i < height; i++)
rowpointers[i] = pixels + i * width_sign * width * bpp;
qpng_write_image(png_ptr, rowpointers);
qpng_write_end(png_ptr, info_ptr);
Q_free(rowpointers);
qpng_destroy_write_struct(&png_ptr, &info_ptr);
VFS_CLOSE(fp);
return true;
}
int Image_WritePNGPLTE (char *filename, int compression,
byte *pixels, int width, int height, byte *palette)
{
int rowbytes = width;
int i;
char name[MAX_PATH];
vfsfile_t *fp;
png_structp png_ptr;
png_infop info_ptr;
png_byte **rowpointers;
if (!png_handle)
return false;
snprintf (name, sizeof(name), "%s", filename);
if (!(fp = FS_OpenVFS(name, "wb", FS_NONE_OS))) {
COM_CreatePath (name);
if (!(fp = FS_OpenVFS(name, "wb", FS_NONE_OS)))
return false;
}
if (!(png_ptr = qpng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
VFS_CLOSE(fp);
return false;
}
if (!(info_ptr = qpng_create_info_struct(png_ptr))) {
qpng_destroy_write_struct(&png_ptr, (png_infopp) NULL);
VFS_CLOSE(fp);
return false;
}
if (setjmp(png_ptr->jmpbuf)) {
qpng_destroy_write_struct(&png_ptr, &info_ptr);
VFS_CLOSE(fp);
return false;
}
qpng_set_write_fn(png_ptr, fp, PNG_IO_user_write_data, PNG_IO_user_flush_data);
qpng_set_compression_level(png_ptr, bound(Z_NO_COMPRESSION, compression, Z_BEST_COMPRESSION));
qpng_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
qpng_set_PLTE(png_ptr, info_ptr, (png_color *) palette, 256);
qpng_write_info(png_ptr, info_ptr);
rowpointers = (png_byte **) Q_malloc (height * sizeof(*rowpointers));
for (i = 0; i < height; i++)
rowpointers[i] = pixels + i * rowbytes;
qpng_write_image(png_ptr, rowpointers);
qpng_write_end(png_ptr, info_ptr);
Q_free(rowpointers);
qpng_destroy_write_struct(&png_ptr, &info_ptr);
VFS_CLOSE(fp);
return true;
}
#else
static void PNG_IO_user_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
vfsfile_t *v = (vfsfile_t *) png_get_io_ptr(png_ptr);
vfserrno_t err;
VFS_READ(v, data, length, &err);
}
static void PNG_IO_user_write_data(png_structp png_ptr, png_bytep data, png_size_t length) {
vfsfile_t *v = (vfsfile_t *) png_get_io_ptr(png_ptr);
VFS_WRITE(v, data, length);
}
static void PNG_IO_user_flush_data(png_structp png_ptr) {
vfsfile_t *v = (vfsfile_t *) png_get_io_ptr(png_ptr);
VFS_FLUSH(v);
}
#define PNG_HEADER_LENGTH 8
#define PNG_LOAD_TEXT 1
#define PNG_LOAD_DATA 2
static qbool PNG_HasHeader (vfsfile_t *fin)
{
byte header[PNG_HEADER_LENGTH];
vfserrno_t err;
VFS_READ(fin, header, PNG_HEADER_LENGTH, &err);
if (png_sig_cmp(header, 0, PNG_HEADER_LENGTH))
{
VFS_CLOSE(fin);
fin = NULL;
return false;
}
return true;
}
png_data *Image_LoadPNG_All (vfsfile_t *fin, const char *filename, int matchwidth, int matchheight, int loadflag, int *real_width, int *real_height)
{
byte **rowpointers = NULL;
byte *data = NULL;
png_structp png_ptr = NULL;
png_infop pnginfo = NULL;
png_textp textchunks = NULL; // Actual text chunks that will be returned.
png_textp png_text_ptr = NULL; // Text chunks are read into this (will be scrapped by libpng).
png_data *png_return_val = NULL; // Return struct containing data + text chunks.
int n_textcount = 0; // Number of text chunks.
int y, width, height, bitdepth, colortype, interlace, compression, filter, bytesperpixel;
unsigned long rowbytes;
// Check if we were given a non-null file pointer
// if so then use it, otherwise try to open the specified filename.
if (!fin && !(fin = FS_OpenVFS(filename, "rb", FS_ANY)))
{
return NULL;
}
// Check if the loaded file contains a PNG header.
if (!PNG_HasHeader (fin))
{
Com_DPrintf ("Invalid PNG image %s\n", COM_SkipPath(filename));
return NULL;
}
// Try creating a PNG structure for reading the file.
if (!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
{
VFS_CLOSE(fin);
fin = NULL;
return NULL;
}
// Create a structure for reading the characteristics of the image.
if (!(pnginfo = png_create_info_struct(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &pnginfo, NULL);
VFS_CLOSE(fin);
fin = NULL;
return NULL;
}
// Set the return adress that PNGLib should return to if
// an error occurs during reading.
#if 0
if (setjmp(png_ptr->jmpbuf))
{
png_destroy_read_struct(&png_ptr, &pnginfo, NULL);
VFS_CLOSE(fin);
fin = NULL;
return NULL;
}
#endif
// Set the read function that should be used.
png_set_read_fn(png_ptr, fin, PNG_IO_user_read_data);
// Tell PNG-lib that we have already handled the first <num_bytes> magic bytes.
// Handling more than 8 bytes from the beginning of the file is an error.
png_set_sig_bytes(png_ptr, PNG_HEADER_LENGTH);
// Read all the file information until the actual image data.
png_read_info(png_ptr, pnginfo);