-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathldglpr.c
More file actions
1494 lines (1317 loc) · 38 KB
/
ldglpr.c
File metadata and controls
1494 lines (1317 loc) · 38 KB
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
/*
* ldglpr.c Module for printing pictures of ldlite .dat files
* Copyright (C) 2001 DMH
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "glwinkit.h"
#include "platform.h"
#include "ldliteVR.h"
#ifndef WINDOWS
// This stuff gets pulled in by glut.h for windows.
#include "wstubs.h"
#else
// glut 3.7 no longer includes windows.h
#if (GLUT_XLIB_IMPLEMENTATION >= 13)
#include <windows.h>
#endif
#endif
extern char buf[10240];
extern int use_uppercase;
extern int use_png_alpha;
extern ZIMAGE z;
extern GLint Width;
extern GLint Height;
extern int cropping;
extern char progname[256];
extern int curstep;
extern int OffScreenRendering;
extern int renderbuffer;
extern int downsample;
#ifdef OSMESA_OPTION
#include "GL/osmesa.h"
void *OSbuffer = NULL;
OSMesaContext ctx;
#endif
#ifdef WIN_DIB_OPTION
HGLRC hGLRC;
HBITMAP m_dibSection; // memory DIB for offscreen rendering
#endif
#ifdef AGL_OFFSCREEN_OPTION
#include <AGL/agl.h>
void *OSbuffer = NULL;
AGLContext ctx;
// NOTE: Apple currently lists aglSetOffScreen() as unsupported by Carbon.
#endif
#ifdef CGL_OFFSCREEN_OPTION
//#include <CGL/CGLCurrent.h>
//#include <CGL/CGLTypes.h>
#include <Carbon/Carbon.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
void *OSbuffer = NULL;
CGLContextObj ctx;
#endif
char *pix;
#ifdef MACOS_X
void GetAvailablePos(int *w, int *h)
{
//HIWindowGetAvailablePositioningBounds() // Leopard only?
//Prior to Leopard, just use GetAvailableWindowPositioningBounds
Rect rect;
//GetAvailableWindowPositioningBounds( GetMainDevice(), &rect );
GetAvailableWindowPositioningBounds( NULL, &rect ); //GetMainDevice() is old.
*w = rect.right - rect.left;
*h = rect.bottom - rect.top;
}
#endif
/***************************************************************/
#define BYTE1(i) ((unsigned char) (i & 0x0ff))
#define BYTE2(i) ((unsigned char) ((i / 0x100) & 0x0ff))
#define BYTE3(i) ((unsigned char) ((i / 0x10000) & 0x0ff))
#define BYTE4(i) ((unsigned char) ((i / 0x1000000) & 0x0ff))
/***************************************************************/
FILE *start_bmp(char *filename, int width, int height)
{
BITMAPINFOHEADER bmhbuf;
BITMAPFILEHEADER bmfh;
LPBITMAPINFOHEADER bmh;
char *p, c;
FILE *fp;
char hdr[54];
printf("Write BMP %s\n", filename);
if ((fp = fopen(filename,"wb+"))==NULL) {
printf("Could not open %s\n", filename);
return(NULL);
}
//bmh = (LPBITMAPINFOHEADER) z.dib;
bmh = (LPBITMAPINFOHEADER) &bmhbuf;
bmh->biSize = 40;
bmh->biWidth = width;
bmh->biHeight = height;
bmh->biPlanes = 1;
//bmh->biBitCount = 16;
bmh->biBitCount = 24;
bmh->biSizeImage = (bmh->biWidth*bmh->biBitCount+31)/32*4
* (bmh->biHeight>0?bmh->biHeight:-bmh->biHeight);
p = (char *) &(bmfh.bfType);
p[0] = 'B';
p[1] = 'M';
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfSize = (bmfh.bfOffBits + bmh->biSizeImage) / 4;
bmh->biSizeImage = 0;
#if 0
// 14 bytes
fwrite(&bmfh.bfType, 2, 1, fp);
fwrite(&bmfh.bfSize, 4, 1, fp);
fwrite(&bmfh.bfReserved1, 2, 1, fp);
fwrite(&bmfh.bfReserved2, 2, 1, fp);
fwrite(&bmfh.bfOffBits, 4, 1, fp);
// 40 bytes
fwrite(bmh, sizeof(BITMAPINFOHEADER), 1, fp);
#else
// Copy the 54 bytes of bmfh and bmh into hdr in intel byte order & packing.
memset(hdr, 0, 54);
hdr[0] = 'B';
hdr[1] = 'M';
hdr[2] = BYTE1(bmfh.bfSize);
hdr[3] = BYTE2(bmfh.bfSize);
hdr[4] = BYTE3(bmfh.bfSize);
hdr[5] = BYTE4(bmfh.bfSize);
hdr[10] = BYTE1(bmfh.bfOffBits);
hdr[11] = BYTE2(bmfh.bfOffBits);
hdr[12] = BYTE3(bmfh.bfOffBits);
hdr[13] = BYTE4(bmfh.bfOffBits);
hdr[14] = 40;
hdr[15] = 0;
hdr[16] = 0;
hdr[17] = 0;
hdr[18] = BYTE1(bmh->biWidth);
hdr[19] = BYTE2(bmh->biWidth);
hdr[20] = BYTE3(bmh->biWidth);
hdr[21] = BYTE4(bmh->biWidth);
hdr[22] = BYTE1(bmh->biHeight);
hdr[23] = BYTE2(bmh->biHeight);
hdr[24] = BYTE3(bmh->biHeight);
hdr[25] = BYTE4(bmh->biHeight);
hdr[26] = 1;
hdr[27] = 0;
hdr[28] = 24;
hdr[29] = 0;
// The rest are all zeros.
fwrite(hdr, 54, 1, fp);
#endif
return(fp);
}
/***************************************************************/
void write_bmp(char *filename)
{
int i, j;
char *p, c;
FILE *fp;
int width = Width;
int height = Height;
GLint xoff = 0;
GLint yoff = 0;
pix = &buf[0];
if (width > 2560)
pix = (char*)malloc(width*3);
if (cropping)
{
xoff = max(0, z.extent_x1);
yoff = max(0, z.extent_y1);
width = min((z.extent_x2 - xoff), (Width - xoff));
height = min((z.extent_y2 + 1 - yoff), (Height - yoff));
//width = ((width + 31)/32) * 32; // round to a multiple of 32.
width = ((width + 3)/4) * 4; // round to a multiple of 4.
if (ldraw_commandline_opts.debug_level == 1)
printf("bmpsize = (%d, %d) at (%d, %d)\n", width, height, xoff, yoff);
if ((width <= 0) || (height <= 0)) return;
}
fp = start_bmp(filename, width, height);
if (fp == NULL)
return;
// no pallete since we use RGB
glReadBuffer(renderbuffer);
for (i = 0; i < height; i++)
{
#ifdef OSMESA_OPTION
if (OffScreenRendering)
{
int j;
char *b = (char *)OSbuffer;
b += ((i+yoff)*Width +xoff) *4;
for (j=0; j<width; j++) {
// MESA or OSmesa bug? ReadPixels gives GBR instead of RGB???
pix[3*j] = b[1];
pix[3*j+1] = b[0];
pix[3*j+2] = b[2];
b+=4;
}
}
else
#endif
{
glReadPixels(xoff, i+yoff, width, 1, GL_RGB, GL_UNSIGNED_BYTE, pix);
p = pix;
for (j = 0; j < width; j++) // RGB -> BGR
{
c = p[0];
#ifdef WINDOWS
p[0] = p[2];
p[2] = c;
#else
#ifdef OSMESA_OPTION
// MESA or OSmesa bug? ReadPixels gives GBR instead of RGB???
p[0] = p[1];
p[1] = c;
#endif
#ifdef CGL_OFFSCREEN_OPTION
// I assume the BGR is for the BMP file, so follow the lead of Windows.
p[0] = p[2];
p[2] = c;
#endif
#endif
p+=3;
}
}
fwrite(pix, width*3, 1, fp);
}
fclose(fp);
if (width > 2560)
free(pix);
}
/***************************************************************/
FILE *start_ppm(char *filename, int width, int height)
{
char *p;
FILE *fp;
if ((p = strrchr(filename, '.')) != NULL)
*p = 0;
strcat(filename, use_uppercase ? ".PPM" : ".ppm");
printf("Write PPM %s\n", filename);
fp = fopen(filename, "wb"); // open in binary mode (to use unix \n chars)
if (!fp) {
printf("Couldn't open image file: %s\n", filename);
return(NULL);
}
fprintf(fp,"P6\n");
fprintf(fp,"# ppm-file created by %s\n", progname);
fprintf(fp,"%i %i\n", width, height);
fprintf(fp,"255\n"); // need unix \n here for some ppm interpreters.
fclose(fp);
fp = fopen(filename, "ab"); /* now append binary data */
if (!fp) {
printf("Couldn't append to image file: %s\n", filename);
return(NULL);
}
return (fp);
}
/***************************************************************/
void write_ppm(char *filename)
{
int i, j;
FILE *fp;
int width = Width;
int height = Height;
GLint xoff = 0;
GLint yoff = 0;
pix = &buf[0];
if (width > 2560)
pix = (char*)malloc(width*3);
if (cropping)
{
xoff = max(0, z.extent_x1);
yoff = max(0, z.extent_y1);
width = min((z.extent_x2 - xoff), (Width - xoff));
height = min((z.extent_y2 + 1 - yoff), (Height - yoff));
//width = ((width + 3)/4) * 4; // round to a multiple of 4.
if (ldraw_commandline_opts.debug_level == 1)
printf("bmpsize = (%d, %d) at (%d, %d)\n", width, height, xoff, yoff);
if ((width <= 0) || (height <= 0)) return;
}
fp = start_ppm(filename, width, height);
if (fp == NULL)
return;
glReadBuffer(renderbuffer);
// Write image rows
//png_write_image(png_ptr, row_pointers);
for (i = height-1; i >= 0; i--)
{
#ifdef OSMESA_OPTION
if (OffScreenRendering)
{
int j;
char *b = (char *)OSbuffer;
b += ((i+yoff)*Width +xoff) *4;
for (j=0; j<width; j++) {
pix[3*j] = b[0];
pix[3*j+1] = b[1];
pix[3*j+2] = b[2];
b+=4;
}
}
else
#endif
glReadPixels(xoff, i+yoff, width, 1, GL_RGB, GL_UNSIGNED_BYTE, pix);
fwrite(pix, width*3, 1, fp);
}
fclose(fp);
if (width > 2560)
free(pix);
}
/***************************************************************/
void
write_targa(char *filename, const GLubyte *buffer, int width, int height)
{
char *p;
FILE *f;
if ((p = strrchr(filename, '.')) != NULL)
*p = 0;
strcat(filename, use_uppercase ? ".TGA" : ".tga");
f = fopen( filename, "w" );
if (f) {
int i, x, y;
const GLubyte *ptr = buffer;
printf ("Write TGA %s\n", filename);
fputc (0x00, f); /* ID Length, 0 => No ID */
fputc (0x00, f); /* Color Map Type, 0 => No color map included */
fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
fputc (0x00, f); /* Next five bytes are about the color map entries */
fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
fputc (0x00, f);
fputc (0x00, f);
fputc (0x00, f);
fputc (0x00, f); /* X-origin of Image */
fputc (0x00, f);
fputc (0x00, f); /* Y-origin of Image */
fputc (0x00, f);
fputc (Width & 0xff, f); /* Image Width */
fputc ((Width>>8) & 0xff, f);
fputc (Height & 0xff, f); /* Image Height */
fputc ((Height>>8) & 0xff, f);
fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
fputc (0x20, f); /* Image Descriptor */
fclose(f);
f = fopen( filename, "ab" ); /* reopen in binary append mode */
for (y=height-1; y>=0; y--) {
for (x=0; x<width; x++) {
i = (y*width + x) * 4;
fputc(ptr[i+2], f); /* write blue */
fputc(ptr[i+1], f); /* write green */
fputc(ptr[i], f); /* write red */
}
}
fclose(f);
}
else
printf ("Failed to write tga %s\n", filename);
}
#ifdef USE_PNG
/***************************************************************/
#include "png.h"
/***************************************************************/
static void png_error_fn(png_structp png_ptr, const char *err_msg)
{
jmp_buf *j;
j= (jmp_buf*) png_get_error_ptr(png_ptr);
longjmp(*j, -1);
}
static void png_warning_fn(png_structp png_ptr, const char *warn_msg)
{
return;
}
/***************************************************************/
FILE *start_png(char *filename, int width, int height,
png_structp *png_pp, png_infop *info_pp)
{
char *p;
png_structp png_ptr;
png_infop info_ptr;
jmp_buf jbuf;
png_color_16 background;
FILE *fp;
if ((p = strrchr(filename, '.')) != NULL)
*p = 0;
strcat(filename, use_uppercase ? ".PNG" : ".png");
printf("Write PNG %s\n", filename);
// Write header stuff
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (void*)(&jbuf),
png_error_fn, png_warning_fn);
if (!png_ptr)
{
printf("No Memory", filename);
return(NULL);
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
printf("No Memory", filename);
return(NULL);
}
if(setjmp(jbuf)) {
// we'll get here if an error occurred in any of the following
// png_ functions
printf("Aborting PNG file %s", filename);
png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
if(fp) fclose(fp);
return(NULL);
}
if ((fp = fopen(filename,"wb+"))==NULL) {
printf("Could not open %s\n", filename);
return(NULL);
}
png_init_io(png_ptr, fp);
if ( use_png_alpha )
{
// Set the default background for transparent image to white.
// I should just store the current background color in a global and use it.
// I should also make this a menu option/command line opt.
// Perhaps ctl-p for the hot key.
// Perhaps I need to create 4 image types. bmp24, bmp8, png, png-alpha
// and menu/hotkey/cmdline support for cropping modifier.
background.red = 0xffff;
background.green = 0xffff;
background.blue = 0xffff;
png_set_IHDR(png_ptr, info_ptr, width, height, 8,
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_set_bKGD(png_ptr, info_ptr, &background);
}
else
{
png_set_IHDR(png_ptr, info_ptr, width, height, 8,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
}
// png_set_bgr(png_ptr);
// png_set_pHYs(png_ptr, info_ptr, resolution_x, resolution_y, 1);
png_write_info(png_ptr, info_ptr);
*png_pp = png_ptr;
*info_pp = info_ptr;
return(fp);
}
#ifdef CHEESY_2X2_AVG
/***************************************************************/
void write_png_avg(char *filename)
{
int i, j, k;
png_structp png_ptr;
png_infop info_ptr;
png_text text_ptr[1];
FILE *fp;
int width = Width;
int height = Height;
GLint xoff = 0;
GLint yoff = 0;
char *px, *PX;
char *PIX = NULL;
pix = &buf[0];
if (width > 2560)
pix = (char*)malloc(width*4); // Alloc rgba even if alpha not used.
if (downsample)
PIX = (char*)malloc(width*4); // Alloc rgba even if alpha not used.
px = pix;
if (cropping)
{
xoff = max(0, z.extent_x1);
yoff = max(0, z.extent_y1);
width = min((z.extent_x2 - xoff), (Width - xoff));
height = min((z.extent_y2 + 1 - yoff), (Height - yoff));
width = ((width + 3)/4) * 4; // round to a multiple of 4.
if (ldraw_commandline_opts.debug_level == 1)
printf("pngsize = (%d, %d) at (%d, %d)\n", width, height, xoff, yoff);
if ((width <= 0) || (height <= 0)) return;
}
if (downsample)
fp = start_png(filename, width/2, height/2, &png_ptr, &info_ptr);
else
fp = start_png(filename, width, height, &png_ptr, &info_ptr);
if (fp == NULL)
return;
glReadBuffer(renderbuffer);
// Write image rows
//png_write_image(png_ptr, row_pointers);
for (i = height-1; i >= 0; i--)
{
for (k = downsample; k >= 0; k--) {
#ifdef OSMESA_OPTION
if (OffScreenRendering)
{
char *b = (char *)OSbuffer;
b += ((i+yoff)*Width +xoff) *4;
for (j=0; j<width; j++) {
if (use_png_alpha){
pix[4*j] = b[0];
pix[4*j+1] = b[1];
pix[4*j+2] = b[2];
pix[4*j+3] = b[3];
}
else {
pix[3*j] = b[0];
pix[3*j+1] = b[1];
pix[3*j+2] = b[2];
}
b+=4;
}
}
else
#endif
if (use_png_alpha)
{
glReadPixels(xoff, i+yoff, width, 1, GL_RGBA, GL_UNSIGNED_BYTE, pix);
char *b = pix+3;
for (j = 0; j < width; j++)
if (b[4*j])
b[4*j] = 0xff; // Convert partial alpha to opaque.
}
else
glReadPixels(xoff, i+yoff, width, 1, GL_RGB, GL_UNSIGNED_BYTE, pix);
if (k > 0) {
if (--i < 0) // Skip this line because png header doesn't expect it.
break; //memcpy(PIX, pix, width*4);
pix = PIX;
continue;
}
#if 1
// Use fast color averaging algorithm from compuphase.com/graphic/scale3.html
if (downsample) { // Now downsample to one halfwidth row.
unsigned int a, b, c, m; // 3 pixel values to work with and the underflow mask.
unsigned int *p;
m = 0xfefefefe; // underflow = lowest bit of each color byte.
pix = px; // Restore pix ptr to the start of buf.
PX = PIX; // px = pointer to top row, PX = bottom row ptr.
p = (unsigned int *)pix; // p = dest ptr (reuse left half of top row).
for (j=0; j<width; j++) {
// Average 2 pixels from one row.
a = *(unsigned int *)px;
px += 4;
b = *(unsigned int *)px;
a = (((a ^ b) & m) >> 1) + (a & b);
// Average 2 pixels from next row.
c = *(unsigned int *)PX;
PX += 4;
b = *(unsigned int *)PX;
c = (((c ^ b) & m) >> 1) + (c & b);
// Average the average pixels to squeeze 2x2 pixels to 1..
a = (((a ^ c) & m) >> 1) + (a & c);
*p++ = a;
px += 4;
PX += 4;
}
px = pix; // Restore px pointer to the start of pix buf.
}
#endif
png_write_row(png_ptr, (unsigned char *)pix);
}
}
text_ptr[0].key = "Software";
text_ptr[0].text = "LdGLite";
text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
png_set_text(png_ptr, info_ptr, text_ptr, 1);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
if (width > 2560)
free(pix);
if (downsample)
free(PIX);
}
#endif
/***************************************************************/
#include <math.h>
// Gamma correct 8 bits into 16 bits for more precise addititon.
// Then downshift to 12 bits and inverse gamma that back to 8 bits.
double gamma_val = 2.2;
int GAMMA[256];
unsigned char GAM2RGB[4096];
/***************************************************************/
void write_png(char *filename)
{
int i, j, k;
png_structp png_ptr;
png_infop info_ptr;
png_text text_ptr[1];
FILE *fp;
int width = Width;
int height = Height;
GLint xoff = 0;
GLint yoff = 0;
unsigned char *p, *p0, *p1, *p2;
pix = &buf[0];
if (downsample) {
ZCOLOR zcp, zcs;
/* Make gamma lookup tables now to avoid pow inside the loops */
for(i = 0; i< 256; i++)
GAMMA[i] = (unsigned int) (65535.0 * pow((i/255.0), gamma_val));
for(i = 0; i< 4096; i++)
GAM2RGB[i] = (unsigned char) (255.0 * pow((i/4095.0), (1.0 / gamma_val)));
// Alloc rgba even if alpha not used.
p0 = (unsigned char*)calloc(width+1, 4);
p1 = (unsigned char*)calloc(width+1, 4);
p2 = (unsigned char*)calloc(width+1, 4);
// Add a topright border of 1 pixel of transparent background for filter.
translate_color(ldraw_commandline_opts.B, &zcp, &zcs);
k = (width+1) * (3 + use_png_alpha);
pix = p0;
for (j=0; j<k; j++){
pix[j++] = zcp.r;
pix[j++] = zcp.g;
pix[j] = zcp.b;
if (use_png_alpha)
pix[++j] = 0;
}
pix = (char *)p2;
k = k - use_png_alpha;
pix[k-3] = zcp.r;
pix[k-2] = zcp.g;
pix[k-1] = zcp.b;
pix = (char *)p1;
pix[k-3] = zcp.r;
pix[k-2] = zcp.g;
pix[k-1] = zcp.b;
}
else if (width > 2560)
pix = (char*)malloc(width*4); // Alloc rgba even if alpha not used.
if (cropping)
{
xoff = max(0, z.extent_x1);
yoff = max(0, z.extent_y1);
width = min((z.extent_x2 - xoff), (Width - xoff));
height = min((z.extent_y2 + 1 - yoff), (Height - yoff));
width = ((width + 3)/4) * 4; // round to a multiple of 4.
if (ldraw_commandline_opts.debug_level == 1)
printf("pngsize = (%d, %d) at (%d, %d)\n", width, height, xoff, yoff);
if ((width <= 0) || (height <= 0)) return;
}
if (downsample)
fp = start_png(filename, width/2, height/2, &png_ptr, &info_ptr);
else
fp = start_png(filename, width, height, &png_ptr, &info_ptr);
if (fp == NULL)
return;
glReadBuffer(renderbuffer);
// Write image rows
//png_write_image(png_ptr, row_pointers);
for (i = height-1; i >= 0; i--)
{
for (k = downsample; k >= 0; k--) {
#ifdef OSMESA_OPTION
if (OffScreenRendering)
{
char *b = (char *)OSbuffer;
b += ((i+yoff)*Width +xoff) *4;
for (j=0; j<width; j++) {
if (use_png_alpha){
pix[4*j] = b[0];
pix[4*j+1] = b[1];
pix[4*j+2] = b[2];
pix[4*j+3] = b[3];
}
else {
pix[3*j] = b[0];
pix[3*j+1] = b[1];
pix[3*j+2] = b[2];
}
b+=4;
}
}
else
#endif
if (use_png_alpha)
{
glReadPixels(xoff, i+yoff, width, 1, GL_RGBA, GL_UNSIGNED_BYTE, pix);
char *b = pix+3;
for (j = 0; j < width; j++)
if (b[4*j])
b[4*j] = 0xff; // Convert partial alpha to opaque.
}
else
glReadPixels(xoff, i+yoff, width, 1, GL_RGB, GL_UNSIGNED_BYTE, pix);
if (k > 0) {
if (--i < 0) // Skip this line because png header doesn't expect it.
break; //memcpy(PIX, pix, width*4);
pix = (char *)p2;
continue;
}
if (downsample) { // Now downsample to one halfwidth row.
unsigned int a, n, N, J;
p = p1; // p = dest ptr (reuse left half of top row).
n = (3 + use_png_alpha);
N = 2 * n;
for (j=0,J=0; j<(width*n); j+=n) {
// Use 3x3 gaussian blur filter (with gamma correction).
// **************************************************************
a = GAMMA[p0[j+0]] >> 4;
a += GAMMA[p0[j+n]] >> 3;
a += GAMMA[p0[j+N]] >> 4;
a += GAMMA[p1[j+0]] >> 3;
a += GAMMA[p1[j+n]] >> 2;
a += GAMMA[p1[j+N]] >> 3;
a += GAMMA[p2[j+0]] >> 4;
a += GAMMA[p2[j+n]] >> 3;
a += GAMMA[p2[j+N]] >> 4;
j++;
p[J++] = GAM2RGB[(a >> 4) & 0xfff];
a = GAMMA[p0[j+0]] >> 4;
a += GAMMA[p0[j+n]] >> 3;
a += GAMMA[p0[j+N]] >> 4;
a += GAMMA[p1[j+0]] >> 3;
a += GAMMA[p1[j+n]] >> 2;
a += GAMMA[p1[j+N]] >> 3;
a += GAMMA[p2[j+0]] >> 4;
a += GAMMA[p2[j+n]] >> 3;
a += GAMMA[p2[j+N]] >> 4;
j++;
p[J++] = GAM2RGB[(a >> 4) & 0xfff];
a = GAMMA[p0[j+0]] >> 4;
a += GAMMA[p0[j+n]] >> 3;
a += GAMMA[p0[j+N]] >> 4;
a += GAMMA[p1[j+0]] >> 3;
a += GAMMA[p1[j+n]] >> 2;
a += GAMMA[p1[j+N]] >> 3;
a += GAMMA[p2[j+0]] >> 4;
a += GAMMA[p2[j+n]] >> 3;
a += GAMMA[p2[j+N]] >> 4;
j++;
p[J++] = GAM2RGB[(a >> 4) & 0xfff];
if (use_png_alpha) {
a = p0[j+0] >> 4;
a += p0[j+n] >> 3;
a += p0[j+N] >> 4;
a += p1[j+0] >> 3;
a += p1[j+n] >> 2;
a += p1[j+N] >> 3;
a += p2[j+0] >> 4;
a += p2[j+n] >> 3;
a += p2[j+N] >> 4;
a &= 0xff;
j++;
p[J++] = a;
}
}
pix = p0; // Swap ptr to third row into first row ptr so we can reuse the row.
p0 = p2;
p2 = pix;
pix = p1; // Setup to read the 2nd and 3rd rows.
}
png_write_row(png_ptr, (unsigned char *)pix);
}
}
text_ptr[0].key = "Software";
text_ptr[0].text = "LdGLite";
text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
png_set_text(png_ptr, info_ptr, text_ptr, 1);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
if (width > 2560)
free(pix);
if (downsample) {
free(p0);
free(p1);
free(p2);
}
}
#endif
#ifdef USE_BMP8
/***************************************************************/
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
#define BMP_PIXELSIZE(width, height, bits) (((((width) * (bits) + 31)/32) * 4) * height)
#define BMP_HEADERSIZE (3 * 2 + 4 * 12)
/***************************************************************/
// Writes a bmp to a file
// added by LZ
static BOOL SaveBMP8(char* fileName, BYTE* colormappedbuffer, UINT width, UINT height, int colors, RGBQUAD *colormap)
{
int datasize, cmapsize, byteswritten, row, col;
long filesize;
int res1, res2;
long pixeloffset;
int bmisize;
long cols;
long rows;
WORD planes;
long compression;
long cmpsize;
long xscale;
long yscale;
long impcolors;
FILE *fp;
char bm[2];
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmh;
int i;
int pixbuf;
int nbits = 0;
int offset; // offset into our color-mapped RGB buffer
BYTE pval;
char hdr[54];
cmapsize = colors * 4;
datasize = BMP_PIXELSIZE(width, height, 8);
filesize = BMP_HEADERSIZE + cmapsize + datasize;
res1 = res2 = 0;
pixeloffset = BMP_HEADERSIZE + cmapsize;
bmisize = 40;
cols = width;
rows = height;
planes = 1;
compression =0;
cmpsize = datasize;
xscale = 0;
yscale = 0;
impcolors = colors;
fp = fopen(fileName, "wb+");
if (fp == NULL)
return FALSE;
bm[0]='B';
bm[1]='M';
// header stuff
bmfh.bfType=*(WORD *)&bm;
bmfh.bfSize= filesize;
bmfh.bfReserved1=0;
bmfh.bfReserved2=0;
bmfh.bfOffBits=pixeloffset;
bmh.biSize = bmisize;
bmh.biWidth = cols;
bmh.biHeight = rows;
bmh.biPlanes = planes;
bmh.biBitCount = 8;
bmh.biCompression = compression;
bmh.biSizeImage = cmpsize;
bmh.biXPelsPerMeter = xscale;
bmh.biYPelsPerMeter = yscale;
bmh.biClrUsed = colors;
bmh.biClrImportant = impcolors;
#if 0
fwrite(&bmfh, sizeof (BITMAPFILEHEADER), 1, fp);
fwrite(&bmh, sizeof (BITMAPINFOHEADER), 1, fp);
#else
// Copy the 54 bytes of bmfh and bmh into hdr in intel byte order & packing.
memset(hdr, 0, 54);
hdr[0] = 'B';
hdr[1] = 'M';
hdr[2] = BYTE1(bmfh.bfSize);
hdr[3] = BYTE2(bmfh.bfSize);
hdr[4] = BYTE3(bmfh.bfSize);
hdr[5] = BYTE4(bmfh.bfSize);
hdr[10] = BYTE1(bmfh.bfOffBits);
hdr[11] = BYTE2(bmfh.bfOffBits);
hdr[12] = BYTE3(bmfh.bfOffBits);
hdr[13] = BYTE4(bmfh.bfOffBits);
hdr[14] = 40;
hdr[15] = 0;
hdr[16] = 0;
hdr[17] = 0;
hdr[18] = BYTE1(bmh.biWidth);
hdr[19] = BYTE2(bmh.biWidth);
hdr[20] = BYTE3(bmh.biWidth);
hdr[21] = BYTE4(bmh.biWidth);
hdr[22] = BYTE1(bmh.biHeight);
hdr[23] = BYTE2(bmh.biHeight);
hdr[24] = BYTE3(bmh.biHeight);
hdr[25] = BYTE4(bmh.biHeight);
hdr[26] = 1;
hdr[27] = 0;
hdr[28] = 8;
hdr[29] = 0;
hdr[46] = BYTE1(bmh.biClrUsed);
hdr[47] = BYTE2(bmh.biClrUsed);
hdr[48] = BYTE3(bmh.biClrUsed);
hdr[49] = BYTE4(bmh.biClrUsed);
hdr[50] = BYTE1(bmh.biClrImportant);
hdr[51] = BYTE2(bmh.biClrImportant);
hdr[52] = BYTE3(bmh.biClrImportant);
hdr[53] = BYTE4(bmh.biClrImportant);
// The rest are all zeros.
fwrite(hdr, 54, 1, fp);
#endif
if (cmapsize)
{
for (i = 0; i< colors; i++)
{
putc(colormap[i].rgbRed, fp);
putc(colormap[i].rgbGreen, fp);
putc(colormap[i].rgbBlue, fp);
putc(0, fp); // dummy
}
}
byteswritten = BMP_HEADERSIZE + cmapsize;
for (row = 0; row< (int)height; row++)
{
nbits = 0;