API Overview API Index Package Overview Direct link to this page
JavaOnTracks 0.1.2
  net.jot.image. JOTAbstractImage View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

/*
------------------------------------
JavaOnTracks          Thibaut Colar
tcolar-jot AT colar DOT net
Artistic Licence 2.0
http://www.javaontracks.net
------------------------------------
 */
package net.jot.image;

import java.io.OutputStream;
import java.util.Vector;

/**
 * Represent an image data in a plain format, decoupled from storage format and AWT.
 * 
 * Data is stored in simple byteArray, RGBA(4 ints per pixel)  ~ potentially a memory hog.
 * @author thibautc
 */
public class JOTAbstractImage {

    /** Default: Alpha values are ignored, but much faster perfomance*/
    public static final int ALPHA_SUPPORT_NONE = 1;
    /**
     * If a pixel is set a new color, we will mix the new color with the old one.
     * ie: if ALPHA is 25% for the new color we will do:
     * TODO: revise formula
     * R(G,B)=(oldR*75/100 + newR*25/100)/2
     * A=newA 
     * 
     * TODO:
     */
    public static final int ALPHA_SUPPORT_SIMPLE_BLEND = 2;
    private int[] data;
    private int width;
    private int height;
    private int alphaSupport = ALPHA_SUPPORT_NONE;

    /**
     * 
     * @param width
     * @param height
     * @param alphaSupport: default is alpha support disabled(faster)
     */
    public JOTAbstractImage(int width, int height, int alphaSupport)
    {
        this.width = width;
        this.height = height;
        data = new int[width * height * 4];
        this.alphaSupport = alphaSupport;
    }

    public JOTAbstractImage(int width, int height)
    {
        this.width = width;
        this.height = height;
        data = new int[width * height * 4];
    }

    public void setPixel(int x, int y, JOTAbstractPixel pixel)
    {
        if (x > -1 && y > -1 && y < height && x < width)
        {
            int offset = y * width * 4 + x * 4;
            data[offset] = pixel.getRed();
            data[offset + 1] = pixel.getGreen();
            data[offset + 2] = pixel.getBlue();
            data[offset + 3] = pixel.getAlpha();
        }
    }

    public JOTAbstractPixel getPixel(int x, int y)
    {
        int offset = y * width * 4 + x * 4;
        int red = data[offset];
        int green = data[offset + 1];
        int blue = data[offset + 2];
        int alpha = data[offset + 3];

        return new JOTAbstractPixel(red, green, blue, alpha);
    }

    public int[] getData()
    {
        return data;
    }

    public int getHeight()
    {
        return height;
    }

    public int getWidth()
    {
        return width;
    }

    /** 
     * return the color depth in bits (ie: how big the palette needs to be)
     * ie:
     * 1: 2 colors or less
     * 4: 16 colors or less
     * 8: 256 colors or less
     * 16: 65536 colors or less
     * 24: 16 777 216 colors or less
     * 32: > 16 777 216 colors
     * resource intensive
     * 
     * Note: maxColors, is the maximum you want to look for, if more that "maxColor" colors are found, an exception will be thrown.
     * @return
     */
    public int getColorDepth(long maxColors) throws Exception
    {
        Vector differentPixels = new Vector();
        // scans the image to count colors
        for (int y = 0; y != height; y++)
        {
            for (int x = 0; x != width; x++)
            {
                JOTAbstractPixel pixel = getPixel(x, y);
                if (!differentPixels.contains(pixel))
                {
                    differentPixels.add(pixel);
                }
                if (differentPixels.size() > maxColors)
                {
                    throw new Exception("Too many colors found : > " + maxColors);
                }
            }
        }
        if (differentPixels.size() < 2)
        {
            return 2;
        }
        if (differentPixels.size() < 16)
        {
            return 4;
        }
        if (differentPixels.size() < 256)
        {
            return 8;
        }
        if (differentPixels.size() < 65536)
        {
            return 16;
        }
        if (differentPixels.size() < 16777216)
        {
            return 24;
        }
        return 32;
    }

