javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ImageDataWrapper.java
author Anton Epple <toni.epple@eppleton.de>
Tue, 11 Feb 2014 13:39:53 +0100
branchcanvas
changeset 1441 86ee3be71c76
parent 1158 633572e14095
permissions -rw-r--r--
fixed method signatures of ImageDataWrapper (double -> int)
     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 package org.apidesign.bck2brwsr.htmlpage;
    20 
    21 import net.java.html.canvas.ImageData;
    22 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    23 
    24 /**
    25  *
    26  * @author Anton Epple <toni.epple@eppleton.de>
    27  */
    28 public class ImageDataWrapper implements ImageData {
    29 
    30     private double width, height = -1;
    31     private Object imageData;
    32     private Data data;
    33 
    34     public ImageDataWrapper(Object imageData) {
    35         this.imageData = imageData;
    36     }
    37 
    38     private Data getData() {
    39         if (data == null) {
    40             data = new Data(getDataImpl(imageData));
    41         }
    42         return data;
    43     }
    44 
    45     @JavaScriptBody(args = {"imageData"}, body = "return imageData.data")
    46     public native Object getDataImpl(Object imageData);
    47 
    48     public double getWidth() {
    49         if (width == -1) {
    50             width = getWidthImpl(imageData);
    51         }
    52         return width;
    53     }
    54 
    55     @JavaScriptBody(args = {"imageData"}, body = "return imagedata.width;")
    56     private static native int getWidthImpl(Object imageData);
    57 
    58     public double getHeight() {
    59         if (height == -1) {
    60             height = getHeightImpl(imageData);
    61         }
    62         return height;
    63     }
    64 
    65     @JavaScriptBody(args = {"imageData"}, body = "return imagedata.height;")
    66     private static native int getHeightImpl(Object imageData);
    67 
    68     Object object() {
    69         return imageData;
    70     }
    71 
    72     @Override
    73     public int getR(int x, int y) {
    74         int idx = (x + y * (int)width) * 4;
    75         return getData().get(idx);
    76     }
    77 
    78     @Override
    79     public int getG(int x, int y) {
    80         int idx = (x + y * (int)width) * 4;
    81         return getData().get(idx + 1);
    82     }
    83 
    84     @Override
    85     public int getB(int x, int y) {
    86         int idx = (x + y * (int)width) * 4;
    87         return getData().get(idx + 2);
    88     }
    89 
    90     @Override
    91     public int getA(int x, int y) {
    92         int idx = (x + y * (int)width) * 4;
    93         return getData().get(idx + 3);
    94     }
    95 
    96     @Override
    97     public void setR(int x, int y, int value) {
    98         int idx = (x + y * (int)width) * 4;
    99         getData().set(idx, value);
   100     }
   101 
   102     @Override
   103     public void setG(int x, int y, int value) {
   104         int idx = (x + y * (int)width) * 4;
   105         getData().set(idx + 1, value);
   106     }
   107 
   108     @Override
   109     public void setB(int x, int y, int value) {
   110         int idx = (x + y * (int)width) * 4;
   111         getData().set(idx + 2, value);
   112     }
   113 
   114     @Override
   115     public void setA(int x, int y, int value) {
   116         int idx = (x + y * (int)width) * 4;
   117         getData().set(idx + 3, value);
   118     }
   119 
   120     private static class Data {
   121 
   122         Object data;
   123 
   124         public Data(Object data) {
   125             this.data = data;
   126         }
   127 
   128         public int get(int index) {
   129             return getImpl(data, index);
   130         }
   131 
   132         public void set(int index, int value) {
   133             setImpl(data, index, value);
   134         }
   135 
   136         @JavaScriptBody(args = {"data", "index", "value"}, body = "data[index]=value;")
   137         private static native void setImpl(Object data, int index, int value);
   138 
   139         @JavaScriptBody(args = {"imagedata", "index"}, body = "return data[index];")
   140         private static native int getImpl(Object data, int index);
   141     }
   142 }