214 pRasInfo->scanStride = 0; 215 } else { 216 pRasInfo->rasBase = (void *) 217 (((uintptr_t) bipriv->base) + bisdo->offset); 218 pRasInfo->pixelStride = bisdo->pixStr; 219 pRasInfo->pixelBitOffset = bisdo->bitoffset; 220 pRasInfo->scanStride = bisdo->scanStr; 221 } 222 if (bipriv->lutbase == NULL) { 223 pRasInfo->lutBase = NULL; 224 pRasInfo->lutSize = 0; 225 } else { 226 pRasInfo->lutBase = bipriv->lutbase; 227 pRasInfo->lutSize = bisdo->lutsize; 228 } 229 if (bipriv->cData == NULL) { 230 pRasInfo->invColorTable = NULL; 231 pRasInfo->redErrTable = NULL; 232 pRasInfo->grnErrTable = NULL; 233 pRasInfo->bluErrTable = NULL; 234 } else { 235 pRasInfo->invColorTable = bipriv->cData->img_clr_tbl; 236 pRasInfo->redErrTable = bipriv->cData->img_oda_red; 237 pRasInfo->grnErrTable = bipriv->cData->img_oda_green; 238 pRasInfo->bluErrTable = bipriv->cData->img_oda_blue; 239 pRasInfo->invGrayTable = bipriv->cData->pGrayInverseLutData; 240 } 241 } 242 243 static void BufImg_Release(JNIEnv *env, 244 SurfaceDataOps *ops, 245 SurfaceDataRasInfo *pRasInfo) 246 { 247 BufImgSDOps *bisdo = (BufImgSDOps *)ops; 248 BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv); 249 250 if (bipriv->base != NULL) { 251 jint mode = (((bipriv->lockFlags & (SD_LOCK_WRITE)) != 0) 252 ? 0 : JNI_ABORT); 253 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->array, 254 bipriv->base, mode); 255 } 256 if (bipriv->lutbase != NULL) { 257 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, 258 bipriv->lutbase, JNI_ABORT); 259 } 260 } 261 262 static ColorData *BufImg_SetupICM(JNIEnv *env, 263 BufImgSDOps *bisdo) 264 { 265 ColorData *cData = NULL; 266 jobject colorData; 267 268 if (JNU_IsNull(env, bisdo->icm)) { 269 return (ColorData *) NULL; 270 } 271 272 colorData = (*env)->GetObjectField(env, bisdo->icm, colorDataID); 273 274 if (JNU_IsNull(env, colorData)) { 275 if (JNU_IsNull(env, clsICMCD)) { 276 // we are unable to create a wrapper object 277 return (ColorData*)NULL; 278 } 279 } else { 280 cData = (ColorData*)JNU_GetLongFieldAsPtr(env, colorData, pDataID); 281 } 282 283 if (cData != NULL) { 284 return cData; 285 } 286 287 cData = (ColorData*)calloc(1, sizeof(ColorData)); 288 289 if (cData != NULL) { 290 jboolean allGray 291 = (*env)->GetBooleanField(env, bisdo->icm, allGrayID); 292 int *pRgb = (int *) 293 ((*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL)); 294 295 if (pRgb == NULL) { 296 free(cData); 297 return (ColorData*)NULL; 298 } 299 300 cData->img_clr_tbl = initCubemap(pRgb, bisdo->lutsize, 32); 301 if (allGray == JNI_TRUE) { 302 initInverseGrayLut(pRgb, bisdo->lutsize, cData); 303 } 304 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb, 305 JNI_ABORT); 306 307 initDitherTables(cData); 308 309 if (JNU_IsNull(env, colorData)) { 310 jlong pData = ptr_to_jlong(cData); 311 colorData = (*env)->NewObjectA(env, clsICMCD, initICMCDmID, (jvalue *)&pData); 312 313 if ((*env)->ExceptionCheck(env)) 314 { 315 free(cData); 316 return (ColorData*)NULL; 317 } 318 319 (*env)->SetObjectField(env, bisdo->icm, colorDataID, colorData); 320 } | 214 pRasInfo->scanStride = 0; 215 } else { 216 pRasInfo->rasBase = (void *) 217 (((uintptr_t) bipriv->base) + bisdo->offset); 218 pRasInfo->pixelStride = bisdo->pixStr; 219 pRasInfo->pixelBitOffset = bisdo->bitoffset; 220 pRasInfo->scanStride = bisdo->scanStr; 221 } 222 if (bipriv->lutbase == NULL) { 223 pRasInfo->lutBase = NULL; 224 pRasInfo->lutSize = 0; 225 } else { 226 pRasInfo->lutBase = bipriv->lutbase; 227 pRasInfo->lutSize = bisdo->lutsize; 228 } 229 if (bipriv->cData == NULL) { 230 pRasInfo->invColorTable = NULL; 231 pRasInfo->redErrTable = NULL; 232 pRasInfo->grnErrTable = NULL; 233 pRasInfo->bluErrTable = NULL; 234 pRasInfo->representsPrimaries = 0; 235 } else { 236 pRasInfo->invColorTable = bipriv->cData->img_clr_tbl; 237 pRasInfo->redErrTable = bipriv->cData->img_oda_red; 238 pRasInfo->grnErrTable = bipriv->cData->img_oda_green; 239 pRasInfo->bluErrTable = bipriv->cData->img_oda_blue; 240 pRasInfo->invGrayTable = bipriv->cData->pGrayInverseLutData; 241 pRasInfo->representsPrimaries = bipriv->cData->representsPrimaries; 242 } 243 } 244 245 static void BufImg_Release(JNIEnv *env, 246 SurfaceDataOps *ops, 247 SurfaceDataRasInfo *pRasInfo) 248 { 249 BufImgSDOps *bisdo = (BufImgSDOps *)ops; 250 BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv); 251 252 if (bipriv->base != NULL) { 253 jint mode = (((bipriv->lockFlags & (SD_LOCK_WRITE)) != 0) 254 ? 0 : JNI_ABORT); 255 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->array, 256 bipriv->base, mode); 257 } 258 if (bipriv->lutbase != NULL) { 259 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, 260 bipriv->lutbase, JNI_ABORT); 261 } 262 } 263 264 static int calculatePrimaryColorsApproximation(int* cmap, unsigned char* cube, int cube_size) { 265 int i, j, k; 266 int index, value, color; 267 // values calculated from cmap 268 int r, g, b; 269 // maximum positive/negative variation allowed for r, g, b values for primary colors 270 int delta = 5; 271 // get the primary color cmap indices from corner of inverse color table 272 for (i = 0; i < cube_size; i += (cube_size - 1)) { 273 for (j = 0; j < cube_size; j += (cube_size - 1)) { 274 for (k = 0; k < cube_size; k += (cube_size - 1)) { 275 // calculate inverse color table index 276 index = i + cube_size * (j + cube_size * k); 277 // get value present in corners of inverse color table 278 value = cube[index]; 279 // use the corner values as index for cmap 280 color = cmap[value]; 281 // extract r,g,b values from cmap value 282 r = ((color) >> 16) & 0xff; 283 g = ((color) >> 8) & 0xff; 284 b = color & 0xff; 285 /* 286 * If i/j/k value is 0 optimum value of b/g/r should be 0 but we allow 287 * maximum positive variation of 5. If i/j/k value is 31 optimum value 288 * of b/g/r should be 255 but we allow maximum negative variation of 5. 289 */ 290 if (i == 0) { 291 if (b > delta) 292 return 0; 293 } else { 294 if (b < (255 - delta)) 295 return 0; 296 } 297 if (j == 0) { 298 if (g > delta) 299 return 0; 300 } else { 301 if (g < (255 - delta)) 302 return 0; 303 } 304 if (k == 0) { 305 if (r > delta) 306 return 0; 307 } else { 308 if (r < (255 - delta)) 309 return 0; 310 } 311 } 312 } 313 } 314 return 1; 315 } 316 317 static ColorData *BufImg_SetupICM(JNIEnv *env, 318 BufImgSDOps *bisdo) 319 { 320 ColorData *cData = NULL; 321 jobject colorData; 322 323 if (JNU_IsNull(env, bisdo->icm)) { 324 return (ColorData *) NULL; 325 } 326 327 colorData = (*env)->GetObjectField(env, bisdo->icm, colorDataID); 328 329 if (JNU_IsNull(env, colorData)) { 330 if (JNU_IsNull(env, clsICMCD)) { 331 // we are unable to create a wrapper object 332 return (ColorData*)NULL; 333 } 334 } else { 335 cData = (ColorData*)JNU_GetLongFieldAsPtr(env, colorData, pDataID); 336 } 337 338 if (cData != NULL) { 339 return cData; 340 } 341 342 cData = (ColorData*)calloc(1, sizeof(ColorData)); 343 344 if (cData != NULL) { 345 jboolean allGray 346 = (*env)->GetBooleanField(env, bisdo->icm, allGrayID); 347 int *pRgb = (int *) 348 ((*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL)); 349 350 if (pRgb == NULL) { 351 free(cData); 352 return (ColorData*)NULL; 353 } 354 355 cData->img_clr_tbl = initCubemap(pRgb, bisdo->lutsize, 32); 356 cData->representsPrimaries = calculatePrimaryColorsApproximation(pRgb, cData->img_clr_tbl, 32); 357 if (allGray == JNI_TRUE) { 358 initInverseGrayLut(pRgb, bisdo->lutsize, cData); 359 } 360 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb, 361 JNI_ABORT); 362 363 initDitherTables(cData); 364 365 if (JNU_IsNull(env, colorData)) { 366 jlong pData = ptr_to_jlong(cData); 367 colorData = (*env)->NewObjectA(env, clsICMCD, initICMCDmID, (jvalue *)&pData); 368 369 if ((*env)->ExceptionCheck(env)) 370 { 371 free(cData); 372 return (ColorData*)NULL; 373 } 374 375 (*env)->SetObjectField(env, bisdo->icm, colorDataID, colorData); 376 } |