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