javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ImageDataWrapper.java
author Anton Epple <toni.epple@eppleton.de>
Mon, 27 May 2013 11:31:41 +0200
branchcanvas
changeset 1156 9a6cf322f890
parent 1145 0e2c3676d77a
child 1158 633572e14095
permissions -rw-r--r--
RGBA values for ImageData
     1 /**
     2  * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
     3  * <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify it under
     6  * the terms of the GNU General Public License as published by the Free Software
     7  * Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12  * details.
    13  *
    14  * You should have received a copy of the GNU General Public License along with
    15  * this program. Look for COPYING file in the top folder. If not, see
    16  * http://opensource.org/licenses/GPL-2.0.
    17  */
    18 /*
    19  * To change this template, choose Tools | Templates
    20  * and open the template in the editor.
    21  */
    22 package org.apidesign.bck2brwsr.htmlpage;
    23 
    24 import net.java.html.canvas.ImageData;
    25 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    26 
    27 /**
    28  *
    29  * @author Anton Epple <toni.epple@eppleton.de>
    30  */
    31 public class ImageDataWrapper implements ImageData {
    32 
    33     private double width, height = -1;
    34     private Object imageData;
    35     private Data data;
    36 
    37     public ImageDataWrapper(Object imageData) {
    38         this.imageData = imageData;
    39     }
    40 
    41     private Data getData() {
    42         if (data == null) {
    43             data = new Data(getDataImpl(imageData));
    44         }
    45         return data;
    46     }
    47 
    48     @JavaScriptBody(args = {"imageData"}, body = "return imageData.data")
    49     public native Object getDataImpl(Object imageData);
    50 
    51     public double getWidth() {
    52         if (width == -1) {
    53             width = getWidthImpl(imageData);
    54         }
    55         return width;
    56     }
    57 
    58     @JavaScriptBody(args = {"imageData"}, body = "return imagedata.width;")
    59     private static native double getWidthImpl(Object imageData);
    60 
    61     public double getHeight() {
    62         if (height == -1) {
    63             height = getHeightImpl(imageData);
    64         }
    65         return height;
    66     }
    67 
    68     @JavaScriptBody(args = {"imageData"}, body = "return imagedata.height;")
    69     private static native double getHeightImpl(Object imageData);
    70 
    71     Object object() {
    72         return imageData;
    73     }
    74 
    75     @Override
    76     public int getR(double x, double y) {
    77         double idx = (x + y * width) * 4;
    78         return getData().get(idx);
    79     }
    80 
    81     @Override
    82     public int getG(double x, double y) {
    83         double idx = (x + y * width) * 4;
    84         return getData().get(idx + 1);
    85     }
    86 
    87     @Override
    88     public int getB(double x, double y) {
    89         double idx = (x + y * width) * 4;
    90         return getData().get(idx + 2);
    91     }
    92 
    93     @Override
    94     public int getA(double x, double y) {
    95         double idx = (x + y * width) * 4;
    96         return getData().get(idx + 3);
    97     }
    98 
    99     @Override
   100     public void setR(double x, double y, int value) {
   101         double idx = (x + y * width) * 4;
   102         getData().set(idx, value);
   103     }
   104 
   105     @Override
   106     public void setG(double x, double y, int value) {
   107         double idx = (x + y * width) * 4;
   108         getData().set(idx + 1, value);
   109     }
   110 
   111     @Override
   112     public void setB(double x, double y, int value) {
   113         double idx = (x + y * width) * 4;
   114         getData().set(idx + 2, value);
   115     }
   116 
   117     @Override
   118     public void setA(double x, double y, int value) {
   119         double idx = (x + y * width) * 4;
   120         getData().set(idx + 3, value);
   121     }
   122 
   123     public static class Data {
   124 
   125         Object data;
   126 
   127         public Data(Object data) {
   128             this.data = data;
   129         }
   130 
   131         public int get(double index) {
   132             return getImpl(data, index);
   133         }
   134 
   135         public void set(double index, int value) {
   136             setImpl(data, index, value);
   137         }
   138 
   139         @JavaScriptBody(args = {"data", "index", "value"}, body = "data[index]=value;")
   140         private static native void setImpl(Object data, double index, int value);
   141 
   142         @JavaScriptBody(args = {"imagedata", "index"}, body = "return data[index];")
   143         private static native int getImpl(Object data, double index);
   144     }
   145 }