-
Notifications
You must be signed in to change notification settings - Fork 1
/
otk_lib.c
8755 lines (7633 loc) · 304 KB
/
otk_lib.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
/****************************************************************************************/
/* Otk_lib.c - A general purpose GUI environment based on OpenGL and GLUT or X-Windows. */
/* (GUI = Graphical User Interface.) */
/* */
/* For Documentation and Usage Notes, see: http://otk.sourceforge.net/ */
/* */
/* To Compile on Unix/Linux: */
/* cc -c -I/usr/X11R6/include otk_lib.c -o otk.o */
/* Link with: -lGLU -lGL -lXmu -lXext -lX11 */
/* */
/* To Compile on Microsoft using MinGW compiler: */
/* gcc -c otk_lib.c -o otk.o */
/* Link with: -lglu32 -lopengl32 -lwinmm -lgdi32 */
/* */
/* Copyright (C) 2002-2009: Carl Kindman */
/* */
/* Lessor GNU Public License - LGPL: */
/* 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 Lessor 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 */
/* */
/* Carl Kindman 8-1-2006 [email protected] */
/* Carl Kindman 9-7-2007 Updated file browser. */
/* Carl Kindman 4-21-2008 Updated image handling. */
/* Carl Kindman 6-21-2008 Added automatic test capture+playback. */
/* Carl Kindman 8-10-2008 Added hover-boxes. */
/* Carl Kindman 9-2-2008 v0.74 Enhanced MS-Win timer. Enabled slider-slot clicking. */
/* Carl Kindman 4-22-2009 v0.84 Activated text-selection highlighting in form-boxes. */
/* Carl Kindman 1-1-2012 v0.95 Added Otk_Set_Timer. */
/* (See release notes for complete revision history.) */
/****************************************************************************************/
#define Otk_version 0.96
#define Otk_MAJOR_VERSION 0
#define Otk_MINOR_VERSION 96
#define Otk_MINOR_REVNUM 0
/* Set debug switch. 1=verbose. 0=quiet. */
#define DEBUG if (0)
#include "otk_lib.h"
// #include "scz_compress/scz.h" /* Optionally, include self-contained decompression library. */
// #include "scz_compress/scz_compress_lib.c"
// #include "scz_compress/scz_decompress_lib.c"
// #include "scz_compress/scz_streams.c"
int Otk_verbose=0;
int OtkWindowSizeX=-1, OtkWindowSizeY=-1;
float Otk_AspectAngle=1.0;
int Otk_showkey=0;
int Otk_MouseButtonState[9]={0,0,0,0,0,0,0,0,0};
int Otk_Clicked_MouseButton=0;
int Otk_MousePixX=-1, Otk_MousePixY=-1, /* Current mouse position in pixels, on click or drag. */
Otk_MouseLastX, Otk_MouseLastY; /* Last mouse position while dragging. */
float Otk_MouseX=-1.0, Otk_MouseY=-1.0; /* Current mouse position in % window coords, on click or during drag. */
float Otk_ClickedX=-1.0; /* Position clicked on down-click or start of drag, in % window coords. */
float Otk_ClickedY=-1.0;
float Otk_DZ=2.0; /* Layer Z spacing. Nominally=1.0. */
int otk_native_window=1;
int Otk_Display_Changed = 4, Otk_nondraws=0, Otk_Always_Update=0;
float Otk_sqrtaspect=1.0, otk_render_quality_hint=1.0;
char *otk_window_name="Otk";
FILE *otk_test_file=0;
int otk_test_playback=0, otk_test_capnum=0;
int otk_highest_qual_tried=0, otk_render_qual_level=0; /* Quality levels span 0 to 4. */
int allow_cpu_antialiasing=1;
int otk_got_accumbuf=1;
double otk_last_redraw_time, OTK_FRAME_PERIOD=0.0;
#if (PLATFORM_KIND==MsVisC_Platform)
#ifndef calloc
#define calloc(n,s) (memset(malloc(n*s),0,(n*s)))
#endif
#ifndef strdup
#define strdup(s) (strcpy(malloc(strlen(s)+1), s))
#endif
#ifdef WIN32
int strcasecmp( const char *str1, char *str2 )
{
int j=-1, cmp;
do
{
j++;
cmp = tolower( str1[j] ) - tolower( str2[j] );
}
while ((cmp == 0) && (str1[j] != '\0') && (str2[j] != '\0'));
return cmp;
}
#endif
#endif
#include "letter2vector2.c"
struct OtkObjectInstance *OtkRootObject=0, *OtkOuterWindow=0, *Otk_keyboard_focus=0, *Otk_OpenMenu=0;
/* Modal parameters. */
float Otk_border_thickness = 1.0;
struct Otkkeyboardstatus
{
int shiftkey;
int ctrlkey;
int keypad_numlock;
int textform_insertion_column, mouse_select_start_column, mouse_select_end_column, mouse_select_old_column;
int textform_insertion_row;
int cursor_blink_count;
} Otk_Keyboard_state;
struct
{
float x, y, z;
float pointx, pointy, pointz;
int moved;
float fov, minZ, maxZ;
} OtkCameraPosition;
OtkWidget Otk_ClickedObject=0, otk_highlighted_text=0, otk_last_scrollable=0;
struct Otk_image *Otk_image_list=0;
int OtkTextureNumber=0, otk_current_mouse_cursor_style=0;
/*=============================================v= Otk =v=============================================*/
/* Otk variables. */
void OtkSetBorderThickness( float thickness ) /* Deprecated in favor of Otk_Set_Default_Border_Thickness(...) */
{ Otk_border_thickness = thickness; }
void Otk_Set_Default_Border_Thickness( float thickness ) /* Replaces OtkSetBorderThickness(...) */
{ Otk_border_thickness = thickness; }
void Otk_Set_Object_Border_Thickness( OtkWidget obj, float thickness )
{ obj->thickness = thickness; }
float Otk_BLACK[4] = {0.0, 0.0, 0.0, 1.0};
OtkColor Otk_Default_Button_Color;
int Otk_Default_Button_Outline_Style=1;
OtkColor OtkSetColor( float r, float g, float b )
{
OtkColor tmpc;
tmpc.r = r;
tmpc.g = g;
tmpc.b = b;
return tmpc;
}
void OtkSetColorVector( float r, float g, float b, OtkColorVector tmpc )
{
tmpc[0] = r;
tmpc[1] = g;
tmpc[2] = b;
tmpc[3] = 1.0;
}
void OtkTranslateColor( OtkColor Cin, OtkColorVector tmpc )
{
tmpc[0] = Cin.r;
tmpc[1] = Cin.g;
tmpc[2] = Cin.b;
tmpc[3] = 1.0;
}
void OtkCopyColor( OtkColorVector dst, OtkColorVector src )
{ int j;
for (j=0; j < 4; j++) dst[j] = src[j];
}
int Otk_snifferrors( int ernum )
{ int error, ecnt=0;
while ((error = glGetError()) != GL_NO_ERROR)
{ecnt++; printf("GL error %d: %s\n", ernum, gluErrorString(error) );}
return ecnt;
}
void OtkInitLighting()
{
glEnable( GL_CULL_FACE ); /* Save performance! */
glEnable( GL_DEPTH_TEST ); /* Depth buffering. */
}
void OtkSetViewWindow( float xmin, float xmax, float ymin, float ymax, float Zmin, float Zmax )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( xmin, xmax, ymin, ymax, Zmin, Zmax );
}
void OtkSetCameraPosition( float CamX, float CamY, float CamZ,
float PointX, float PointY, float PointZ,
float OrienX, float OrienY, float OrienZ
)
{
glMatrixMode( GL_MODELVIEW );
gluLookAt( CamX, CamY, CamZ, PointX, PointY, PointZ, OrienX, OrienY, OrienZ );
}
void Otk_Set_Camera( float minx, float maxx, float miny, float maxy, float minZ, float maxZ, float cx, float cy, float cz )
{ /*set_camera*/
OtkCameraPosition.x = cx;
OtkCameraPosition.y = cy;
OtkCameraPosition.z = cz;
OtkCameraPosition.minZ = minZ;
OtkCameraPosition.maxZ = maxZ;
OtkSetViewWindow( minx, maxx, miny, maxy, minZ, maxZ ); /* minx, max, miny, maxy, minZ, maxZ. */
/* Camera location (x,y,z), Pointed at (x,y,z), Up direction (x,y,z). */
OtkCameraPosition.pointx = 0.0;
OtkCameraPosition.pointy = 0.0;
OtkCameraPosition.pointz = 0.0;
OtkSetCameraPosition( OtkCameraPosition.x, OtkCameraPosition.y, OtkCameraPosition.z, OtkCameraPosition.pointx, OtkCameraPosition.pointy, OtkCameraPosition.pointz, 0.0, 1.0, 0.0 );
} /*set_camera*/
void Otk_Set_Window_Name( char *name )
{
otk_window_name = strdup( name );
}
/* FONTS begin */
void Otk_glShear( float xy, float xz, float yx, float yz, float zx, float zy )
{
float m[16] = { 1, xy, xz, 0, yx, 1, yz, 0, zx, zy, 1, 0, 0, 0, 0, 1 };
glMultMatrixf( m );
}
/* FONTS end */
#define mouse_groove_click 6
#if (WinGLEnabled==0)
Bool OtkMultiSample=0;
#else
int OtkMultiSample=0;
#endif
/* Prototypes */
OtkWidget OtkMakePanel( OtkWidget container, int panel_subtype, OtkColor panel_color, float left, float top, float horiz_size, float vert_size );
void otk_update_draw_object( OtkWidget objpt ); /* Redraw just the given object. For rapid updates when */
void Otk_Set_Default_Button_Color( float r, float g, float b );
void Otk_swap_buffers();
void Otk_fb_clear_last_selected();
void otk_check_timer();
int Otk_handle_mouse_click( int state, int button_number );
int BLEND=0;
double otk_started_time;
int otk_realize_image=1; /* Flag to control display of read image, or to get image-data only. */
void otk_set_realize_image( int j ) { otk_realize_image = j; }
void Otk_Get_Screen_Size( int *scrn_width, int *scrn_height ) /* Passes back screen size in pixels. */
{
/* #if (PLATFORM_KIND == Mingw_Platform)
*scrn_width = GetSystemMetrics(SM_CXSCREEN);
*scrn_height = GetSystemMetrics(SM_CYSCREEN);
#else */
Display *Otkdpy;
int screen_number;
Otkdpy = XOpenDisplay(NULL);
screen_number = DefaultScreen( Otkdpy);
*scrn_width = DisplayWidth( Otkdpy, screen_number );
*scrn_height = DisplayHeight( Otkdpy, screen_number );
XCloseDisplay(Otkdpy);
// #endif
}
void OtkMakeOuterWindow()
{
otk_started_time = otk_report_time();
OtkInitLighting();
if (Otk_snifferrors(300)) printf("OGL Errors on initial read-in.\n");
glClearColor( 0.8, 0.8, 0.85, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
Otk_swap_buffers();
Otk_Set_Camera( 0.0, 100.0, -100.0, 0.0, 5.0, 510.0, 0.0, 0.0, 505.0 ); /* minx, maxx, miny, maxy, minZ, maxZ, camera_x_position, camera_y_position, camera_z_position. */
/* Make the outer window. */
OtkMakePanel( 0, Otk_subtype_plane, OtkSetColor( 0.75, 0.75, 0.75 ), 0.0, 0.0, 100.0, 100.0 );
glDisable( GL_LIGHTING );
/* For transparency. */
glBlendFunc( GL_SRC_ALPHA, GL_DST_ALPHA );
if (BLEND)
{
if (OtkMultiSample)
{
#if (WinGLEnabled==0)
if (Otk_verbose) printf("Enabling MultiSampling\n");
glEnable(GL_MULTISAMPLE);
glHint( GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST );
#endif
}
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST ); // GL_DONT_CARE
}
Otk_Set_Default_Button_Color( 0.75, 0.75, 0.75 );
Otk_Set_HoverBox_Style( OtkSetColor( 1.0, 1.0, 0.75 ), Otk_Black, 1.0 );
}
void Otk_AcceptCommandLineArguments( int argc, char **argv )
{
int k=1;
printf("\nUsing OTK - V%1.2f\n\n", Otk_version);
k = 1;
while (k<argc)
{
if (argv[k][0]=='-')
{
if (strcmp(argv[k],"-otk_v")==0)
{
k++; if (argc <= k) {printf("ERROR: Missing verbosity level.\n"); exit(0);}
if (sscanf(argv[k],"%d",&Otk_verbose)!=1) {printf("ERROR: Bad verbosity level '%s'.\n",argv[k]); exit(0);}
printf(" Setting verbosity to %d\n", Otk_verbose);
}
else
if (strcmp(argv[k],"-windowsz_x")==0)
{
k++; if (argc <= k) {printf("ERROR: Missing windowsz_x value.\n"); exit(0);}
if (sscanf(argv[k],"%d",&OtkWindowSizeX)!=1) {printf("ERROR: Bad windowsz_x '%s'.\n",argv[k]); exit(0);}
printf(" Setting windowsz_x to %d\n", OtkWindowSizeX);
}
else
if (strcmp(argv[k],"-windowsz_y")==0)
{
k++; if (argc <= k) {printf("ERROR: Missing windowsz_y value.\n"); exit(0);}
if (sscanf(argv[k],"%d",&OtkWindowSizeY)!=1) {printf("ERROR: Bad windowsz_y '%s'.\n",argv[k]); exit(0);}
printf(" Setting windowsz_y to %d\n", OtkWindowSizeY);
}
else
if (strcmp(argv[k],"-aspect_ratio")==0)
{
k++; if (argc <= k) {printf("ERROR: Missing Aspect Ratio value.\n"); exit(0);}
if (sscanf(argv[k],"%f",&Otk_AspectAngle)!=1) {printf("ERROR: Bad Aspect Ratio '%s'.\n",argv[k]); exit(0);}
printf(" Setting Aspect Ratio to %g\n", Otk_AspectAngle);
}
else
if (strcmp(argv[k],"-dz")==0)
{
k++; if (argc <= k) {printf("ERROR: Missing dz value.\n"); exit(0);}
if (sscanf(argv[k],"%f",&Otk_DZ)!=1) {printf("ERROR: Bad dz '%s'.\n",argv[k]); exit(0);}
printf(" Setting DZ to %g\n", Otk_DZ);
}
else
if (strcmp(argv[k],"-blend")==0)
{
BLEND = 1;
printf(" Setting BLEND\n");
}
else
if (strcmp(argv[k],"-otk_test_capture")==0)
{
Otk_Start_Capturing_TestFile();
}
else
if (strcmp(argv[k],"-otk_test_play")==0)
{
otk_test_playback = 1;
}
else
if (strcmp(argv[k],"-otk_make_movie")==0)
{
Otk_SnapShot_Size(2); /* Set snap-shoot to half-sized, for better speed. */
Otk_MakeMovie(1); /* Turn movie frame capture on. */
}
else
if (strcmp(argv[k],"-h")==0)
{
printf("\nOtk Option Summary:\n");
printf(" -v xx : Set verbosity. Default = 0.\n");
printf(" -windowsz_x : Initial window width.\n");
printf(" -windowsz_y : Initial window heigth.\n");
printf(" -aspect_ratio : Aspect ratio.\n");
printf(" -otk_test_capture : Record test file.\n");
printf(" -otk_test_play : Read test file.\n");
printf(" -h : Show options.\n");
}
// else printf("Unknown option '%s'\n", argv[k]);
}
k++;
}
Otk_Keyboard_state.shiftkey = 0;
}
float Otk_Set_Render_Quality_Hint( float q ) /* Sets render-quality-hint. Returns previous value. */
{
float oldq=1.0/(fabs(otk_render_quality_hint)+1e-12);
otk_render_quality_hint = 1.0 / (fabs(q) + 1.0e-12); /* Prevent divide-by-zero. */
return oldq;
}
void Otk_Dispose_Image( char *filename )
{ /* Frees all resources associated with named image. */
struct Otk_image *image, *prv=0;
#if (PLATFORM_KIND != MsVisC_Platform)
image = Otk_image_list; /* Check to find image in list. */
while ((image!=0) && (strcmp(image->filename,filename)!=0))
{ prv = image; image = image->nxt; }
if (image == 0) { printf("Otk_Dispose_Image: '%s' not presently loaded.\n", filename ); return; /* Not in list. */ }
if (prv == 0) Otk_image_list = image->nxt; /* Remove from list. */
else prv->nxt = image->nxt;
glDeleteTextures( 1, &(image->texturename) ); /* Remove resources. */
free( image->image );
free( image->textureimage );
free (image );
#endif
}
struct Otk_image *Otk_Read_Image_File( char *fname )
{
struct Otk_image *image;
#if (PLATFORM_KIND != MsVisC_Platform)
FILE *infile;
char buf[2048], capsfname[2048], *ofname;
int ispipe=0, istmp=0;
int magic[2], ch;
int nrows, ncols, maxval, row, col=0, r, g, b, maximgsz;
unsigned char rc, bc, gc;
image = Otk_image_list; /* Check to see if this image file was already read. */
while ((image!=0) && (strcmp(image->filename,fname)!=0)) image = image->nxt;
/* If already read, then return pointer to image. */
if (image!=0) { return image; }
ofname = fname;
do {capsfname[col] = toupper(fname[col]); col++; } while (fname[col-1]!='\0');
#ifdef SCZ_DEFS
{
struct Otk_image *Otk_Read_SCZ_Image_File( char *fname, char *capsfname );
if ( ((strstr(capsfname,".SCZ")!=0) && (strlen(strstr(capsfname,".SCZ"))==4))
|| ((strstr(capsfname,".ISCZ")!=0) && (strlen(strstr(capsfname,".ISCZ"))==5)) )
{ return Otk_Read_SCZ_Image_File( fname, capsfname ); }
}
#endif
/* Check if image needs to be converted. */
if ((strstr(capsfname,".JPG")!=0) || (strstr(capsfname,".JPEG")!=0))
{
sprintf(buf,"jpegtopnm %s > tmpimg.ppm", fname);
system(buf); fname = strdup("tmpimg.ppm"); istmp = 1;
}
else
if (strstr(capsfname,".GIF")!=0)
{
sprintf(buf,"giftopnm %s > tmpimg.ppm", fname);
system(buf); fname = strdup("tmpimg.ppm"); istmp = 1;
}
else
if (strstr(capsfname,".PNG")!=0)
{
sprintf(buf,"pngtopnm %s > tmpimg.ppm", fname);
system(buf); fname = strdup("tmpimg.ppm"); istmp = 1;
}
infile = fopen(fname,"rb");
if (infile==0) { printf("ERROR: Could not open '%s'.\n", fname); return 0; }
if (Otk_verbose) printf("READING IMAGE %s\n",fname);
magic[0] = getc(infile); /* Read magic numerals. */
magic[1] = getc(infile);
if ( magic[0] != 'P' || magic[1] < '1' || magic[1] > '6' )
{
fclose( infile );
snprintf( buf, 2048, "anytopnm %s", fname );
infile = popen( buf, "rb" );
if (infile==0) { printf("OTK ERROR: Could not read or convert '%s'.\n", fname); return 0; }
ispipe = 1;
magic[0] = getc(infile);
magic[1] = getc(infile);
}
switch ( magic[1] )
{
case '1': /* pbm bitmap */
case '4': /* binary */
printf("ERROR: Bad PBM image file '%s'.\n", fname);
return 0;
break;
case '2': /* pgm greyscale */
case '5': /* binary */
case '3': /* ppm pixmap */
case '6': /* binary */
while ( (ch = getc(infile)) && isspace(ch) ); /* Chew whitespace. */
while ( ch == '#' )
{
while ( getc(infile) != '\n' ); /* Eat comment line. */
ch = getc(infile);
}
ungetc( ch, infile );
fscanf( infile, "%d", &ncols ); /* Number of columns */
while ( (ch = getc(infile)) && isspace(ch) ); /* chew whitespace */
while ( ch == '#' )
{
while ( getc(infile) != '\n' ); /* Eat comment line. */
ch = getc(infile);
}
ungetc( ch, infile );
fscanf( infile, "%d", &nrows ); /* Number of rows. */
while ( (ch = getc(infile)) && isspace(ch) ); /* chew whitespace */
while ( ch == '#' )
{
while ( getc(infile) != '\n' ); /* Eat comment line. */
ch = getc(infile);
}
ungetc( ch, infile );
fscanf( infile, "%d", &maxval ); /* Maximum value of a pixel. */
while (getc(infile)!='\n');
if ((ncols * nrows < 0) || (ncols * nrows > 25000000))
{ printf("Otk Error: Image size %d x %d out of range.\n", ncols, nrows ); return 0; }
/* Get the data. */
image = (struct Otk_image *)malloc( sizeof(struct Otk_image) );
image->filename = (char *)strdup(ofname);
image->image = (struct Otk_image_rec *)malloc( (ncols * nrows) * sizeof(struct Otk_image_rec) );
if (image->image == 0) {printf("Otk Error: Could not allocate image memory.\n"); return 0; }
image->native_cols = ncols; image->native_rows = nrows;
image->cols = ncols; image->rows = nrows;
if (nrows<=32) image->texturerows = 32; else
if (nrows<=64) image->texturerows = 64; else
if (nrows<=128) image->texturerows = 128; else
if (nrows<=256) image->texturerows = 256; else
if (nrows<=512) image->texturerows = 512; else
if (nrows<=1024) image->texturerows = 1024; else
if (nrows<=2048) image->texturerows = 2048; else image->texturerows = 4096;
if (ncols<=32) image->texturecols = 32; else
if (ncols<=64) image->texturecols = 64; else
if (ncols<=128) image->texturecols = 128; else
if (ncols<=256) image->texturecols = 256; else
if (ncols<=512) image->texturecols = 512; else
if (ncols<=1024) image->texturecols = 1024; else
if (ncols<=2048) image->texturecols = 2048; else image->texturecols = 4096;
image->texturesize = image->texturerows * image->texturecols;
image->textureimage = (GLubyte *)malloc( 4 * image->texturesize * sizeof(GLubyte) );
if (image->textureimage == 0) {printf("Otk Error: Could not allocate image memory.\n"); return 0; }
image->texturename = OtkTextureNumber++;
if ( magic[1] == '6' )
{ /* binary */
int k=0;
for (row=0; row != nrows; row++)
{
for (col=0; col != ncols; col++)
{
fread( &rc, 1, 1, infile );
fread( &gc, 1, 1, infile );
fread( &bc, 1, 1, infile );
image->image[k].r = rc;
image->image[k].b = bc;
image->image[k].g = gc;
k++;
}
}
}
else
if ( magic[1] == '5' )
{ /* binary */
int k=0;
for (row=0; row != nrows; row++)
for (col=0; col != ncols; col++)
{
fread( &rc, 1, 1, infile );
image->image[k].r = rc;
image->image[k].b = rc;
image->image[k].g = rc;
k++;
}
}
else
{ /* ascii */
int k=0;
for (row=0; row != nrows; row++)
for (col=0; col != ncols; col++)
{
fscanf(infile,"%d",&r); fscanf(infile,"%d",&g); fscanf(infile,"%d",&b);
image->image[k].r = r;
image->image[k].b = b;
image->image[k++].g = g;
}
}
break;
}
if( ispipe ) pclose( infile );
else fclose( infile );
// printf("nrows (columns) = %d, ncols (rows) = %d\n", nrows, ncols );
if (istmp) { remove("tmpimg.ppm"); free(fname); }
if (otk_realize_image == 0) { return image; }
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maximgsz );
/* Force nrows to equal ncols and be power of 2 for OGL. */
if ((nrows != ncols) || (image->texturerows != image->rows) || (image->texturerows > maximgsz))
{
int new_nrows, new_ncols, j, k=2, m;
float tmpfc, tmpfr;
struct Otk_image_rec *tmpimage;
if (nrows > ncols)
{ new_nrows = nrows; new_ncols = nrows; }
else
{ new_nrows = ncols; new_ncols = ncols; }
while (k < new_nrows) k = 2 * k;
if (k > maximgsz) k = maximgsz; /* Limit size if larger than current display capability. */
new_nrows = k; new_ncols = k;
tmpimage = (struct Otk_image_rec *)malloc( (new_nrows * new_ncols) * sizeof(struct Otk_image_rec) );
if (tmpimage == 0) {printf("Otk Error: Could not allocate image memory.\n"); return 0; }
m = 0;
tmpfc = (float)ncols / (float)new_ncols;
tmpfr = (float)nrows / (float)new_nrows;
for (row=0; row<new_nrows; row++)
{
j = (float)row * tmpfr;
j = j * ncols;
for (col=0; col<new_ncols; col++)
{
k = j + (float)col * tmpfc;
tmpimage[m].r = image->image[k].r;
tmpimage[m].g = image->image[k].g;
tmpimage[m].b = image->image[k].b;
m++;
}
}
free( image->image );
image->image = tmpimage;
image->cols = new_nrows; image->rows = new_ncols;
image->texturerows = new_nrows;
image->texturecols = new_nrows;
image->texturesize = image->texturerows * image->texturecols;
free(image->textureimage);
image->textureimage = (GLubyte *)malloc( 4 * image->texturesize * sizeof(GLubyte) );
if (image->textureimage == 0) {printf("Otk Error: Could not allocate image memory.\n"); return 0; }
}
{
int p = 0, k, m = 0;
for (row=0; row < image->texturerows; row++)
{
k = p;
for (col=0; col < image->texturecols; col++)
{
image->textureimage[m++] = image->image[k].r;
image->textureimage[m++] = image->image[k].g;
image->textureimage[m++] = image->image[k].b;
image->textureimage[m++] = 255;
k++;
}
p = p + image->cols;
}
}
image->nxt = Otk_image_list;
Otk_image_list = image;
image->calllist_num = glGenLists(1);
glDisable( GL_TEXTURE_2D );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glGenTextures( 1, &(image->texturename) );
glBindTexture( GL_TEXTURE_2D, image->texturename );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, image->texturerows, image->texturecols, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->textureimage );
glNewList( image->calllist_num, GL_COMPILE );
glBindTexture( GL_TEXTURE_2D, image->texturename );
glEndList();
#else
image = 0; /* Unsupported. */
#endif
return image;
}
#ifdef SCZ_DEFS
struct Otk_image *Otk_Read_SCZ_Image_File( char *fname, char *capsfname )
{
struct Otk_image *image;
#if (PLATFORM_KIND != MsVisC_Platform)
int magic[2], ch;
int nrows, ncols, maxval, row, col=0, r, g, b, maximgsz;
unsigned char rc, bc, gc;
char *buffer;
int bufindx=0, buflen;
if (Otk_verbose) printf("READING IMAGE %s\n",fname);
if (Scz_Decompress_File2Buffer( fname, &buffer, &buflen )!=1)
{ printf("SCZ Error: Unable to decompress file '%s'.\n", fname); return 0; }
magic[0] = buffer[bufindx++]; /* Read magic numerals. */
magic[1] = buffer[bufindx++];
if ( magic[0] != 'P' || magic[1] < '1' || magic[1] > '6' )
{
printf("OTK ERROR: Could not read or convert '%s'.\n", fname);
free(buffer);
return 0;
}
switch ( magic[1] )
{
case '1': /* pbm bitmap */
case '4': /* binary */
printf("ERROR: Bad PBM image file '%s'.\n", fname);
free(buffer);
return 0;
break;
case '2': /* pgm greyscale */
case '5': /* binary */
case '3': /* ppm pixmap */
case '6': /* binary */
do
{
while ( buffer[bufindx++] != '\n' ); /* Advance to next line. */
while ( isspace(buffer[bufindx]) ) bufindx++;
}
while (buffer[bufindx]=='#');
ncols = atoi( &(buffer[bufindx]) ); /* Number of columns */
while ( ! isspace(buffer[bufindx]) ) bufindx++; /* Advance to next item. */
while ( isspace(buffer[bufindx]) ) bufindx++;
while (buffer[bufindx]=='#')
{
while ( buffer[bufindx++] != '\n' ); /* Advance to next line. */
while ( isspace(buffer[bufindx]) ) bufindx++;
}
nrows = atoi( &(buffer[bufindx]) ); /* Number of rows. */
while ( ! isspace(buffer[bufindx]) ) bufindx++; /* Advance to next item. */
while ( isspace(buffer[bufindx]) ) bufindx++;
while (buffer[bufindx]=='#')
{
while ( buffer[bufindx++] != '\n' ); /* Advance to next line. */
while ( isspace(buffer[bufindx]) ) bufindx++;
}
maxval = atoi( &(buffer[bufindx]) ); /* Maximum value of a pixel. */
while (buffer[bufindx++]!='\n');
if ((nrows * ncols) > 25000000) {printf("Image too large.\n"); exit(0);}
if ((ncols * nrows < 0) || (ncols * nrows > 25000000))
{ printf("Otk Error: Image size %d x %d out of range.\n", ncols, nrows ); return 0; }
/* Get the data. */
image = (struct Otk_image *)malloc( sizeof(struct Otk_image) );
image->filename = (char *)strdup(fname);
image->image = (struct Otk_image_rec *)malloc( (ncols * nrows) * sizeof(struct Otk_image_rec) );
if (image->image == 0) {printf("Otk Error: Could not allocate image memory.\n"); free(buffer); return 0; }
image->native_cols = ncols; image->native_rows = nrows;
image->cols = ncols; image->rows = nrows;
if (nrows<=32) image->texturerows = 32; else
if (nrows<=64) image->texturerows = 64; else
if (nrows<=128) image->texturerows = 128; else
if (nrows<=256) image->texturerows = 256; else
if (nrows<=512) image->texturerows = 512; else
if (nrows<=1024) image->texturerows = 1024; else
if (nrows<=2048) image->texturerows = 2048; else image->texturerows = 4096;
if (ncols<=32) image->texturecols = 32; else
if (ncols<=64) image->texturecols = 64; else
if (ncols<=128) image->texturecols = 128; else
if (ncols<=256) image->texturecols = 256; else
if (ncols<=512) image->texturecols = 512; else
if (ncols<=1024) image->texturecols = 1024; else
if (ncols<=2048) image->texturecols = 2048; else image->texturecols = 4096;
image->texturesize = image->texturerows * image->texturecols;
image->textureimage = (GLubyte *)malloc( 4 * image->texturesize * sizeof(GLubyte) );
if (image->textureimage == 0) {printf("Otk Error: Could not allocate image memory.\n"); free(buffer); return 0; }
image->texturename = OtkTextureNumber++;
if ( magic[1] == '6' )
{ /* binary */
int j, k=0;
if (buflen < 3 * nrows * ncols + bufindx) {printf("Otk ERROR: Image Buffer too small reading '%s'. buffer=%d < image= %d\n", fname, buflen, 3 * nrows * ncols + bufindx); exit(0);}
for (row=0; row!=nrows; row++)
{
for (col=0; col!=ncols; col++)
{
rc = buffer[bufindx++];
gc = buffer[bufindx++];
bc = buffer[bufindx++];
j = k + col;
image->image[j].r = rc;
image->image[j].b = bc;
image->image[j].g = gc;
}
k = k + ncols;
}
}
else
if ( magic[1] == '5' )
{ /* binary */
if (buflen < 3 * nrows * ncols + bufindx) {printf("Image Buffer too small. %d<%d\n", buflen, 3 * nrows * ncols + bufindx); exit(0);}
for (row=0; row!=nrows; row++)
for (col=0; col!=ncols; col++)
{
rc = buffer[bufindx++];
image->image[row*ncols+col].r = rc;
image->image[row*ncols+col].b = rc;
image->image[row*ncols+col].g = rc;
}
}
else
{ /* ascii */
for (row=0; row!=nrows; row++)
for (col=0; col!=ncols; col++)
{
r = atoi( &(buffer[bufindx]) ); while (!isspace(buffer[bufindx++]));
if (bufindx>buflen) {printf("Image Buffer too small.\n"); exit(0);}
g = atoi( &(buffer[bufindx]) ); while (!isspace(buffer[bufindx++]));
if (bufindx>buflen) {printf("Image Buffer too small.\n"); exit(0);}
b = atoi( &(buffer[bufindx]) ); while (!isspace(buffer[bufindx++]));
if (bufindx>buflen) {printf("Image Buffer too small.\n"); exit(0);}
image->image[row*ncols+col].r = r;
image->image[row*ncols+col].b = b;
image->image[row*ncols+col].g = g;
}
}
break;
}
if (Otk_verbose>1) printf(" %d rows by %d columns.\n", nrows, ncols );
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maximgsz );
if ((strstr(capsfname,".ISCZ")!=0) && (strlen(strstr(capsfname,".ISCZ"))==5))
{ /*iscz*/
struct Otk_image_rec imel, last_imel;
int k=0, m, tmpv;
/* Integrate the image, to convert it from differential encoding. */
for (row=0; row < nrows; row++)
{
last_imel = image->image[k];
for (col=1; col < ncols; col++)
{
m = k + col;
imel = image->image[ m ];
tmpv = imel.r + last_imel.r - 127.0;
if ((int)(tmpv) < 0) tmpv = 0; else
if ((int)(tmpv) > 255) tmpv = 255;
image->image[m].r = tmpv;
tmpv = imel.g + last_imel.g - 127.0;
if ((int)(tmpv) < 0) tmpv = 0; else
if ((int)(tmpv) > 255) tmpv = 255;
image->image[m].g = tmpv;
tmpv = imel.b + last_imel.b - 127.0;
if ((int)(tmpv) < 0) tmpv = 0; else
if ((int)(tmpv) > 255) tmpv = 255;
image->image[m].b = tmpv;
last_imel = image->image[ m ];
}
k = k + ncols;
}
} /*iscz*/
/* Force nrows to equal ncols and be power of 2 for OGL. */
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maximgsz );
if ((nrows != ncols) || (image->texturerows != image->rows) || (image->texturerows > maximgsz))
{
int new_nrows, new_ncols, j, k=2, m1, m2;
float tmpfr, tmpfc;
struct Otk_image_rec *tmpimage;
if (nrows > ncols)
{ new_nrows = nrows; new_ncols = nrows; }
else
{ new_nrows = ncols; new_ncols = ncols; }
while (k < new_nrows) k = 2 * k;
if (k > maximgsz) k = maximgsz;
new_nrows = k; new_ncols = k;
tmpimage = (struct Otk_image_rec *)malloc( (new_nrows * new_ncols) * sizeof(struct Otk_image_rec) );
if (tmpimage == 0) {printf("Otk Error: Could not allocate image memory.\n"); free(buffer); return 0; }
m1 = 0;
tmpfc = (float)ncols / (float)new_ncols;
tmpfr = (float)nrows / (float)new_nrows;
for (row=0; row<new_nrows; row++)
{
j = (float)row * tmpfr;
m2 = j * ncols;
for (col=0; col<new_ncols; col++)
{
k = m2 + (int)((float)col * tmpfc);
tmpimage[m1].r = image->image[k].r;
tmpimage[m1].g = image->image[k].g;
tmpimage[m1].b = image->image[k].b;
m1++;
}
}
if (Otk_verbose>1) printf(" Forcing resize to %d rows by %d columns.\n", new_nrows, new_ncols );
free( image->image );
image->image = tmpimage;
image->cols = new_nrows; image->rows = new_ncols;
image->texturerows = new_nrows;
image->texturecols = new_nrows;
image->texturesize = image->texturerows * image->texturecols;
free(image->textureimage);
image->textureimage = (GLubyte *)malloc( 4 * image->texturesize * sizeof(GLubyte) );
if (image->textureimage == 0) {printf("Otk Error: Could not allocate image memory.\n"); free(buffer); return 0; }
}
{
int p = 0, k, m = 0;
for (row=0; row < image->texturerows; row++)
{
k = p;
for (col=0; col < image->texturecols; col++)
{
image->textureimage[m++] = image->image[k].r;
image->textureimage[m++] = image->image[k].g;
image->textureimage[m++] = image->image[k].b;
image->textureimage[m++] = 255;
k++;
}
p = p + image->cols;
}
}
image->nxt = Otk_image_list;
Otk_image_list = image;
image->calllist_num = glGenLists(1);
glDisable( GL_TEXTURE_2D );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glGenTextures( 1, &(image->texturename) );
glBindTexture( GL_TEXTURE_2D, image->texturename );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, image->texturerows, image->texturecols, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->textureimage );
glNewList( image->calllist_num, GL_COMPILE );
glBindTexture( GL_TEXTURE_2D, image->texturename );
glEndList();
free(buffer);
#else
image = 0; /* Unsupported. */
#endif
return image;
}
#endif /*SCZ_DEFS*/
struct Otk_image *Otk_Make_Image_From_Matrix( char *name, int nrows, int ncols, struct Otk_image_rec *newimage )
{
struct Otk_image *image;
#if (PLATFORM_KIND != MsVisC_Platform)
int row, col, maximgsz, mm;
image = Otk_image_list; /* Check to see if this image file was already read. */
while ((image!=0) && (strcmp(image->filename,name)!=0)) image = image->nxt;
/* Get the data. */
if (image==0)
{
image = (struct Otk_image *)malloc( sizeof(struct Otk_image) );
image->filename = (char *)strdup(name);
image->nxt = Otk_image_list;