aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/OpenGL_misc.c
blob: 5f9d502012eb53b94c08d98eecec2fd624cc2a18 (plain)
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

#include "OpenGL_misc.h"
#include "gl4java_GLContext.h"

#ifdef _X11_
	#include <GL/glx.h>
	#include <dlfcn.h>
#endif

#ifdef macintosh
	#include <agl.h>
	#include <string.h>
	#include <CodeFragments.h>
	#include <Errors.h>
	#include <TextUtils.h>
	#include <StringCompare.h>
	#include "mac_console.h"
	
	#define fragNoErr 0
#endif

static const char * _lib_version_=  __LIB_VERSION__ ;

#ifdef macintosh
  static const char * _lib_vendor_ = "Jausoft - Sven Goethel Software Development & Gerard Ziemski"; 
#else
  static const char * _lib_vendor_ = "Jausoft - Sven Goethel Software Development"; 
#endif

#ifndef CALLBACK
	#define CALLBACK
#endif

jboolean LIBAPIENTRY testJavaGLTypes(jboolean verbose)
{
    jboolean ret=JNI_TRUE;
    jint i1 = (jint)0xaaff;
    jint i2 = 0;
    jlong l1 = (jlong)i1;
    i2 = (jint)l1;

    if(i1!=i2)
    {
        fprintf(stderr,"(jint)((jlong)0xaaff) != (jint)0xaaff\n");
	ret = JNI_FALSE;
    }

    /* NON DEPENDENCE CHECKS */

    /* NOW LET's CHECK THE TAKEN JNI-TYPE <-> GL-TYPE MAPPING */

    if( sizeof(GLUtriangulatorObj *) > sizeof(jlong) )
    {
        fprintf(stderr,"(GLUtriangulatorObj *) > (jlong)\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLUnurbsObj *) > sizeof(jlong) )
    {
        fprintf(stderr,"(GLUnurbsObj *) > (jlong)\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLUquadricObj *) > sizeof(jlong) )
    {
        fprintf(stderr,"(GLUquadricObj *) > (jlong)\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLvoid *) > sizeof(jbyte *) )
    {
        fprintf(stderr,"(GLvoid *) > (jbyte *)\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLbyte) != sizeof(jbyte) )
    {
        fprintf(stderr,"GLbyte != jbyte\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLshort) != sizeof(jshort) )
    {
        fprintf(stderr,"GLshort != jshort\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLint) != sizeof(jint) )
    {
        fprintf(stderr,"GLint != jint\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLsizei) != sizeof(jint) )
    {
        fprintf(stderr,"GLsizei != jint\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLfloat) != sizeof(jfloat) )
    {
        fprintf(stderr,"GLfloat != jfloat\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLclampf) != sizeof(jfloat) )
    {
        fprintf(stderr,"GLclampf != jfloat\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLdouble) != sizeof(jdouble) )
    {
        fprintf(stderr,"GLdouble != jdouble\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLclampd) != sizeof(jdouble) )
    {
        fprintf(stderr,"GLclampd != jdouble\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLubyte) != sizeof(jbyte) )
    {
        fprintf(stderr,"GLubyte != jboolean\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLboolean) != sizeof(jboolean) )
    {
        fprintf(stderr,"GLboolean != jboolean\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLushort) != sizeof(jshort) )
    {
        fprintf(stderr,"GLushort != jshort\n");
	ret = JNI_FALSE;
    }

    /* NOT DEF'ed IN JNI
     *
     * GLuint, GLenum : 32-bit unsigned integer
     * GLbitfield     :       dito.
     */

    if( sizeof(void *) > sizeof(PointerHolder) )
    {
        fprintf(stderr,"(void *) > (PointerHolder)\n");
	ret = JNI_FALSE;
    }

    if( sizeof(void *) > sizeof(jlong) )
    {
        fprintf(stderr,"(void *) > (jlong)\n");
	ret = JNI_FALSE;
    }

    if( sizeof(GLuint) != sizeof(jint) )
    {
        fprintf(stderr,"GLuint != jint\n");
	ret = JNI_FALSE;
    }
    if( sizeof(GLenum) != sizeof(jint) )
    {
        fprintf(stderr,"GLenum != jint\n");
	ret = JNI_FALSE;
    }
    if( sizeof(GLbitfield) != sizeof(jint) )
    {
        fprintf(stderr,"GLbitfield != jint\n");
	ret = JNI_FALSE;
    }

    if(ret==JNI_FALSE)
    {
	fflush(stderr);
    }

    /* VERBOSE/ERROR DUMP OF GL Types */

    if(JNI_FALSE==verbose && ret==JNI_TRUE) return ret;

    fprintf(stderr,"GLbyte %d\n", sizeof(GLbyte)); 
    fprintf(stderr,"GLshort %d\n", sizeof(GLshort)); 
    fprintf(stderr,"GLint %d\n", sizeof(GLint)); 
    fprintf(stderr,"GLfloat %d\n", sizeof(GLfloat)); 
    fprintf(stderr,"GLdouble %d\n", sizeof(GLdouble)); 
    fprintf(stderr,"GLubyte %d\n", sizeof(GLubyte)); 
    fprintf(stderr,"GLboolean %d\n", sizeof(GLboolean)); 
    fprintf(stderr,"GLushort %d\n", sizeof(GLushort)); 
    fprintf(stderr,"GLuint %d\n", sizeof(GLuint)); 
    fprintf(stderr,"GLenum %d\n", sizeof(GLenum)); 
    fprintf(stderr,"GLbitfield %d\n", sizeof(GLbitfield)); 

    fprintf(stderr,"\njboolean %d\n", sizeof(jboolean)); 
    fprintf(stderr,"jbyte %d\n", sizeof(jbyte)); 
    fprintf(stderr,"jchar %d\n", sizeof(jchar)); 
    fprintf(stderr,"jshort %d\n", sizeof(jshort)); 
    fprintf(stderr,"jint %d\n", sizeof(jint)); 
    fprintf(stderr,"jlong %d\n", sizeof(jlong)); 
    fprintf(stderr,"jfloat %d\n", sizeof(jfloat)); 
    fprintf(stderr,"jdouble %d\n", sizeof(jdouble)); 

    fprintf(stderr,"\nPointerHolder %d\n", sizeof(PointerHolder)); 

    fprintf(stderr, "GL4Java: useJAWT=%d\n\n",
	(int) Java_gl4java_GLContext_useJAWT( 0, 0 ) );

    fflush(stderr);
    return ret;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljCheckGLTypesNative(JNIEnv *env, jobject obj )
{
    return testJavaGLTypes(JNI_TRUE);
}


JNIEXPORT jstring JNICALL
Java_gl4java_GLContext_gljGetNativeLibVersionNative(JNIEnv *env, jobject obj )
{
    return (*env)->NewStringUTF(env, _lib_version_);
}

JNIEXPORT jstring JNICALL
Java_gl4java_GLContext_gljGetNativeLibVendorNative(JNIEnv *env, jobject obj )
{
    return (*env)->NewStringUTF(env, _lib_vendor_);
}

/**
 * Experimental Code, not done yet !
 * This one is to upspeed the Offscreen rendering engine for e.g. Swing !
 */
JNIEXPORT void JNICALL
Java_gl4java_GLContext_gljReadPixelGL2AWT__IIIIIII_3B (
	JNIEnv *env, jobject obj,
	jint xpos,
	jint ypos,
	jint width,
	jint height,
	jint format,
	jint type,
	jint bufferName,
	jbyteArray pixelGLDest)
{
	GLint swapbytes, lsbfirst, rowlength;
	GLint skiprows, skippixels, alignment;
	GLint unswapbytes, unlsbfirst, unrowlength;
	GLint unskiprows, unskippixels, unalignment;

	jbyte *pixelGLDestData = NULL;
	jsize lenPixelGLDest = 0;

	jboolean isCopiedArray = JNI_FALSE;

	if(pixelGLDest==NULL)
		return;

	lenPixelGLDest = (*env)->GetArrayLength(env, pixelGLDest);
	pixelGLDestData = (*env)->GetByteArrayElements(env, pixelGLDest, 
	                                               &isCopiedArray);

	/* Save current modes. */
	glGetIntegerv(GL_PACK_SWAP_BYTES, &swapbytes);
	glGetIntegerv(GL_PACK_LSB_FIRST, &lsbfirst);
	glGetIntegerv(GL_PACK_ROW_LENGTH, &rowlength);
	glGetIntegerv(GL_PACK_SKIP_ROWS, &skiprows);
	glGetIntegerv(GL_PACK_SKIP_PIXELS, &skippixels);
	glGetIntegerv(GL_PACK_ALIGNMENT, &alignment);
	glGetIntegerv(GL_UNPACK_SWAP_BYTES, &unswapbytes);
	glGetIntegerv(GL_UNPACK_LSB_FIRST, &unlsbfirst);
	glGetIntegerv(GL_UNPACK_ROW_LENGTH, &unrowlength);
	glGetIntegerv(GL_UNPACK_SKIP_ROWS, &unskiprows);
	glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &unskippixels);
	glGetIntegerv(GL_UNPACK_ALIGNMENT, &unalignment);

	/* Little endian machines (DEC Alpha, Inten X86, PPC(in LSB mode)...  
	   for example) could benefit from setting 
	   GL_PACK_LSB_FIRST to GL_TRUE
	   instead of GL_FALSE, but this would require changing the
	   generated bitmaps too. */
	glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
	glPixelStorei(GL_PACK_LSB_FIRST, GL_TRUE);
        glPixelStorei(GL_PACK_ROW_LENGTH, width);
	glPixelStorei(GL_PACK_SKIP_ROWS, 0);
	glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_PACK_ALIGNMENT, 1);
	glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
	glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
        glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	/* Actually read the pixels. */
        glReadBuffer(bufferName);
        glReadPixels(xpos, ypos, width, height, format, type, 
	             pixelGLDestData);

	/* Restore saved modes. */
	glPixelStorei(GL_PACK_SWAP_BYTES, swapbytes);
	glPixelStorei(GL_PACK_LSB_FIRST, lsbfirst);
	glPixelStorei(GL_PACK_ROW_LENGTH, rowlength);
	glPixelStorei(GL_PACK_SKIP_ROWS, skiprows);
	glPixelStorei(GL_PACK_SKIP_PIXELS, skippixels);
	glPixelStorei(GL_PACK_ALIGNMENT, alignment);
	glPixelStorei(GL_UNPACK_SWAP_BYTES, unswapbytes);
	glPixelStorei(GL_UNPACK_LSB_FIRST, unlsbfirst);
	glPixelStorei(GL_UNPACK_ROW_LENGTH, unrowlength);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, unskiprows);
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, unskippixels);
	glPixelStorei(GL_UNPACK_ALIGNMENT, unalignment);

	glFlush();
	glFinish();

	(*env)->ReleaseByteArrayElements(env,  
	                                 pixelGLDest, pixelGLDestData, 
					 (isCopiedArray==JNI_TRUE)?0:JNI_ABORT);
}

/**
 * This one is to upspeed the Offscreen rendering engine for e.g. Swing !
 */
JNIEXPORT void JNICALL
Java_gl4java_GLContext_gljReadPixelGL2AWT__IIIIIII_3B_3I (
	JNIEnv *env, jobject obj,
	jint xpos,
	jint ypos,
	jint width,
	jint height,
	jint format,
	jint type,
	jint bufferName,
	jbyteArray pixelGL,
	jintArray pixelDest)
{
	GLint swapbytes, lsbfirst, rowlength;
	GLint skiprows, skippixels, alignment;
	GLint unswapbytes, unlsbfirst, unrowlength;
	GLint unskiprows, unskippixels, unalignment;

	jsize lenPixelSource = 0;
	jbyte *pixelGLData = NULL;
	jsize lenPixelDest = 0;
	jint *pixelDestData = NULL;
        int alpha=0;
        int red=0;
        int green=0;
        int blue=0;
        int offset=0;
        int y_desc;
	int x;

	jboolean isCopiedArray0 = JNI_FALSE;
	jboolean isCopiedArray1 = JNI_FALSE;

	if(pixelGL==NULL || pixelDest==NULL)
		return;

	lenPixelSource = (*env)->GetArrayLength(env, pixelGL);
	pixelGLData = (*env)->GetByteArrayElements(env, pixelGL, 
	                                           &isCopiedArray0);
	lenPixelDest = (*env)->GetArrayLength(env, pixelDest);
	pixelDestData = (*env)->GetIntArrayElements(env, pixelDest, 
	                                            &isCopiedArray1);

	/* Save current modes. */
	glGetIntegerv(GL_PACK_SWAP_BYTES, &swapbytes);
	glGetIntegerv(GL_PACK_LSB_FIRST, &lsbfirst);
	glGetIntegerv(GL_PACK_ROW_LENGTH, &rowlength);
	glGetIntegerv(GL_PACK_SKIP_ROWS, &skiprows);
	glGetIntegerv(GL_PACK_SKIP_PIXELS, &skippixels);
	glGetIntegerv(GL_PACK_ALIGNMENT, &alignment);
	glGetIntegerv(GL_UNPACK_SWAP_BYTES, &unswapbytes);
	glGetIntegerv(GL_UNPACK_LSB_FIRST, &unlsbfirst);
	glGetIntegerv(GL_UNPACK_ROW_LENGTH, &unrowlength);
	glGetIntegerv(GL_UNPACK_SKIP_ROWS, &unskiprows);
	glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &unskippixels);
	glGetIntegerv(GL_UNPACK_ALIGNMENT, &unalignment);

	/* Little endian machines (DEC Alpha, Inten X86, PPC(in LSB mode)...  
	   for example) could benefit from setting 
	   GL_PACK_LSB_FIRST to GL_TRUE
	   instead of GL_FALSE, but this would require changing the
	   generated bitmaps too. */
	glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
	glPixelStorei(GL_PACK_LSB_FIRST, GL_TRUE);
        glPixelStorei(GL_PACK_ROW_LENGTH, width);
	glPixelStorei(GL_PACK_SKIP_ROWS, 0);
	glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_PACK_ALIGNMENT, 1);
	glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
	glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
        glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	/* Actually read the pixels. */
        glReadBuffer(bufferName);
        glReadPixels(xpos, ypos, width, height, format, type, pixelGLData);

	/* Restore saved modes. */
	glPixelStorei(GL_PACK_SWAP_BYTES, swapbytes);
	glPixelStorei(GL_PACK_LSB_FIRST, lsbfirst);
	glPixelStorei(GL_PACK_ROW_LENGTH, rowlength);
	glPixelStorei(GL_PACK_SKIP_ROWS, skiprows);
	glPixelStorei(GL_PACK_SKIP_PIXELS, skippixels);
	glPixelStorei(GL_PACK_ALIGNMENT, alignment);
	glPixelStorei(GL_UNPACK_SWAP_BYTES, unswapbytes);
	glPixelStorei(GL_UNPACK_LSB_FIRST, unlsbfirst);
	glPixelStorei(GL_UNPACK_ROW_LENGTH, unrowlength);
	glPixelStorei(GL_UNPACK_SKIP_ROWS, unskiprows);
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, unskippixels);
	glPixelStorei(GL_UNPACK_ALIGNMENT, unalignment);

        for(y_desc=height-1; y_desc>=0; y_desc--)
        {
              for(x=0;x<width;x++)
              {
                red   = ( (int)(pixelGLData[offset++]) & 0xff ) << 16;
                green = ( (int)(pixelGLData[offset++]) & 0xff ) <<  8;
                blue  =   (int)(pixelGLData[offset++]) & 0xff;

                if(format==GL_RGBA)
                    alpha = ( (int)(pixelGLData[offset++]) & 0xff ) << 24;
                else
                    alpha = ( (int) 0xff ) << 24;

                pixelDestData[y_desc*width + x] = alpha | red | green | blue;
              }
        }     

	(*env)->ReleaseByteArrayElements(env,  pixelGL, pixelGLData, 
	                               (isCopiedArray0==JNI_TRUE)?0:JNI_ABORT);

	(*env)->ReleaseIntArrayElements(env,  pixelDest, pixelDestData, 
	                               (isCopiedArray1==JNI_TRUE)?0:JNI_ABORT);
}

/**
 * Experimental Code, not done yet !
 * This one is to upspeed the Offscreen rendering engine for e.g. Swing !
 */
JNIEXPORT void JNICALL 
Java_gl4java_GLContext_gljCpyOffScrnImg2Buffer__III_3B
  (JNIEnv *env, jobject obj, jint width, jint height, jint format, jbyteArray pixelDest)
{
}

/**
 * Experimental Code, not done yet !
 * This one is to upspeed the Offscreen rendering engine for e.g. Swing !
 */
JNIEXPORT void JNICALL 
Java_gl4java_GLContext_gljCpyOffScrnImg2Buffer__III_3I
  (JNIEnv *env, jobject obj, jint width, jint height, jint format, jintArray pixelDest)
{
#ifdef _X11_

        jclass cls = 0;
        jfieldID fpixmapHandle=0;

	jsize lenPixelDest = 0;
	jint *pixelDestData = NULL;
	jboolean isCopiedArray = JNI_FALSE;
	jboolean ret = JNI_TRUE;
	int sizemax=0;
	int sizepix=0;


        jfieldID fdisplayHandle=0;
        Display *disp=0;
	Pixmap pix=0;
        XImage *image;

        cls = (*env)->GetObjectClass(env, obj);

	fdisplayHandle = (*env)->GetFieldID(env, cls, "displayHandle", "I");
	if (fdisplayHandle == 0) ret= JNI_FALSE;
	else disp=(Display *)(*env)->GetIntField(env, obj, fdisplayHandle);

	fpixmapHandle = (*env)->GetFieldID(env, cls, "pixmapHandle", "I");
	if (fpixmapHandle == 0) ret= JNI_FALSE;
	else pix = (Pixmap)(*env)->GetIntField(env, obj, fpixmapHandle);

	if(pix==0 || pixelDest==NULL)
		return;

	lenPixelDest = (*env)->GetArrayLength(env, pixelDest);
	pixelDestData = (*env)->GetIntArrayElements(env, pixelDest, 
	                                            &isCopiedArray);

	if(format==GL_RGBA) {
          image = XGetImage(disp, pix, 0, 0, width, height, 
	                    0xffffffff, ZPixmap);
          printf("format RGBA!!\n");
	} else if(format==GL_RGB) {
          image = XGetImage(disp, pix, 0, 0, width, height, 
	                    0x00ffffff, ZPixmap);
          printf("format RGB!!\n");
	} else {
          image = XGetImage(disp, pix, 0, 0, width, height, 
	                    0xffffffff, ZPixmap);
          printf("format not supported - guessing RGBA!!\n");
	}

	if(image!=NULL)
	{
	    printf("width %d height %d\n", (int)width, (int)height);
	    printf("depth %d, bytes_per_line %d, width %d, height %d\n",
	       image->depth, image->bytes_per_line, image->width, image->height);
	    sizemax=(int)(lenPixelDest*sizeof(jint));
	    sizepix=image->bytes_per_line*image->height;

	    printf("sizeof dest-array: %d bytes\n", sizemax);
	    printf("sizeof image bpl*height: %d bytes\n", sizepix); 

	    if(sizepix<sizemax) sizemax=sizepix;

	    (*env)->SetIntArrayRegion(env,  pixelDest, 0, sizemax/sizeof(jint),
				      (jint*)image->data);
	    printf("copied ..\n");
	} else {
	    printf("invalid image ...\n");
	}

	if(pixelDestData!=NULL)
	    (*env)->ReleaseIntArrayElements(env,  pixelDest, pixelDestData, 
	                                    JNI_ABORT);
#endif
}

#ifdef macintosh

#ifndef NDEBUG

static void PrintSymbolNamesByConnection (CFragConnectionID myConnID)
{
       long           myIndex;
       long           myCount;       /*number of exported symbols in fragment*/
       OSErr          myErr;
       Str255         myName;        /*symbol name*/
       Ptr            myAddr;        /*symbol address*/
       CFragSymbolClass       myClass;       /*symbol class*/
       static char buffer[256];

       myErr = CountSymbols(myConnID, &myCount);
       if (!myErr)
          for (myIndex = 1; myIndex <= myCount; myIndex++)
             {
                myErr = GetIndSymbol(myConnID, myIndex, myName, 
                                        &myAddr, &myClass);
                if (!myErr)
		{
			p2cstrcpy (buffer, myName);
								 
            printf("%d/%d: class %d - name %s\n", 
		   		myIndex, myCount, myClass, buffer);
		}
             }
}

static Ptr SeekSymbolNamesByConnection (CFragConnectionID myConnID, Str255 name)
{
       long           myIndex;
       long           myCount;       /*number of exported symbols in fragment*/
       OSErr          myErr;
       Str255         myName;        /*symbol name*/
       Ptr            myAddr;        /*symbol address*/
       CFragSymbolClass       myClass;       /*symbol class*/

       myErr = CountSymbols(myConnID, &myCount);
       if (!myErr)
          for (myIndex = 1; myIndex <= myCount; myIndex++)
             {
                myErr = GetIndSymbol(myConnID, myIndex, myName, 
                                        &myAddr, &myClass);
                if (!myErr && EqualString (myName, name, true, true) == true )
			return myAddr;
             }
}

#endif /* ifndef NDEBUG */

#endif /* ifdef macintosh */

void * LIBAPIENTRY getGLProcAddressHelper(const char * func, int * method,
					  int debug, int verbose )
{
	void * funcPtr=NULL;
	int lmethod;

#ifdef _X11_

	void * libHandleGL;

	/*
	 * void (*glXGetProcAddressARB(const GLubyte *procName))
	 */
	static void * (CALLBACK *__glXGetProcAddress)
					(const GLubyte *procName) = NULL;
        static int                  __firstAccess                 = 1;

        if(__glXGetProcAddress==NULL && __firstAccess)
	{
	    libHandleGL = dlopen("libGL.so", RTLD_LAZY|RTLD_GLOBAL);
	    if(libHandleGL==NULL)
	    {
		printf("cannot access OpenGL library libGL.so\n");
		fflush(NULL);
	    } else {
		__glXGetProcAddress = 
				    dlsym(libHandleGL, "glXGetProcAddressARB");

		if(__glXGetProcAddress!=NULL && verbose) {
		        printf("found glXGetProcAddressARB in libGL.so\n");
		        fflush(NULL);
		}

		if(__glXGetProcAddress==NULL) 
		{
		    __glXGetProcAddress = 
				    dlsym(libHandleGL, "glXGetProcAddressEXT");

		    if(__glXGetProcAddress!=NULL && verbose) {
		        printf("found glXGetProcAddressEXT in libGL.so\n");
		        fflush(NULL);
		    }
		}

		if(__glXGetProcAddress==NULL) 
		{
		    __glXGetProcAddress = 
				    dlsym(libHandleGL, "glXGetProcAddress");

		    if(__glXGetProcAddress!=NULL && verbose) {
		        printf("found glXGetProcAddress in libGL.so\n");
		        fflush(NULL);
		    }
		}

		dlclose(libHandleGL);
		libHandleGL=NULL;

		if(__glXGetProcAddress==NULL)
		{
		    printf("cannot find glXGetProcAddress* in OpenGL library libGL.so\n");
		    fflush(NULL);
		    libHandleGL = dlopen("libglx.so", RTLD_LAZY|RTLD_GLOBAL);
		    if(libHandleGL==NULL)
		    {
			printf("cannot access GLX library libglx.so\n");
			fflush(NULL);
		    } else {
			__glXGetProcAddress = 
				    dlsym(libHandleGL, "glXGetProcAddressARB");

		        if(__glXGetProcAddress!=NULL && verbose) {
		            printf("found glXGetProcAddressARB in libglx.so\n");
		            fflush(NULL);
		        }

			if(__glXGetProcAddress==NULL) {
			    __glXGetProcAddress = 
				    dlsym(libHandleGL, "glXGetProcAddressEXT");

		            if(__glXGetProcAddress!=NULL && verbose) {
		                printf("found glXGetProcAddressEXT in libglx.so\n");
		                fflush(NULL);
		            }
			}

			if(__glXGetProcAddress==NULL) {
			    __glXGetProcAddress = 
				    dlsym(libHandleGL, "glXGetProcAddress");

		            if(__glXGetProcAddress!=NULL && verbose) {
		                printf("found glXGetProcAddress in libglx.so\n");
		                fflush(NULL);
		            }
			}

			dlclose(libHandleGL);
			libHandleGL=NULL;
		    }
		}
	    }
	}
        if(__glXGetProcAddress==NULL && __firstAccess)
	{
	    printf("cannot find glXGetProcAddress* in GLX library libglx.so\n");
	    fflush(NULL);
	}
        __firstAccess = 0;

        if(__glXGetProcAddress!=NULL)
		funcPtr = __glXGetProcAddress(func);

	if(funcPtr==NULL) {
		lmethod=2;
		libHandleGL = dlopen("libGL.so", RTLD_LAZY|RTLD_GLOBAL);
		if(libHandleGL==NULL)
		{
			printf("cannot access OpenGL library libGL.so\n");
			fflush(NULL);
		} else {
			funcPtr = dlsym(libHandleGL, func);
			dlclose(libHandleGL);
		}
	} else lmethod=1;
#endif


#ifdef _WIN32_

	HMODULE hDLL_OPENGL32 = 0;



	funcPtr = wglGetProcAddress(func);
	if(funcPtr==NULL)
	{
		lmethod=2;


		if(hDLL_OPENGL32==NULL)

		{
			hDLL_OPENGL32 = LoadLibrary ("OPENGL32.DLL");

			/* hDLL_OPENGL32 = GetModuleHandle ("OPENGL32.DLL"); */

		}
		if(hDLL_OPENGL32==NULL)
		{
			printf("cannot access OpenGL library OPENGL32.DLL\n");
			fflush(NULL);
		} else {
			funcPtr = GetProcAddress (hDLL_OPENGL32, func);
			FreeLibrary(hDLL_OPENGL32);
		}
	} else lmethod=1;
#endif

#ifdef macintosh
   	Str255 errName;
	Str255 funcName;
	Ptr glLibMainAddr = 0;
	CFragConnectionID glLibConnectId = 0;
	CFragSymbolClass glLibSymClass = 0;
	OSErr returnError=fragNoErr;
	#ifndef NDEBUG
        static int firstTime = 1;
    #endif
	     
        returnError = 
		GetSharedLibrary("\pOpenGLLibrary", 
			       kPowerPCCFragArch, 
			       kReferenceCFrag,
			       &glLibConnectId, 
			       &glLibMainAddr, 
                   errName);

	if (returnError != fragNoErr)
	{
        	printf ("GetSharedLibrary Err(%d): Ahhh!  Didn't find LIBRARY !\n",
        		returnError);
	} 

	if (returnError == fragNoErr)
	{
		c2pstrcpy ( funcName, func );
		
		#ifndef NDEBUG
		 if(firstTime)
		 {
			PrintSymbolNamesByConnection (glLibConnectId);
			firstTime=0;
		 } 
		 funcPtr = (void *) 
			SeekSymbolNamesByConnection (glLibConnectId, funcName);
		#endif
		
		if(funcPtr==NULL)
		{
		  returnError = 
		    FindSymbol (glLibConnectId, funcName, 
		                &funcPtr, & glLibSymClass );
		    lmethod=2;
		} 
		#ifndef NDEBUG
		 else lmethod=3;
		#endif
		
		if (returnError != fragNoErr)
		{
		  printf ("GetSharedLibrary Err(%d): Ahhh!  Didn't find SYMBOL: %s !\n",
		  	returnError, func);
		} 
		returnError = fragNoErr; // fall back to ok mode ...
	} 

	if (returnError == fragNoErr && glLibConnectId!=NULL)
	{
		returnError = CloseConnection(&glLibConnectId);

		if (returnError != fragNoErr)
		{
		  printf ("GetSharedLibrary Err(%d): Ahhh!  Didn't close LIBRARY !\n",
		  	returnError);
		} 
	} 

	
#endif

	if(funcPtr==NULL)
	{
	  if(debug||verbose)
	  {
		printf("%s (%d): not implemented !\n", func, lmethod);
		fflush(NULL);
	  }
	} else if(verbose) {
		printf("%s (%d): loaded !\n", func, lmethod);
		fflush(NULL);
	} 
	if(method!=NULL) *method=lmethod;
	return funcPtr;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljTestGLProc (
	JNIEnv *env, jobject obj, jstring name, jboolean verbose )
{
     jboolean res;
     int method=0;
     
     const char *str = (*env)->GetStringUTFChars(env, name, 0);

     #ifdef macintosh
     	InstallJavaConsole(env);
     #endif
     
     res = ( getGLProcAddressHelper(str, &method, 0, verbose)!=NULL )?
			JNI_TRUE:JNI_FALSE;

     (*env)->ReleaseStringUTFChars(env, name, str);
     return res;
}


JNIEXPORT jint JNICALL
Java_gl4java_GLContext_gljGetCurrentContext( 
	JNIEnv *env, jobject obj )
{
	#ifdef _WIN32_
                return (jint) wglGetCurrentContext();
        #else
          #ifdef macintosh
                return (jint) aglGetCurrentContext();
          #else
                return (jint) glXGetCurrentContext();   
	  #endif
        #endif
}