RGBA values for ImageData canvas
authorAnton Epple <toni.epple@eppleton.de>
Mon, 27 May 2013 11:31:41 +0200
branchcanvas
changeset 11569a6cf322f890
parent 1155 ab08a4271d5f
child 1157 c312f00cbace
RGBA values for ImageData
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ImageDataWrapper.java
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ImageDataWrapper.java	Mon May 27 11:26:55 2013 +0200
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ImageDataWrapper.java	Mon May 27 11:31:41 2013 +0200
     1.3 @@ -1,19 +1,19 @@
     1.4  /**
     1.5 - * Back 2 Browser Bytecode Translator
     1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
     1.8 + * <jaroslav.tulach@apidesign.org>
     1.9   *
    1.10 - * This program is free software: you can redistribute it and/or modify
    1.11 - * it under the terms of the GNU General Public License as published by
    1.12 - * the Free Software Foundation, version 2 of the License.
    1.13 + * This program is free software: you can redistribute it and/or modify it under
    1.14 + * the terms of the GNU General Public License as published by the Free Software
    1.15 + * Foundation, version 2 of the License.
    1.16   *
    1.17 - * This program is distributed in the hope that it will be useful,
    1.18 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.20 - * GNU General Public License for more details.
    1.21 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.22 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.23 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1.24 + * details.
    1.25   *
    1.26 - * You should have received a copy of the GNU General Public License
    1.27 - * along with this program. Look for COPYING file in the top folder.
    1.28 - * If not, see http://opensource.org/licenses/GPL-2.0.
    1.29 + * You should have received a copy of the GNU General Public License along with
    1.30 + * this program. Look for COPYING file in the top folder. If not, see
    1.31 + * http://opensource.org/licenses/GPL-2.0.
    1.32   */
    1.33  /*
    1.34   * To change this template, choose Tools | Templates
    1.35 @@ -28,34 +28,41 @@
    1.36   *
    1.37   * @author Anton Epple <toni.epple@eppleton.de>
    1.38   */
    1.39 -public class ImageDataWrapper implements ImageData{
    1.40 +public class ImageDataWrapper implements ImageData {
    1.41  
    1.42 +    private double width, height = -1;
    1.43      private Object imageData;
    1.44      private Data data;
    1.45  
    1.46      public ImageDataWrapper(Object imageData) {
    1.47          this.imageData = imageData;
    1.48      }
    1.49 -    
    1.50 -    public Data getData(){
    1.51 -        if (data == null){
    1.52 +
    1.53 +    private Data getData() {
    1.54 +        if (data == null) {
    1.55              data = new Data(getDataImpl(imageData));
    1.56          }
    1.57          return data;
    1.58      }
    1.59 -    
    1.60 +
    1.61      @JavaScriptBody(args = {"imageData"}, body = "return imageData.data")
    1.62      public native Object getDataImpl(Object imageData);
    1.63  
    1.64      public double getWidth() {
    1.65 -        return getWidthImpl(imageData);
    1.66 +        if (width == -1) {
    1.67 +            width = getWidthImpl(imageData);
    1.68 +        }
    1.69 +        return width;
    1.70      }
    1.71  
    1.72      @JavaScriptBody(args = {"imageData"}, body = "return imagedata.width;")
    1.73      private static native double getWidthImpl(Object imageData);
    1.74  
    1.75      public double getHeight() {
    1.76 -        return getHeightImpl(imageData);
    1.77 +        if (height == -1) {
    1.78 +            height = getHeightImpl(imageData);
    1.79 +        }
    1.80 +        return height;
    1.81      }
    1.82  
    1.83      @JavaScriptBody(args = {"imageData"}, body = "return imagedata.height;")
    1.84 @@ -66,15 +73,52 @@
    1.85      }
    1.86  
    1.87      @Override
    1.88 -    public int getPixel(double x, double y) {
    1.89 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    1.90 +    public int getR(double x, double y) {
    1.91 +        double idx = (x + y * width) * 4;
    1.92 +        return getData().get(idx);
    1.93      }
    1.94  
    1.95      @Override
    1.96 -    public void setPixel(double x, double y, int value) {
    1.97 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    1.98 +    public int getG(double x, double y) {
    1.99 +        double idx = (x + y * width) * 4;
   1.100 +        return getData().get(idx + 1);
   1.101      }
   1.102  
   1.103 +    @Override
   1.104 +    public int getB(double x, double y) {
   1.105 +        double idx = (x + y * width) * 4;
   1.106 +        return getData().get(idx + 2);
   1.107 +    }
   1.108 +
   1.109 +    @Override
   1.110 +    public int getA(double x, double y) {
   1.111 +        double idx = (x + y * width) * 4;
   1.112 +        return getData().get(idx + 3);
   1.113 +    }
   1.114 +
   1.115 +    @Override
   1.116 +    public void setR(double x, double y, int value) {
   1.117 +        double idx = (x + y * width) * 4;
   1.118 +        getData().set(idx, value);
   1.119 +    }
   1.120 +
   1.121 +    @Override
   1.122 +    public void setG(double x, double y, int value) {
   1.123 +        double idx = (x + y * width) * 4;
   1.124 +        getData().set(idx + 1, value);
   1.125 +    }
   1.126 +
   1.127 +    @Override
   1.128 +    public void setB(double x, double y, int value) {
   1.129 +        double idx = (x + y * width) * 4;
   1.130 +        getData().set(idx + 2, value);
   1.131 +    }
   1.132 +
   1.133 +    @Override
   1.134 +    public void setA(double x, double y, int value) {
   1.135 +        double idx = (x + y * width) * 4;
   1.136 +        getData().set(idx + 3, value);
   1.137 +    }
   1.138  
   1.139      public static class Data {
   1.140  
   1.141 @@ -84,18 +128,18 @@
   1.142              this.data = data;
   1.143          }
   1.144  
   1.145 -        public int get(int index) {
   1.146 +        public int get(double index) {
   1.147              return getImpl(data, index);
   1.148          }
   1.149  
   1.150 -        public void set(int index, int value) {
   1.151 +        public void set(double index, int value) {
   1.152              setImpl(data, index, value);
   1.153          }
   1.154  
   1.155          @JavaScriptBody(args = {"data", "index", "value"}, body = "data[index]=value;")
   1.156 -        private static native void setImpl(Object data, int index, int value);
   1.157 +        private static native void setImpl(Object data, double index, int value);
   1.158  
   1.159          @JavaScriptBody(args = {"imagedata", "index"}, body = "return data[index];")
   1.160 -        private static native int getImpl(Object data, int index);
   1.161 +        private static native int getImpl(Object data, double index);
   1.162      }
   1.163  }