summaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/xlib/natXImage.cc
blob: 81022227d2bea9893bec2cdf3099caaa6bb1d34a (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
/* Copyright (C) 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <gcj/cni.h>
#include <gnu/gcj/RawData.h>

#include <java/lang/OutOfMemoryError.h>

#include <gnu/gcj/xlib/Display.h>
#include <gnu/gcj/xlib/Screen.h>
#include <gnu/gcj/xlib/Visual.h>
#include <gnu/gcj/xlib/XImage.h>
#include <java/lang/System.h>
#include <java/io/PrintStream.h>

void gnu::gcj::xlib::XImage::init(Visual* visual, jint depth,
				  jint format, jint xoffset,
				  jint width, jint height,
				  jint bitmapPad, jint bytesPerLine,
				  jint bitsPerPixel)
{
  ::Display* dpy = (::Display*) visual->display->display;
  ::Visual* vis = (::Visual*) visual->getVisualStructure();

  char* data = 0; // no preallocated data
  ::XImage* ximage = XCreateImage(dpy, vis, depth, format, xoffset,
				  data,
				  width, height, 
				  bitmapPad,
				  bytesPerLine
				  );
  if (ximage == 0)
    {
      jstring errorMessage = JvNewStringLatin1("XCreateImage failed");
      throw new ::java::lang::OutOfMemoryError(errorMessage);
    }
    
  bool reinitialize = false;
    
  if ((bitsPerPixel != 0) && (ximage->bits_per_pixel != bitsPerPixel))
    {
      ximage->bits_per_pixel = bitsPerPixel;
      reinitialize = true;
    }
    
  // FIXME: make autoconf test?
  jshort endianTestShort[] = { 1 };
  jbyte* endianTestByte = reinterpret_cast<jbyte*>(endianTestShort);
    
  jint byteOrder;
  if (endianTestByte[0] == 1)
    {
      // little endian machine
      byteOrder = LEAST_SIGNIFICANT_B_FIRST_ORDER;
    }
  else
    {
      // big endian machine
      byteOrder = MOST_SIGNIFICANT_B_FIRST_ORDER;
    }
  /* NB: This doesn't consider those weird machines out there with
     middle-endian byte order. */
    
  if (byteOrder != ximage->byte_order)
    {
      ximage->byte_order = byteOrder;
      reinitialize = true;
    }
    
  if (reinitialize)
    XInitImage(ximage);
    
  structure = reinterpret_cast<gnu::gcj::RawData*>(ximage);
  // Notice that no image data has been allocated at this point
}

void gnu::gcj::xlib::XImage::init(Visual* visual, 
				  jint width,
				  jint height)
{
  int depth = visual->getDepth();
    
  int format = ZPixmap; // Chunky, not planar.
  int offset = 0;
  int bitmapPad = 32; // FIXME, don't hardcode this
  int bytesPerLine = 0; // Let the server figure it out

  init(visual, depth, format, offset, width, height, bitmapPad,
       bytesPerLine, 0);
}

void gnu::gcj::xlib::XImage::internalSetData(jbyteArray data, jint offset)
{
  ::XImage* ximage = (::XImage*) structure;
  ximage->data = reinterpret_cast<char*>(elements(data)+offset);
}

void gnu::gcj::xlib::XImage::internalSetData(jshortArray data, jint offset)
{
  ::XImage* ximage = (::XImage*) structure;
  ximage->data = reinterpret_cast<char*>(elements(data)+offset);
}

void gnu::gcj::xlib::XImage::internalSetData(jintArray data, jint offset)
{
  ::XImage* ximage = (::XImage*) structure;
  ximage->data = reinterpret_cast<char*>(elements(data)+offset);
}

void gnu::gcj::xlib::XImage::finalize()
{
  ::XImage* ximage = (::XImage*) structure;
  if (ownsData)
    delete ximage->data;
  
  ximage->data = 0; // Never allow XLib to free the data allocation.
  dataRef = 0;
  XDestroyImage(ximage);
}

jint gnu::gcj::xlib::XImage::getWidth()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->width;
}

jint gnu::gcj::xlib::XImage::getHeight()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->height;
}

jint gnu::gcj::xlib::XImage::getDepth()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->depth;
}

jint gnu::gcj::xlib::XImage::getFormat()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->format;
}

jint gnu::gcj::xlib::XImage::getXOffset()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->xoffset;
}

jint gnu::gcj::xlib::XImage::getImageByteOrder()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->byte_order;
}

jint gnu::gcj::xlib::XImage::getBitmapBitOrder()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->bitmap_bit_order;
}

jint gnu::gcj::xlib::XImage::getBitmapUnit()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->bitmap_unit;
}

jint gnu::gcj::xlib::XImage::getBitmapPad()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->bitmap_pad;
}

jint gnu::gcj::xlib::XImage::getBytesPerLine()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->bytes_per_line;
}

jint gnu::gcj::xlib::XImage::getBitsPerPixel()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->bits_per_pixel;
}


// True/Direct Color specific:

jint gnu::gcj::xlib::XImage::getRedMask()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->red_mask;
}

jint gnu::gcj::xlib::XImage::getGreenMask()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->green_mask;
}

jint gnu::gcj::xlib::XImage::getBlueMask()
{
  ::XImage* ximage = (::XImage*) structure;
  return ximage->blue_mask;
}

void gnu::gcj::xlib::XImage::setPixel(jint x, jint y, jint pixel)
{
  ::XImage* ximage = (::XImage*) structure;
  XPutPixel(ximage, x, y, pixel);
}