2
|
1 |
/*
|
|
2 |
* Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
#include "BufImgSurfaceData.h"
|
|
27 |
#include "malloc.h"
|
|
28 |
|
|
29 |
#include "sun_awt_image_BufImgSurfaceData.h"
|
|
30 |
|
|
31 |
#include "img_util_md.h"
|
|
32 |
#include "jni_util.h"
|
|
33 |
/* Define uintptr_t */
|
|
34 |
#include "gdefs.h"
|
|
35 |
|
|
36 |
/**
|
|
37 |
* This include file contains support code for loops using the
|
|
38 |
* SurfaceData interface to talk to an X11 drawable from native
|
|
39 |
* code.
|
|
40 |
*/
|
|
41 |
|
|
42 |
static LockFunc BufImg_Lock;
|
|
43 |
static GetRasInfoFunc BufImg_GetRasInfo;
|
|
44 |
static ReleaseFunc BufImg_Release;
|
|
45 |
static DisposeFunc BufImg_Dispose;
|
|
46 |
|
|
47 |
static ColorData *BufImg_SetupICM(JNIEnv *env, BufImgSDOps *bisdo);
|
|
48 |
|
|
49 |
static jfieldID rgbID;
|
|
50 |
static jfieldID mapSizeID;
|
|
51 |
static jfieldID CMpDataID;
|
|
52 |
static jfieldID allGrayID;
|
|
53 |
|
|
54 |
/*
|
|
55 |
* Class: sun_awt_image_BufImgSurfaceData
|
|
56 |
* Method: initIDs
|
|
57 |
* Signature: ()V
|
|
58 |
*/
|
|
59 |
JNIEXPORT void JNICALL
|
|
60 |
Java_sun_awt_image_BufImgSurfaceData_initIDs
|
|
61 |
(JNIEnv *env, jclass bisd, jclass icm)
|
|
62 |
{
|
|
63 |
if (sizeof(BufImgRIPrivate) > SD_RASINFO_PRIVATE_SIZE) {
|
|
64 |
JNU_ThrowInternalError(env, "Private RasInfo structure too large!");
|
|
65 |
return;
|
|
66 |
}
|
|
67 |
|
|
68 |
rgbID = (*env)->GetFieldID(env, icm, "rgb", "[I");
|
|
69 |
allGrayID = (*env)->GetFieldID(env, icm, "allgrayopaque", "Z");
|
|
70 |
mapSizeID = (*env)->GetFieldID(env, icm, "map_size", "I");
|
|
71 |
CMpDataID = (*env)->GetFieldID(env, icm, "pData", "J");
|
|
72 |
if (allGrayID == 0 || rgbID == 0 || mapSizeID == 0 || CMpDataID == 0) {
|
|
73 |
JNU_ThrowInternalError(env, "Could not get field IDs");
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
/*
|
|
78 |
* Class: sun_java2d_SurfaceData
|
|
79 |
* Method: freeNativeICMData
|
|
80 |
* Signature: (Ljava/awt/image/IndexColorModel;)V
|
|
81 |
*/
|
|
82 |
JNIEXPORT void JNICALL
|
|
83 |
Java_sun_awt_image_BufImgSurfaceData_freeNativeICMData
|
|
84 |
(JNIEnv *env, jclass sd, jobject icm)
|
|
85 |
{
|
|
86 |
jlong pData;
|
|
87 |
ColorData *cdata;
|
|
88 |
|
|
89 |
if (JNU_IsNull(env, icm)) {
|
|
90 |
JNU_ThrowNullPointerException(env, "IndexColorModel cannot be null");
|
|
91 |
return;
|
|
92 |
}
|
|
93 |
|
|
94 |
pData = (*env)->GetLongField (env, icm, CMpDataID);
|
|
95 |
cdata = (ColorData *)pData;
|
|
96 |
freeICMColorData(cdata);
|
|
97 |
}
|
|
98 |
|
|
99 |
/*
|
|
100 |
* Class: sun_awt_image_BufImgSurfaceData
|
|
101 |
* Method: initOps
|
|
102 |
* Signature: (Ljava/lang/Object;IIIII)V
|
|
103 |
*/
|
|
104 |
JNIEXPORT void JNICALL
|
|
105 |
Java_sun_awt_image_BufImgSurfaceData_initRaster(JNIEnv *env, jobject bisd,
|
|
106 |
jobject array,
|
|
107 |
jint offset, jint bitoffset,
|
|
108 |
jint width, jint height,
|
|
109 |
jint pixStr, jint scanStr,
|
|
110 |
jobject icm)
|
|
111 |
{
|
|
112 |
BufImgSDOps *bisdo =
|
|
113 |
(BufImgSDOps*)SurfaceData_InitOps(env, bisd, sizeof(BufImgSDOps));
|
|
114 |
bisdo->sdOps.Lock = BufImg_Lock;
|
|
115 |
bisdo->sdOps.GetRasInfo = BufImg_GetRasInfo;
|
|
116 |
bisdo->sdOps.Release = BufImg_Release;
|
|
117 |
bisdo->sdOps.Unlock = NULL;
|
|
118 |
bisdo->sdOps.Dispose = BufImg_Dispose;
|
|
119 |
bisdo->array = (*env)->NewWeakGlobalRef(env, array);
|
|
120 |
bisdo->offset = offset;
|
|
121 |
bisdo->bitoffset = bitoffset;
|
|
122 |
bisdo->scanStr = scanStr;
|
|
123 |
bisdo->pixStr = pixStr;
|
|
124 |
if (JNU_IsNull(env, icm)) {
|
|
125 |
bisdo->lutarray = NULL;
|
|
126 |
bisdo->lutsize = 0;
|
|
127 |
bisdo->icm = NULL;
|
|
128 |
} else {
|
|
129 |
jobject lutarray = (*env)->GetObjectField(env, icm, rgbID);
|
|
130 |
bisdo->lutarray = (*env)->NewWeakGlobalRef(env, lutarray);
|
|
131 |
bisdo->lutsize = (*env)->GetIntField(env, icm, mapSizeID);
|
|
132 |
bisdo->icm = (*env)->NewWeakGlobalRef(env, icm);
|
|
133 |
}
|
|
134 |
bisdo->rasbounds.x1 = 0;
|
|
135 |
bisdo->rasbounds.y1 = 0;
|
|
136 |
bisdo->rasbounds.x2 = width;
|
|
137 |
bisdo->rasbounds.y2 = height;
|
|
138 |
}
|
|
139 |
|
|
140 |
/*
|
|
141 |
* Method for disposing native BufImgSD
|
|
142 |
*/
|
|
143 |
static void BufImg_Dispose(JNIEnv *env, SurfaceDataOps *ops)
|
|
144 |
{
|
|
145 |
/* ops is assumed non-null as it is checked in SurfaceData_DisposeOps */
|
|
146 |
BufImgSDOps *bisdo = (BufImgSDOps *)ops;
|
|
147 |
(*env)->DeleteWeakGlobalRef(env, bisdo->array);
|
|
148 |
if (bisdo->lutarray != NULL) {
|
|
149 |
(*env)->DeleteWeakGlobalRef(env, bisdo->lutarray);
|
|
150 |
}
|
|
151 |
if (bisdo->icm != NULL) {
|
|
152 |
(*env)->DeleteWeakGlobalRef(env, bisdo->icm);
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
static jint BufImg_Lock(JNIEnv *env,
|
|
157 |
SurfaceDataOps *ops,
|
|
158 |
SurfaceDataRasInfo *pRasInfo,
|
|
159 |
jint lockflags)
|
|
160 |
{
|
|
161 |
BufImgSDOps *bisdo = (BufImgSDOps *)ops;
|
|
162 |
BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
|
|
163 |
|
|
164 |
if ((lockflags & (SD_LOCK_LUT)) != 0 && JNU_IsNull(env, bisdo->lutarray)) {
|
|
165 |
/* REMIND: Should this be an InvalidPipe exception? */
|
|
166 |
JNU_ThrowNullPointerException(env, "Attempt to lock missing colormap");
|
|
167 |
return SD_FAILURE;
|
|
168 |
}
|
|
169 |
if ((lockflags & SD_LOCK_INVCOLOR) != 0 ||
|
|
170 |
(lockflags & SD_LOCK_INVGRAY) != 0)
|
|
171 |
{
|
|
172 |
bipriv->cData = BufImg_SetupICM(env, bisdo);
|
|
173 |
if (bipriv->cData == NULL) {
|
|
174 |
JNU_ThrowNullPointerException(env, "Could not initialize "
|
|
175 |
"inverse tables");
|
|
176 |
return SD_FAILURE;
|
|
177 |
}
|
|
178 |
} else {
|
|
179 |
bipriv->cData = NULL;
|
|
180 |
}
|
|
181 |
|
|
182 |
bipriv->lockFlags = lockflags;
|
|
183 |
bipriv->base = NULL;
|
|
184 |
bipriv->lutbase = NULL;
|
|
185 |
|
|
186 |
SurfaceData_IntersectBounds(&pRasInfo->bounds, &bisdo->rasbounds);
|
|
187 |
|
|
188 |
return SD_SUCCESS;
|
|
189 |
}
|
|
190 |
|
|
191 |
static void BufImg_GetRasInfo(JNIEnv *env,
|
|
192 |
SurfaceDataOps *ops,
|
|
193 |
SurfaceDataRasInfo *pRasInfo)
|
|
194 |
{
|
|
195 |
BufImgSDOps *bisdo = (BufImgSDOps *)ops;
|
|
196 |
BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
|
|
197 |
|
|
198 |
if ((bipriv->lockFlags & (SD_LOCK_RD_WR)) != 0) {
|
|
199 |
bipriv->base =
|
|
200 |
(*env)->GetPrimitiveArrayCritical(env, bisdo->array, NULL);
|
|
201 |
}
|
|
202 |
if ((bipriv->lockFlags & (SD_LOCK_LUT)) != 0) {
|
|
203 |
bipriv->lutbase =
|
|
204 |
(*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL);
|
|
205 |
}
|
|
206 |
|
|
207 |
if (bipriv->base == NULL) {
|
|
208 |
pRasInfo->rasBase = NULL;
|
|
209 |
pRasInfo->pixelStride = 0;
|
|
210 |
pRasInfo->pixelBitOffset = 0;
|
|
211 |
pRasInfo->scanStride = 0;
|
|
212 |
} else {
|
|
213 |
pRasInfo->rasBase = (void *)
|
|
214 |
(((uintptr_t) bipriv->base) + bisdo->offset);
|
|
215 |
pRasInfo->pixelStride = bisdo->pixStr;
|
|
216 |
pRasInfo->pixelBitOffset = bisdo->bitoffset;
|
|
217 |
pRasInfo->scanStride = bisdo->scanStr;
|
|
218 |
}
|
|
219 |
if (bipriv->lutbase == NULL) {
|
|
220 |
pRasInfo->lutBase = NULL;
|
|
221 |
pRasInfo->lutSize = 0;
|
|
222 |
} else {
|
|
223 |
pRasInfo->lutBase = bipriv->lutbase;
|
|
224 |
pRasInfo->lutSize = bisdo->lutsize;
|
|
225 |
}
|
|
226 |
if (bipriv->cData == NULL) {
|
|
227 |
pRasInfo->invColorTable = NULL;
|
|
228 |
pRasInfo->redErrTable = NULL;
|
|
229 |
pRasInfo->grnErrTable = NULL;
|
|
230 |
pRasInfo->bluErrTable = NULL;
|
|
231 |
} else {
|
|
232 |
pRasInfo->invColorTable = bipriv->cData->img_clr_tbl;
|
|
233 |
pRasInfo->redErrTable = bipriv->cData->img_oda_red;
|
|
234 |
pRasInfo->grnErrTable = bipriv->cData->img_oda_green;
|
|
235 |
pRasInfo->bluErrTable = bipriv->cData->img_oda_blue;
|
|
236 |
pRasInfo->invGrayTable = bipriv->cData->pGrayInverseLutData;
|
|
237 |
}
|
|
238 |
}
|
|
239 |
|
|
240 |
static void BufImg_Release(JNIEnv *env,
|
|
241 |
SurfaceDataOps *ops,
|
|
242 |
SurfaceDataRasInfo *pRasInfo)
|
|
243 |
{
|
|
244 |
BufImgSDOps *bisdo = (BufImgSDOps *)ops;
|
|
245 |
BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
|
|
246 |
|
|
247 |
if (bipriv->base != NULL) {
|
|
248 |
jint mode = (((bipriv->lockFlags & (SD_LOCK_WRITE)) != 0)
|
|
249 |
? 0 : JNI_ABORT);
|
|
250 |
(*env)->ReleasePrimitiveArrayCritical(env, bisdo->array,
|
|
251 |
bipriv->base, mode);
|
|
252 |
}
|
|
253 |
if (bipriv->lutbase != NULL) {
|
|
254 |
(*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray,
|
|
255 |
bipriv->lutbase, JNI_ABORT);
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
static ColorData *BufImg_SetupICM(JNIEnv *env,
|
|
260 |
BufImgSDOps *bisdo)
|
|
261 |
{
|
|
262 |
ColorData *cData;
|
|
263 |
|
|
264 |
if (JNU_IsNull(env, bisdo->icm)) {
|
|
265 |
return (ColorData *) NULL;
|
|
266 |
}
|
|
267 |
|
|
268 |
cData = (ColorData *) JNU_GetLongFieldAsPtr(env, bisdo->icm, CMpDataID);
|
|
269 |
|
|
270 |
if (cData == NULL) {
|
|
271 |
cData = (ColorData*)calloc(1, sizeof(ColorData));
|
|
272 |
|
|
273 |
if (cData != NULL) {
|
|
274 |
jboolean allGray
|
|
275 |
= (*env)->GetBooleanField(env, bisdo->icm, allGrayID);
|
|
276 |
int *pRgb = (int *)
|
|
277 |
((*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL));
|
|
278 |
cData->img_clr_tbl = initCubemap(pRgb, bisdo->lutsize, 32);
|
|
279 |
if (allGray == JNI_TRUE) {
|
|
280 |
initInverseGrayLut(pRgb, bisdo->lutsize, cData);
|
|
281 |
}
|
|
282 |
(*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb,
|
|
283 |
JNI_ABORT);
|
|
284 |
|
|
285 |
initDitherTables(cData);
|
|
286 |
|
|
287 |
JNU_SetLongFieldFromPtr(env, bisdo->icm, CMpDataID, cData);
|
|
288 |
}
|
|
289 |
}
|
|
290 |
|
|
291 |
return cData;
|
|
292 |
}
|