-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgfxio.c
1804 lines (1568 loc) · 52.6 KB
/
gfxio.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
/* Stereograph 0.33b, 17/11/2003;
* Graphics I/O functions;
* Copyright (c) 2000-2003 by Fabian Januszewski <fabian.linux@januszewski.de>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <dlfcn.h>
#include <endian.h>
#include <png.h>
#include <jpeglib.h>
#ifdef X11GUI
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/keysymdef.h>
#include <X11/keysym.h>
typedef unsigned long Pixel;
#endif /* X11GUI */
#include "renderer.h"
#include "gfxio.h"
#include "globals.h"
#define frand1() ((float)rand() / (float)RAND_MAX)
#define PI 3.14159265358979
#define DIRECT 0
#define AUXIL 1
/* add a pair of triangles to a stereogram */
int GFX_AddTriangles(int x, int size, int width, struct GFX_DATA *gfx) {
int z, y;
for(y = 0; (y < size) && (y < gfx->Height); y++)
for(z = 0; (z < (size - (y * 2))); z++)
if(((y + x - (width/2) + z) >= 0) && ((y + x + width/2 + z) < gfx->Width))
gfx->Data[(y * gfx->Width) + y + x - width/2 + z] = gfx->Data[(y * gfx->Width) + y + x + width/2 + z] = 0;
return 0;
}
/* generates a random texture */
int GFX_Generate_RandomTexture(struct GFX_DATA *gfx, int width, int height, int type) {
int a = 0, z;
gfx->Data = (int*) malloc(width * height * sizeof(int));
if(gfx->Data) {
srand((unsigned int)time(NULL));
gfx->Width = width;
gfx->Height = height;
switch(type) {
case GFX_TEX_BW:
for(z = 0; z < (height * width); z++)
gfx->Data[z] = (rand()&1) * 65793 * 255;
break;
case GFX_TEX_GRAYSCALE:
for(z = 0; z < (height * width); z++)
gfx->Data[z] = (rand()&255) * 65793;
break;
case GFX_TEX_COLORED:
for(z = 0; z < (height * width); z++)
gfx->Data[z] = (rand()&(65793 * 255));
break;
case GFX_TEX_SINLINE:
a = GFX_Generate_SinlineTexture(gfx);
break;
default:
free(gfx->Data);
gfx->Data = NULL;
if (stereograph_verbose) printf("FAILED\n");
fprintf(stderr, "unsupported random texture type;\n");
a = GFX_ERROR_UNSUPP_RANDOMTEX;
}
} else {
if (stereograph_verbose) printf("FAILED\n");
fprintf(stderr, "could not allocate memory for random texture;\n");
a = GFX_ERROR_MEMORY_PROBLEM;
}
if(stereograph_verbose && !a) printf("done;\n");
return a;
}
/* resizes a texture to the width of width pixels */
int GFX_Resize(struct GFX_DATA *gfx, int width) {
int y;
int *temp_gfx;
/* only resize the gfx if necessary */
if((width > 0) && (gfx->Width >= width)) {
if(stereograph_verbose) printf("resizing texture...");
temp_gfx = (int*) malloc(gfx->Height * width * sizeof(int));
if(!temp_gfx) {
if(stereograph_verbose) printf("FAILED;\n");
fprintf(stderr, "error allocating memory for texture!\n");
return GFX_ERROR_MEMORY_PROBLEM;
} else
for(y = 0; (y < gfx->Height); y++)
memcpy(temp_gfx + y * width, gfx->Data + gfx->Width * y, width * sizeof(int));
free(gfx->Data);
gfx->Data = temp_gfx;
gfx->Width = width;
if(stereograph_verbose) printf("done;\n");
}
return 0;
}
/* inverts a base (gray scaled) image */
int GFX_Invert(struct GFX_DATA *gfx) {
int z, r = 0, g = 0, b = 0;
if(stereograph_verbose) printf("inverting base...");
for(z = 0; z < (gfx->Width * gfx->Height); z++) {
r = 255 - (gfx->Data[z] & 255);
g = 255 - ((gfx->Data[z] >> 8) & 255);
b = 255 - ((gfx->Data[z] >> 16) & 255);
gfx->Data[z] = r + (g << 8) + (b << 16);
}
if(stereograph_verbose) printf("done;\n");
return 0;
}
/* adjusts the base levels for transparent rendering */
int GFX_T_AdjustLevel(struct GFX_DATA *T_gfx, int T_layers, int T_level) {
int t, z, r_t = 0, g_t = 0, b_t = 0, r = 0, g = 0, b = 0;
if(stereograph_verbose) printf("ajdusting levels...");
for(t = 1; t < T_layers; t++)
for(z = 0; z < (T_gfx[0].Width * T_gfx[0].Height); z++) {
r = T_gfx[t - 1].Data[z] & 255;
g = (T_gfx[t - 1].Data[z] >> 8) & 255;
b = (T_gfx[t - 1].Data[z] >> 16) & 255;
r_t = T_gfx[t].Data[z] & 255;
g_t = (T_gfx[t].Data[z] >> 8) & 255;
b_t = (T_gfx[t].Data[z] >> 16) & 255;
switch (T_level) {
case GFX_T_NO_LEVEL:
break;
case GFX_T_BACK_LEVEL:
if(!r_t && !g_t && !r_t) {
r_t = r;
g_t = g;
b_t = b;
}
break;
case GFX_T_TOP_LEVEL:
if((r_t + g_t + r_t) < (r + g + b)) {
r_t = r;
g_t = g;
b_t = b;
}
break;
default :
if(stereograph_verbose) printf("FAILED\n");
fprintf(stderr, "Unimplemented transparency level;\n");
return GFX_ERROR_UNSUPP_T_LEVEL;
}
T_gfx[t].Data[z] = r_t + (g_t << 8) + (b_t << 16);
}
if(stereograph_verbose) printf("done;\n");
return 0;
}
/* reads a png or targa file */
int GFX_Read_File (char *file_name, struct GFX_DATA *gfx)
{
int a;
FILE *ifile = NULL;
unsigned char check_header[8];
ifile = fopen(file_name, "r");
if(ifile == NULL) {
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "loading gfx data...FAILED;\n");
fprintf(stderr, "cannot open file '%s'!\n", file_name);
return GFX_ERROR_READ_ERROR;
} else {
a = GFX_Identify_File(ifile, check_header);
switch (a) {
case GFX_IO_JPG :
a = GFX_Read_JPG(ifile, check_header, gfx);
break;
case GFX_IO_PNG :
a = GFX_Read_PNG(ifile, check_header, gfx);
break;
case GFX_IO_TARGA :
a = GFX_Read_TARGA(ifile, check_header, gfx);
break;
case GFX_IO_PPM :
a = GFX_Read_PPM(ifile, check_header, gfx);
break;
case GFX_IO_C :
a = GFX_Read_C(ifile, file_name, check_header, gfx);
break;
}
}
fclose(ifile);
ifile = NULL;
if(!a && stereograph_verbose) printf("gfx data read (%i*%i);\n", gfx->Width, gfx->Height);
return a;
}
/* writes a png, ppm or targa file */
int GFX_Write_File (char *file_name, int output_format, struct GFX_DATA *gfx)
{
int a;
FILE *ofile = NULL;
if(stereograph_verbose) printf("saving '%s' (%i*%i)...", file_name, gfx->Width, gfx->Height);
if(!file_name)
ofile = stdout;
else
ofile = fopen(file_name, "w+");
if(ofile == NULL) {
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "writing gfx data...FAILED;\n");
fprintf(stderr, "cannot create file %s!\n", file_name);
return -1;
} else
switch (output_format) {
#ifdef X11GUI
case GFX_IO_X11 :
a = GFX_Write_X11(ofile, gfx);
break;
#endif /* X11GUI */
case GFX_IO_TARGA :
a = GFX_Write_TARGA(ofile, gfx);
break;
case GFX_IO_JPG :
a = GFX_Write_JPG(ofile, gfx);
break;
case GFX_IO_PNG :
a = GFX_Write_PNG(ofile, gfx);
break;
case GFX_IO_PPM :
a = GFX_Write_PPM(ofile, gfx);
break;
default :
if(strlen(file_name) >= 4) {
if(!strcmp(file_name + strlen(file_name) - 4, ".tga"))
a = GFX_Write_TARGA(ofile, gfx);
else if(!strcmp(file_name + strlen(file_name) - 4, ".png"))
a = GFX_Write_PNG(ofile, gfx);
else if(!strcmp(file_name + strlen(file_name) - 4, ".ppm"))
a = GFX_Write_PPM(ofile, gfx);
else {
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "writing gfx data...FAILED;\n");
fprintf(stderr, "unsupported output format!\n");
a = GFX_ERROR_UNSUPP_FORMAT;
}
} else {
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "writing gfx data...FAILED;\n");
fprintf(stderr, "unsupported output format!\n");
a = GFX_ERROR_UNSUPP_FORMAT;
}
}
fclose(ofile);
ofile = NULL;
return a;
}
/*** gfxio internals ***/
int GFX_Identify_File(FILE *ifile, unsigned char *check_header) {
if(fread(check_header, sizeof(char), 2, ifile) != 2) {
if(stereograph_verbose) printf("FAILED!\n"); else fprintf(stderr, "reading gfx data...FAILED;\n");
fprintf(stderr, "cannot read header! (file corrupt?)\n");
return GFX_ERROR_READ_ERROR;
} else if((check_header[0] == 'P') && ((check_header[1] == '3') || (check_header[1] == '6')))
return GFX_IO_PPM;
else if((check_header[0] == '/') && ((check_header[1] == '*')))
return GFX_IO_C;
else {
if(fread(check_header + 2, sizeof(char), 6, ifile) != 6) {
if(stereograph_verbose) printf("FAILED!\n"); else fprintf(stderr, "reading gfx data...FAILED;\n");
fprintf(stderr, "cannot read header! (file corrupt?)\n");
return GFX_ERROR_READ_ERROR;
} else {
if(!png_sig_cmp(check_header, 0, 8))
return GFX_IO_PNG;
else
if(check_header[0] == 0xff && check_header[1] == 0xd8 &&
(!strncasecmp(check_header+6, "jf", 2) ||
!strncasecmp(check_header+6, "ex", 2)))
return GFX_IO_JPG;
else
return GFX_IO_TARGA;
}
}
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED\n");
fprintf(stderr, "cannot identify gfx file, unsupported format!\n");
return GFX_ERROR_UNSUPP_FORMAT;
}
int skip_space_n_comments(FILE *ifile) {
int a;
while(isspace(a = fgetc(ifile)) || (a == '#'))
if(a == '#')
while(fgetc(ifile) != 10);
ungetc(a, ifile);
return 0;
}
int get_dec(FILE *ifile) {
static int a, c;
c = 0;
while(isdigit(a = fgetc(ifile)))
c = c * 10 + (a - '0');
ungetc(a, ifile);
return c;
}
typedef double point[3];
typedef void (*BFunction)();
typedef double (*ZFunction)(double x, double y);
typedef void (*PFunction)(point p, double x, double y);
double CONV_RAD=M_PI/180;
int color_range=765;
int auxil=DIRECT;
int width=0, height=0;
int *pixel_color = NULL;
struct GFX_DATA *global_gfx;
double X1=-1.0, X2=1.0, Y1=-1.0, Y2=1.0, Z1=-1.0, Z2=1.0;
double ratio_x=1.0, ratio_y=1.0, ratio_z=1.0;
double m_rot[12]={1,0,0,0,0,1,0,0,0,0,1,0};
double max(double x, double y)
{
if(x>=y) return x; else return y;
}
double min(double x, double y)
{
if(x>=y) return x; else return y;
}
void init_graphic_window(int w, int h, int mode)
{
int s;
width = w;
height = h;
auxil = mode;
s = width * height * sizeof(int);
global_gfx->Width = width;
global_gfx->Height = height;
global_gfx->Data = (int*)malloc(s);
memset(global_gfx->Data, 0, s);
if (auxil) {
pixel_color = (int *)malloc(s);
memset(pixel_color, 0, s);
} else
pixel_color = global_gfx->Data;
}
void translation(double a, double b, double c)
{
m_rot[3] = a;
m_rot[7] = b;
m_rot[11] = c;
}
void scale(double a)
{
int i;
for (i=0; i<=11; i++) m_rot[i] *= a;
}
void rotation_angles(double theta, double phi, double psi)
{
double ca,cb,cc, sa,sb,sc;
ca=theta*CONV_RAD;
cb=phi*CONV_RAD;
cc=psi*CONV_RAD;
sa=sin(ca); ca=cos(ca);
sb=sin(cb); cb=cos(cb);
sc=sin(cc); cc=cos(cc);
m_rot[0] = ca*cb;
m_rot[1] = -sa*cc-ca*sb*sc;
m_rot[2] = sa*sc-ca*sb*cc;
m_rot[4] = sa*cb;
m_rot[5] = ca*cc-sa*sb*sc;
m_rot[6] = -ca*sc-sa*sb*cc;
m_rot[8] = sb;
m_rot[9] = cb*sc;
m_rot[10]= cb*cc;
}
void rotate(point p, point q)
{
int i,j;
for(i=0; i<=2; i++)
{
j=4*i;
q[i] = m_rot[j]*p[0]+m_rot[j+1]*p[1]+m_rot[j+2]*p[2]+m_rot[j+3];
}
}
void mesh(point *p, int h, int i, int j)
{
point a[3];
double ca,cb,cc,cd, delta, s,t, adds,addt, x, y, z0,z1,z2;
int u1, u2, v1, v2;
int k,l,m;
rotate(p[h],a[0]);
rotate(p[i],a[1]);
rotate(p[j],a[2]);
u1=width; u2=-1;
v1=height; v2=-1;
for(k=0; k<=2; k++)
{
l=(int)((a[k][0]-X1)/ratio_x);
if(l<u1) u1=l;
if(l>u2) u2=l;
l=(int)((Y2-a[k][1])/ratio_y);
if(l<v1) v1=l;
if(l>v2) v2=l;
}
if(u1<0) u1=0;
if(u2>=width) u2=width-1;
if(v1<0) v1=0;
if(v2>=height) v2=height-1;
if(u1>u2 || v1>v2) return;
z0 = a[0][2];
z1 = a[1][2]-z0;
z2 = a[2][2]-z0;
z0 = (z0-Z1)*ratio_z;
z1 = z1*ratio_z;
z2 = z2*ratio_z;
delta = (a[1][0]-a[0][0])*(a[2][1]-a[0][1])
- (a[1][1]-a[0][1])*(a[2][0]-a[0][0]);
if (fabs(delta)<1E-5) return;
delta=1/delta;
ca = (a[2][1]-a[0][1])*delta;
cb = (a[0][0]-a[2][0])*delta;
cc = (a[0][1]-a[1][1])*delta;
cd = (a[1][0]-a[0][0])*delta;
adds = ca*ratio_x;
addt = cc*ratio_x;
for(l=v1; l<=v2; l++)
{
x = X1 + u1*ratio_x - a[0][0];
y = Y2 - l*ratio_y - a[0][1];
s = ca*x + cb*y;
t = cc*x + cd*y;
for(k=u1; k<=u2; k++)
{
if (s>=-0.0001 && t>=-0.0001 && s+t<=1.0001)
{
m = (int) (z0 + s*z1 + t*z2);
if (m<0) m=0;
if (m>color_range) m=color_range;
if (m>pixel_color[k+l*width]) pixel_color[k+l*width] = m;
}
s = s + adds;
t = t + addt;
}
}
}
extern void put_pixel(int x, int y, int r, int g, int b)
{
pixel_color[x+y*width] = r + (g<<8) + (b<<16);
}
void build(BFunction bfunct)
{
bfunct();
}
void light(ZFunction zfunct)
{
int i, j;
int k;
double x,y;
for(j=0; j<height; j++)
{
y = Y2-ratio_y*j;
for(i=0; i<width; i++)
{
x = ratio_x*i+X1;
k = (int)((zfunct(x,y)-Z1)*ratio_z);
if (k<0) k=0;
if (k>color_range) k=color_range;
if (k>pixel_color[i+j*width]) pixel_color[i+j*width] = k;
}
}
}
void graph(PFunction pfunct, double xi, double xs, int nx,
double yi, double ys, int ny)
{
int i, j, l;
double hx,hy;
point p[4];
if(nx<=0 || ny<=0) return;
hx = (xs-xi)/nx;
hy = (ys-yi)/ny;
for(j=0; j<ny; j++)
{
for(l=0; l<=1; l++)
pfunct(p[l],xi,yi+(j+l)*hy);
for(i=1; i<=nx; i++)
{
memcpy(p[2], p[0], sizeof(point));
memcpy(p[3], p[1], sizeof(point));
for(l=0; l<=1; l++)
pfunct(p[l], xi+i*hx,yi+(j+l)*hy);
mesh(p,0,1,2); mesh(p,1,2,3);
}
}
}
void set_range(char c, double u1, double u2)
{
if (width == 0 || height == 0) {
fprintf(stderr, "init_graphic_window() should be used first !!\n");
return;
}
if (c == 'x') {
X1 = u1;
X2 = u2;
ratio_x = (X2-X1)/(double)width;
if (ratio_x == 0)
fprintf(stderr, "Values x1=%g, x2=%g invalid\n", X1, X2);
} else
if (c == 'y') {
Y1 = u1;
Y2 = u2;
ratio_y = (Y2-Y1)/(double)height;
if (ratio_y == 0)
fprintf(stderr, "Values y1=%g, y2=%g invalid\n", Y1, Y2);
} else
if (c == 'z') {
Z1 = u1;
Z2 = u2;
ratio_z = Z2-Z1;
if (ratio_z<=0)
fprintf(stderr, "Values z1=%g, z2=%g invalid\n", Z1, Z2);
else
ratio_z = ((double)color_range)/ratio_z;
}
}
void set_grey_levels()
{
int x, y, k, p;
for (y=0; y<height; y++)
for (x=0; x<width; x++) {
k = x + y*width;
p = pixel_color[k];
if (p)
global_gfx->Data[k] = (p/3) + (((p+1)/3)<<8) + (((p+2)/3)<<16);
}
}
void set_color_levels()
{
if (!auxil) return;
memcpy(global_gfx->Data, pixel_color, width*height*sizeof(int));
}
int GFX_Read_C (FILE *ifile, char *file_name, unsigned char *check_header, struct GFX_DATA *gfx)
{
void *dl_handle = NULL;
void *(* iproc)();
char *ptr;
char cmd[4096];
global_gfx = gfx;
ptr = rindex(file_name, '/');
if (ptr)
ptr += 1;
else
ptr = file_name;
sprintf(cmd, "gcc -c -I. -I./include -I%s %s -o /tmp/stereo-%s.o ; cd /tmp ; "
"gcc -shared -Wl,-soname,stereo-%s.so stereo-%s.o -o stereo-%s.so\n",
((stereograph_include_dir)?
stereograph_include_dir:"."),
file_name, ptr, ptr, ptr, ptr);
system(cmd);
sprintf(cmd, "/tmp/stereo-%s.so", ptr);
dl_handle = dlopen(cmd, RTLD_LAZY);
system("rm -f /tmp/stereo-*");
if (!dl_handle) {
fprintf(stderr, "Couldn't create valid dynamic procedure !!\n");
return -1;
}
iproc = dlsym(dl_handle, "process");
if ((ptr = dlerror()) != NULL || !iproc) {
if (ptr) fprintf(stderr, "Error: %s\n", ptr);
fprintf(stderr, "Couldn't load process() in dynamic procedure !!\n");
return -1;
}
iproc();
dlclose(dl_handle);
if (auxil && pixel_color) {
free(pixel_color);
pixel_color = NULL;
}
return 0;
}
struct error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
static struct error_mgr jerr;
typedef struct error_mgr *error_ptr;
/*
* Here's the routine that will replace the standard error_exit method:
*/
static void
error_exit(j_common_ptr cinfo)
{
char buf[JMSG_LENGTH_MAX];
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
error_ptr err = (error_ptr) cinfo->err;
cinfo->err->format_message(cinfo, buf);
fprintf(stderr, "%s\n", buf);
/* Return control to the setjmp point */
longjmp(err->setjmp_buffer, 1);
}
int GFX_Read_JPG (FILE *ifile, unsigned char *check_header, struct GFX_DATA *gfx)
{
int x, y, z;
struct jpeg_decompress_struct cinfo;
int row_stride, ncolors;
JSAMPROW scanline[1];
char *buffer;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = error_exit;
if (setjmp(jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error.
* We need to clean up the JPEG object, close the input file,
* and return.
*/
jpeg_destroy_decompress(&cinfo);
return GFX_ERROR_LIBJPG;
}
jpeg_create_decompress(&cinfo);
fseek(ifile, 0, SEEK_SET);
jpeg_stdio_src(&cinfo, ifile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
gfx->Width = cinfo.output_width;
gfx->Height = cinfo.output_height;
if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
ncolors = cinfo.desired_number_of_colors;
row_stride = gfx->Width;
} else {
row_stride = gfx->Width * 3;
}
buffer = malloc(row_stride);
scanline[0] = (JSAMPROW) buffer;
gfx->Data = (int*)malloc(gfx->Width*gfx->Height*sizeof(int));
if (!buffer || !gfx->Data)
return GFX_ERROR_MEMORY_PROBLEM;
cinfo.quantize_colors = FALSE;
z = 0;
while (cinfo.output_scanline < gfx->Height) {
(void) jpeg_read_scanlines(&cinfo, scanline, (JDIMENSION) 1);
y = 0;
/* fprintf(stderr, "JPEG : %d\n", cinfo.output_scanline); */
if (cinfo.jpeg_color_space == JCS_GRAYSCALE)
for(x = 0; x < gfx->Width; x++) {
gfx->Data[z] = buffer[y++] * 65793; /* 65536 + 256 + 1 */
z++;
}
else
for(x = 0; x < gfx->Width; x++) {
gfx->Data[z] = buffer[y++];
gfx->Data[z] += buffer[y++] << 8;
gfx->Data[z] += buffer[y++] << 16;
z++;
}
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
free(buffer);
if (jerr.pub.num_warnings > 0) { /* XXX */
fprintf(stderr, "JPEG image may be corrupted\n");
longjmp(jerr.setjmp_buffer, 1);
return GFX_ERROR_LIBJPG;
}
return 0;
}
int GFX_Read_PPM (FILE *ifile, unsigned char *check_header, struct GFX_DATA *gfx)
{
int a, z, c_max;
skip_space_n_comments(ifile);
gfx->Width = get_dec(ifile);
skip_space_n_comments(ifile);
gfx->Height = get_dec(ifile);
gfx->Data = (int*)malloc(gfx->Width*gfx->Height*sizeof(int));
if(!gfx->Data) {
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED;\n");
fprintf(stderr, "error allocating memory for image dimensions of %i * %i pixels in 32 bit\n", gfx->Width, gfx->Height);
return GFX_ERROR_MEMORY_PROBLEM;
}
skip_space_n_comments(ifile);
c_max = 255 / get_dec(ifile);
if(check_header[1] == '3')
for(z = 0; z < (gfx->Width * gfx->Height); z++) {
while(isspace(a = fgetc(ifile))); ungetc(a, ifile);
gfx->Data[z] = get_dec(ifile) * c_max;
while(isspace(a = fgetc(ifile))); ungetc(a, ifile);
gfx->Data[z] += (get_dec(ifile) * c_max) << 8;
while(isspace(a = fgetc(ifile))); ungetc(a, ifile);
gfx->Data[z] += (get_dec(ifile) * c_max) << 16;
}
else if (check_header[1] == '6') {
while(isspace(a = fgetc(ifile))); ungetc(a, ifile);
for(z = 0; z < (gfx->Width * gfx->Height); z++) {
gfx->Data[z] = fgetc(ifile) * c_max;
gfx->Data[z] += (fgetc(ifile) * c_max) << 8;
gfx->Data[z] += (fgetc(ifile) * c_max) << 16;
}
} else {
if (stereograph_verbose) printf("FAILED\n");
fprintf(stderr, "unknown PPM magic!\n");
return GFX_ERROR_UNSUPP_FORMAT;
}
return 0;
}
int GFX_Read_PNG (FILE *ifile, unsigned char *check_header, struct GFX_DATA *gfx)
{
png_voidp user_error_ptr = NULL;
png_structp png_ptr;
png_infop info_ptr;
png_infop end_info;
int bit_depth, color_type, interlace_type, interlace_passes;
//unsigned char header_check[8];
int z;
#if __BYTE_ORDER == __BIG_ENDIAN
int p;
unsigned char r, g, b;
#endif
/*
int p;
unsigned char r, g, b;
*/
/* checking png header */
//fread(header_check, sizeof(char), 8, ifile);
//} else {
/* initializing */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, user_error_ptr, NULL, NULL);
if (!png_ptr) {
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED\n");
fprintf(stderr, "cannot create png read struct!\n");
return GFX_ERROR_LIBPNG;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED\n");
fprintf(stderr, "cannot create png info struct!\n");
return GFX_ERROR_LIBPNG;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED\n");
fprintf(stderr, "cannot create png (end) info struct!\n");
return GFX_ERROR_LIBPNG;
}
/* libpng error handling */
if (setjmp(png_ptr->jmpbuf)) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED\n");
fprintf(stderr, "libpng reported an error!\n");
return GFX_ERROR_LIBPNG;
}
/* setting up file pointer */
png_init_io(png_ptr, ifile);
/* the offset created by the png check */
png_set_sig_bytes(png_ptr, 8);
/* setting up alpha channel for non-transparency */
/* png_set_invert_alpha(png_ptr); */
/* okay, let's get the full header now */
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, NULL, NULL, &bit_depth, &color_type, &interlace_type, NULL, NULL);
/* general format conversions */
/* handle lower bit rates */
/*if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth <= 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))*/
png_set_expand(png_ptr);
/* reduce from 16 bits/channel to 8 bits */
/* correct use of little endian */
/* if (bit_depth == 16) { */
#if __BYTE_ORDER != __BIG_ENDIAN
png_set_swap(png_ptr); // swap byte pairs to little endian
#endif
png_set_strip_16(png_ptr);
/* } */
/* if it's a gray scale, convert it to RGB */
/* if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) */
png_set_gray_to_rgb(png_ptr);
/* convert indexed to full 32 bit RGBA */
/* if (color_type == PNG_COLOR_TYPE_RGB) */
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
/* activate libpng interlace handling */
interlace_passes = png_set_interlace_handling(png_ptr);
/* okay, final info set up */
png_read_update_info(png_ptr, info_ptr);
gfx->Width = png_get_image_width(png_ptr, info_ptr);
gfx->Height = png_get_image_height(png_ptr, info_ptr);
gfx->Data = (int*)malloc( gfx->Width * gfx->Height * sizeof(int) );
if(!gfx->Data) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
if(stereograph_verbose) printf("FAILED;\n"); else fprintf(stderr, "reading gfx data...FAILED;\n");
fprintf(stderr, "error allocating memory for image dimensions of %i * %i pixels in 32 bit\n", gfx->Width, gfx->Height);
return GFX_ERROR_MEMORY_PROBLEM;
}
/* alternatively read it row by row, interlacing as expected */
for(z = 0; z < (gfx->Height * interlace_passes); z++)
png_read_row(png_ptr, (void *) (gfx->Data + ((z % gfx->Height) * gfx->Width)), NULL);
#if __BYTE_ORDER == __BIG_ENDIAN
for(z = 0; z < (gfx->Width * gfx->Height); z++) {
p = gfx->Data[z];
r = (p>>24)&255;
g = (p>>16)&255;
b = p>>8;
gfx->Data[z] = r + (g<<8) + (b<<16);
}
#endif
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
/* that's it */
//}
return 0;
}
int GFX_Write_JPG (FILE *ofile, struct GFX_DATA *gfx)
{
struct jpeg_compress_struct cinfo;
int i, z, p, row_stride;
JSAMPROW scanline[1];
unsigned char *d, *buffer = NULL;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = error_exit;
if (setjmp(jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error.
* We need to clean up the JPEG object, close the output file,
* and return.
*/
jpeg_destroy_compress(&cinfo);
return GFX_ERROR_LIBJPG;
}
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, ofile);
cinfo.image_width = gfx->Width;
cinfo.image_height = gfx->Height;
cinfo.in_color_space = JCS_RGB;
cinfo.input_components = 3;
buffer = malloc(gfx->Width * 3);
if (!buffer)
return GFX_ERROR_MEMORY_PROBLEM;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
row_stride = gfx->Width * cinfo.input_components;
scanline[0] = buffer;
z = 0;
while (cinfo.next_scanline < cinfo.image_height) {
/*
* If we have a greyscale or TrueColor image, just feed
* the raw data to the JPEG routine. Otherwise, we must
* build an array of RGB triples in 'buffer'.