Path: blob/master/src/java.desktop/share/native/libawt/awt/image/DataBufferNative.c
41159 views
/*1* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>2627#include "SurfaceData.h"28#include "sun_awt_image_DataBufferNative.h"2930#include "jni_util.h"31#include "debug_trace.h"32#include <stdio.h>3334unsigned char *DBN_GetPixelPointer(JNIEnv *env, jint x, int y,35SurfaceDataRasInfo *lockInfo,36SurfaceDataOps *ops, int lockFlag)37{38if (ops == NULL) {39return NULL;40}4142lockInfo->bounds.x1 = x;43lockInfo->bounds.y1 = y;44lockInfo->bounds.x2 = x + 1;45lockInfo->bounds.y2 = y + 1;46if (ops->Lock(env, ops, lockInfo, lockFlag) != SD_SUCCESS) {47return NULL;48}49ops->GetRasInfo(env, ops, lockInfo);50if (lockInfo->rasBase) {51unsigned char *pixelPtr = (52(unsigned char*)lockInfo->rasBase +53(x * lockInfo->pixelStride + y * lockInfo->scanStride));54return pixelPtr;55}56SurfaceData_InvokeRelease(env, ops, lockInfo);57SurfaceData_InvokeUnlock(env, ops, lockInfo);58return NULL;59}6061/*62* Class: sun_awt_image_DataBufferNative63* Method: getElem64* Signature:65*/66JNIEXPORT jint JNICALL67Java_sun_awt_image_DataBufferNative_getElem(JNIEnv *env, jobject dbn,68jint x, jint y, jobject sd)69{70jint returnVal = -1;71unsigned char *pixelPtr;72SurfaceDataRasInfo lockInfo;73SurfaceDataOps *ops;74lockInfo.rasBase = NULL;7576ops = SurfaceData_GetOps(env, sd);77JNU_CHECK_EXCEPTION_RETURN(env, -1);7879if (!(pixelPtr = DBN_GetPixelPointer(env, x, y, &lockInfo,80ops, SD_LOCK_READ)))81{82return returnVal;83}84switch (lockInfo.pixelStride) {85case 4:86returnVal = *(int *)pixelPtr;87break;88/* REMIND: do we need a 3-byte case (for 24-bit) here? */89case 2:90returnVal = *(unsigned short *)pixelPtr;91break;92case 1:93returnVal = *pixelPtr;94break;95default:96break;97}98SurfaceData_InvokeRelease(env, ops, &lockInfo);99SurfaceData_InvokeUnlock(env, ops, &lockInfo);100return returnVal;101}102103104/*105* Class: sun_awt_image_DataBufferNative106* Method: setElem107* Signature:108*/109JNIEXPORT void JNICALL110Java_sun_awt_image_DataBufferNative_setElem(JNIEnv *env, jobject dbn,111jint x, jint y, jint val, jobject sd)112{113SurfaceDataRasInfo lockInfo;114SurfaceDataOps *ops;115unsigned char *pixelPtr;116lockInfo.rasBase = NULL;117118ops = SurfaceData_GetOps(env, sd);119JNU_CHECK_EXCEPTION(env);120121if (!(pixelPtr = DBN_GetPixelPointer(env, x, y, &lockInfo,122ops, SD_LOCK_WRITE)))123{124return;125}126127switch (lockInfo.pixelStride) {128case 4:129*(int *)pixelPtr = val;130break;131/* REMIND: do we need a 3-byte case (for 24-bit) here? */132case 2:133*(unsigned short *)pixelPtr = (unsigned short)val;134break;135case 1:136*pixelPtr = (unsigned char)val;137break;138default:139break;140}141SurfaceData_InvokeRelease(env, ops, &lockInfo);142SurfaceData_InvokeUnlock(env, ops, &lockInfo);143}144145146