1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 
  28 #include "SurfaceData.h"
  29 #include "sun_awt_image_DataBufferNative.h"
  30 
  31 #include "jni_util.h"
  32 #include "debug_trace.h"
  33 #include <stdio.h>
  34 
  35 unsigned char *DBN_GetPixelPointer(JNIEnv *env, jint x, int y,
  36                                    SurfaceDataRasInfo *lockInfo,
  37                                    SurfaceDataOps *ops, int lockFlag)
  38 {
  39     if (ops == NULL) {
  40         return NULL;
  41     }
  42 
  43     lockInfo->bounds.x1 = x;
  44     lockInfo->bounds.y1 = y;
  45     lockInfo->bounds.x2 = x + 1;
  46     lockInfo->bounds.y2 = y + 1;
  47     if (ops->Lock(env, ops, lockInfo, lockFlag) != SD_SUCCESS) {
  48         return NULL;
  49     }
  50     ops->GetRasInfo(env, ops, lockInfo);
  51     if (lockInfo->rasBase) {
  52         unsigned char *pixelPtr = (
  53             (unsigned char*)lockInfo->rasBase +
  54             (x * lockInfo->pixelStride + y * lockInfo->scanStride));
  55         return pixelPtr;
  56     }
  57     SurfaceData_InvokeRelease(env, ops, lockInfo);
  58     SurfaceData_InvokeUnlock(env, ops, lockInfo);
  59     return NULL;
  60 }
  61 
  62 /*
  63  * Class:     sun_awt_image_DataBufferNative
  64  * Method:    getElem
  65  * Signature:
  66  */
  67 JNIEXPORT jint JNICALL
  68 Java_sun_awt_image_DataBufferNative_getElem(JNIEnv *env, jobject dbn,
  69                                             jint x, jint y, jobject sd)
  70 {
  71     jint returnVal = -1;
  72     unsigned char *pixelPtr;
  73     SurfaceDataRasInfo lockInfo;
  74     SurfaceDataOps *ops;
  75     lockInfo.rasBase = NULL;
  76 
  77     ops = SurfaceData_GetOps(env, sd);
  78     JNU_CHECK_EXCEPTION_RETURN(env, -1);
  79 
  80     if (!(pixelPtr = DBN_GetPixelPointer(env, x, y, &lockInfo,
  81                                          ops, SD_LOCK_READ)))
  82     {
  83         return returnVal;
  84     }
  85     switch (lockInfo.pixelStride) {
  86     case 4:
  87         returnVal = *(int *)pixelPtr;
  88         break;
  89     /* REMIND: do we need a 3-byte case (for 24-bit) here? */
  90     case 2:
  91         returnVal = *(unsigned short *)pixelPtr;
  92         break;
  93     case 1:
  94         returnVal = *pixelPtr;
  95         break;
  96     default:
  97         break;
  98     }
  99     SurfaceData_InvokeRelease(env, ops, &lockInfo);
 100     SurfaceData_InvokeUnlock(env, ops, &lockInfo);
 101     return returnVal;
 102 }
 103 
 104 
 105 /*
 106  * Class:     sun_awt_image_DataBufferNative
 107  * Method:    setElem
 108  * Signature:
 109  */
 110 JNIEXPORT void JNICALL
 111 Java_sun_awt_image_DataBufferNative_setElem(JNIEnv *env, jobject dbn,
 112                                             jint x, jint y, jint val, jobject sd)
 113 {
 114     SurfaceDataRasInfo lockInfo;
 115     SurfaceDataOps *ops;
 116     unsigned char *pixelPtr;
 117     lockInfo.rasBase = NULL;
 118 
 119     ops = SurfaceData_GetOps(env, sd);
 120     JNU_CHECK_EXCEPTION(env);
 121 
 122     if (!(pixelPtr = DBN_GetPixelPointer(env, x, y, &lockInfo,
 123                                          ops, SD_LOCK_WRITE)))
 124     {
 125         return;
 126     }
 127 
 128     switch (lockInfo.pixelStride) {
 129     case 4:
 130         *(int *)pixelPtr = val;
 131         break;
 132     /* REMIND: do we need a 3-byte case (for 24-bit) here? */
 133     case 2:
 134         *(unsigned short *)pixelPtr = (unsigned short)val;
 135         break;
 136     case 1:
 137         *pixelPtr = (unsigned char)val;
 138         break;
 139     default:
 140         break;
 141     }
 142     SurfaceData_InvokeRelease(env, ops, &lockInfo);
 143     SurfaceData_InvokeUnlock(env, ops, &lockInfo);
 144 }