    public void drawRectangle(int lineThickness, int x, int y, int x2, int y2, JOTAbstractPixel pixel)
    {
        drawHorizontalLine(lineThickness, x, x2, y, pixel);
        drawHorizontalLine(lineThickness, x, x2, y2, pixel);
        drawVerticalLine(lineThickness, x, y, y2, pixel);
        drawVerticalLine(lineThickness, x2, y, y2, pixel);
    }

    public void drawFilledRectangle(int x, int y, int x2, int y2, JOTAbstractPixel pixel)
    {
        for (int i = x; i < x2; i++)
        {
            drawVerticalLine(1, i, y, y2, pixel);
        }
    }

    /**
     * fill the whole image with the same pixel
     * ie: set a background
     * @param pixel
     */
    public void fillImage(JOTAbstractPixel pixel)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                setPixel(x, y, pixel);
            }
        }
    }

    public void drawLine(int lineThickness, int x, int y, int x2, int y2, JOTAbstractPixel pixel)
    {
        //System.out.println(new Date()+" "+x+":" +y+"-"+x2+":"+y2);
        int xdir=1;
        int ydir=1;
        if (x > x2)
        {
            xdir=-1;
        }
        if (y > y2)
        {
            ydir=-1;
        }
        if (y == y2)
        {
           //horiz
            drawHorizontalLine(lineThickness, x, x2, y, pixel);
        } else if (x == x2)
        {
           //vert
            drawVerticalLine(lineThickness, x, y, y2, pixel);
        } else
        {
            // "seideways" line, time to do some math :-)
            int w = x2 - x;
            int h = y2 - y;

            // first pixel
            for (int j = 0; j != lineThickness; j++)
            {
                int thickOff = (j + 1) / 2;
                if (thickOff != 0)
                {
                    if ((j + 1) % 2 != 0)
                    {
                        thickOff = -thickOff;
                    }
                }
                setPixel(x + (w>h?0:thickOff), y  + (w>h?thickOff:0), pixel);
            }
            if (w != 0)
            {
                if(w>h)
                {
                //mostly horizontal
                float m = (float) h / (float) w;
                float b = y - m * x;
                while (x != x2)
                {
                    x+=xdir;
                    y = Math.round(m * x + b);
                    for (int j = 0; j != lineThickness; j++)
                    {
                        int thickOff = (j+1) / 2;
                        if (thickOff != 0)
                        {
                            if((j+1)%2!=0)
                                thickOff=-thickOff;
                        }
                        setPixel(x, y+thickOff, pixel);
                    }
                }
                }
                else
                {
                //mostly vertical
                float m = (float) w / (float) h;
                float b = x - m * y;
                while (y != y2)
                {
                    y+=ydir;
                    x = Math.round(m * y + b);
                    for (int j = 0; j != lineThickness; j++)
                    {
                        int thickOff = (j+1) / 2;
                        if (thickOff != 0)
                        {
                            if((j+1)%2!=0)
                                thickOff=-thickOff;
                        }
                        setPixel(x+thickOff, y, pixel);
                    }
                }
                }
            }
        }
    }

    protected void drawHorizontalLine(int lineThickness, int x, int x2, int y, JOTAbstractPixel pixel)
    {
        int xdir=x2>x?1:-1;
        for (int i = x; i < x2; i+=xdir)
        {
            for (int j = 0; j != lineThickness; j++)
            {
                int thickOff = (j + 1) / 2;
                if ((j + 1) % 2 == 0)
                {
                    thickOff = -thickOff;
                }
                setPixel(i, y + thickOff, pixel);
            }
        }
    }

    protected void drawVerticalLine(int lineThickness, int x, int y, int y2, JOTAbstractPixel pixel)
    {
        int ydir=y2>y?1:-1;
        for (int i = y; i != y2; i+=ydir)
        {
            for (int j = 0; j != lineThickness; j++)
            {
                int thickOff = (j + 1) / 2;
                if ((j + 1) % 2 == 0)
                {
                    thickOff = -thickOff;
                }
                setPixel(x + thickOff, i, pixel);
            }
        }
    }

    public int writeToStream(JOTAbstractImageWriterInterface imageWriter, OutputStream stream) throws Exception
    {
        return imageWriter.writeToStream(this, stream);
    }
}

Generated By: JavaOnTracks Doclet 0.1.5     ©Thibaut Colar