1 /*
   2  * Copyright (c) 2000, 2014, 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 
  76     ops = SurfaceData_GetOps(env, sd);
  77     JNU_CHECK_EXCEPTION_RETURN(env, -1);
  78 
  79     if (!(pixelPtr = DBN_GetPixelPointer(env, x, y, &lockInfo,
  80                                          ops, SD_LOCK_READ)))
  81     {
  82         return returnVal;
  83     }
  84     switch (lockInfo.pixelStride) {
  85     case 4:
  86         returnVal = *(int *)pixelPtr;
  87         break;
  88     /* REMIND: do we need a 3-byte case (for 24-bit) here? */
  89     case 2:
  90         returnVal = *(unsigned short *)pixelPtr;
  91         break;
  92     case 1:
  93         returnVal = *pixelPtr;
  94         break;
  95     default:
  96         break;
  97     }
  98     SurfaceData_InvokeRelease(env, ops, &lockInfo);
  99     SurfaceData_InvokeUnlock(env, ops, &lockInfo);
 100     return returnVal;
 101 }
 102 
 103 
 104 /*
 105  * Class:     sun_awt_image_DataBufferNative
 106  * Method:    setElem
 107  * Signature:
 108  */
 109 JNIEXPORT void JNICALL
 110 Java_sun_awt_image_DataBufferNative_setElem(JNIEnv *env, jobject dbn,
 111                                             jint x, jint y, jint val, jobject sd)
 112 {
 113     SurfaceDataRasInfo lockInfo;
 114     SurfaceDataOps *ops;
 115     unsigned char *pixelPtr;
 116 
 117 
 118     ops = SurfaceData_GetOps(env, sd);
 119     JNU_CHECK_EXCEPTION(env);
 120 
 121     if (!(pixelPtr = DBN_GetPixelPointer(env, x, y, &lockInfo,
 122                                          ops, SD_LOCK_WRITE)))
 123     {
 124         return;
 125     }
 126 
 127     switch (lockInfo.pixelStride) {
 128     case 4:
 129         *(int *)pixelPtr = val;
 130         break;
 131     /* REMIND: do we need a 3-byte case (for 24-bit) here? */
 132     case 2:
 133         *(unsigned short *)pixelPtr = (unsigned short)val;
 134         break;
 135     case 1:
 136         *pixelPtr = (unsigned char)val;
 137         break;
 138     default:
 139         break;
 140     }
 141     SurfaceData_InvokeRelease(env, ops, &lockInfo);
 142     SurfaceData_InvokeUnlock(env, ops, &lockInfo);
 143 }