1 /* Copyright (C) 2000  Free Software Foundation
2 
3    This file is part of libgcj.
4 
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8 
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 
12 #include <gcj/cni.h>
13 #include <gnu/gcj/RawData.h>
14 
15 #include <java/lang/OutOfMemoryError.h>
16 
17 #include <gnu/gcj/xlib/Display.h>
18 #include <gnu/gcj/xlib/Screen.h>
19 #include <gnu/gcj/xlib/Visual.h>
20 #include <gnu/gcj/xlib/XImage.h>
21 #include <java/lang/System.h>
22 #include <java/io/PrintStream.h>
23 
init(Visual * visual,jint depth,jint format,jint xoffset,jint width,jint height,jint bitmapPad,jint bytesPerLine,jint bitsPerPixel)24 void gnu::gcj::xlib::XImage::init(Visual* visual, jint depth,
25 				  jint format, jint xoffset,
26 				  jint width, jint height,
27 				  jint bitmapPad, jint bytesPerLine,
28 				  jint bitsPerPixel)
29 {
30   ::Display* dpy = (::Display*) visual->display->display;
31   ::Visual* vis = (::Visual*) visual->getVisualStructure();
32 
33   char* data = 0; // no preallocated data
34   ::XImage* ximage = XCreateImage(dpy, vis, depth, format, xoffset,
35 				  data,
36 				  width, height,
37 				  bitmapPad,
38 				  bytesPerLine
39 				  );
40   if (ximage == 0)
41     {
42       jstring errorMessage = JvNewStringLatin1("XCreateImage failed");
43       throw new ::java::lang::OutOfMemoryError(errorMessage);
44     }
45 
46   bool reinitialize = false;
47 
48   if ((bitsPerPixel != 0) && (ximage->bits_per_pixel != bitsPerPixel))
49     {
50       ximage->bits_per_pixel = bitsPerPixel;
51       reinitialize = true;
52     }
53 
54   // FIXME: make autoconf test?
55   jshort endianTestShort[] = { 1 };
56   jbyte* endianTestByte = reinterpret_cast<jbyte*>(endianTestShort);
57 
58   jint byteOrder;
59   if (endianTestByte[0] == 1)
60     {
61       // little endian machine
62       byteOrder = LEAST_SIGNIFICANT_B_FIRST_ORDER;
63     }
64   else
65     {
66       // big endian machine
67       byteOrder = MOST_SIGNIFICANT_B_FIRST_ORDER;
68     }
69   /* NB: This doesn't consider those weird machines out there with
70      middle-endian byte order. */
71 
72   if (byteOrder != ximage->byte_order)
73     {
74       ximage->byte_order = byteOrder;
75       reinitialize = true;
76     }
77 
78   if (reinitialize)
79     XInitImage(ximage);
80 
81   structure = reinterpret_cast<gnu::gcj::RawData*>(ximage);
82   // Notice that no image data has been allocated at this point
83 }
84 
init(Visual * visual,jint width,jint height)85 void gnu::gcj::xlib::XImage::init(Visual* visual,
86 				  jint width,
87 				  jint height)
88 {
89   int depth = visual->getDepth();
90 
91   int format = ZPixmap; // Chunky, not planar.
92   int offset = 0;
93   int bitmapPad = 32; // FIXME, don't hardcode this
94   int bytesPerLine = 0; // Let the server figure it out
95 
96   init(visual, depth, format, offset, width, height, bitmapPad,
97        bytesPerLine, 0);
98 }
99 
internalSetData(jbyteArray data,jint offset)100 void gnu::gcj::xlib::XImage::internalSetData(jbyteArray data, jint offset)
101 {
102   ::XImage* ximage = (::XImage*) structure;
103   ximage->data = reinterpret_cast<char*>(elements(data)+offset);
104 }
105 
internalSetData(jshortArray data,jint offset)106 void gnu::gcj::xlib::XImage::internalSetData(jshortArray data, jint offset)
107 {
108   ::XImage* ximage = (::XImage*) structure;
109   ximage->data = reinterpret_cast<char*>(elements(data)+offset);
110 }
111 
internalSetData(jintArray data,jint offset)112 void gnu::gcj::xlib::XImage::internalSetData(jintArray data, jint offset)
113 {
114   ::XImage* ximage = (::XImage*) structure;
115   ximage->data = reinterpret_cast<char*>(elements(data)+offset);
116 }
117 
finalize()118 void gnu::gcj::xlib::XImage::finalize()
119 {
120   ::XImage* ximage = (::XImage*) structure;
121   if (ownsData)
122     delete ximage->data;
123 
124   ximage->data = 0; // Never allow XLib to free the data allocation.
125   dataRef = 0;
126   XDestroyImage(ximage);
127 }
128 
getWidth()129 jint gnu::gcj::xlib::XImage::getWidth()
130 {
131   ::XImage* ximage = (::XImage*) structure;
132   return ximage->width;
133 }
134 
getHeight()135 jint gnu::gcj::xlib::XImage::getHeight()
136 {
137   ::XImage* ximage = (::XImage*) structure;
138   return ximage->height;
139 }
140 
getDepth()141 jint gnu::gcj::xlib::XImage::getDepth()
142 {
143   ::XImage* ximage = (::XImage*) structure;
144   return ximage->depth;
145 }
146 
getFormat()147 jint gnu::gcj::xlib::XImage::getFormat()
148 {
149   ::XImage* ximage = (::XImage*) structure;
150   return ximage->format;
151 }
152 
getXOffset()153 jint gnu::gcj::xlib::XImage::getXOffset()
154 {
155   ::XImage* ximage = (::XImage*) structure;
156   return ximage->xoffset;
157 }
158 
getImageByteOrder()159 jint gnu::gcj::xlib::XImage::getImageByteOrder()
160 {
161   ::XImage* ximage = (::XImage*) structure;
162   return ximage->byte_order;
163 }
164 
getBitmapBitOrder()165 jint gnu::gcj::xlib::XImage::getBitmapBitOrder()
166 {
167   ::XImage* ximage = (::XImage*) structure;
168   return ximage->bitmap_bit_order;
169 }
170 
getBitmapUnit()171 jint gnu::gcj::xlib::XImage::getBitmapUnit()
172 {
173   ::XImage* ximage = (::XImage*) structure;
174   return ximage->bitmap_unit;
175 }
176 
getBitmapPad()177 jint gnu::gcj::xlib::XImage::getBitmapPad()
178 {
179   ::XImage* ximage = (::XImage*) structure;
180   return ximage->bitmap_pad;
181 }
182 
getBytesPerLine()183 jint gnu::gcj::xlib::XImage::getBytesPerLine()
184 {
185   ::XImage* ximage = (::XImage*) structure;
186   return ximage->bytes_per_line;
187 }
188 
getBitsPerPixel()189 jint gnu::gcj::xlib::XImage::getBitsPerPixel()
190 {
191   ::XImage* ximage = (::XImage*) structure;
192   return ximage->bits_per_pixel;
193 }
194 
195 
196 // True/Direct Color specific:
197 
getRedMask()198 jint gnu::gcj::xlib::XImage::getRedMask()
199 {
200   ::XImage* ximage = (::XImage*) structure;
201   return ximage->red_mask;
202 }
203 
getGreenMask()204 jint gnu::gcj::xlib::XImage::getGreenMask()
205 {
206   ::XImage* ximage = (::XImage*) structure;
207   return ximage->green_mask;
208 }
209 
getBlueMask()210 jint gnu::gcj::xlib::XImage::getBlueMask()
211 {
212   ::XImage* ximage = (::XImage*) structure;
213   return ximage->blue_mask;
214 }
215 
setPixel(jint x,jint y,jint pixel)216 void gnu::gcj::xlib::XImage::setPixel(jint x, jint y, jint pixel)
217 {
218   ::XImage* ximage = (::XImage*) structure;
219   XPutPixel(ximage, x, y, pixel);
220 }
221