/
ymageMatrix.java
745 lines (675 loc) · 28.6 KB
/
ymageMatrix.java
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
// ymageMatrix.java
// (C) 2005 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 16.09.2005 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
// $LastChangedRevision: 1986 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/*
This Class implements some convenience-methods to support drawing of statistical Data
It is not intended to replace existing awt-funktions even if it looks so
This class provides some drawing methods that creates transparency effects that
are not available in awt.
*/
package de.anomic.ymage;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import de.anomic.server.serverByteBuffer;
import de.anomic.server.serverMemory;
public class ymageMatrix {
// colors regarding RGB Color Model
public static final long RED = 0xFF0000;
public static final long GREEN = 0x00FF00;
public static final long BLUE = 0x0000FF;
public static final long GREY = 0x888888;
public static final byte MODE_REPLACE = 0;
public static final byte MODE_ADD = 1;
public static final byte MODE_SUB = 2;
public static final byte FILTER_ANTIALIASING = 0;
public static final byte FILTER_BLUR = 1;
public static final byte FILTER_INVERT = 2;
protected int width, height;
private BufferedImage image;
private WritableRaster grid;
private int[] defaultCol;
private long backgroundCol;
private byte defaultMode;
public ymageMatrix(int width, int height, byte drawMode, String backgroundColor) {
this(width, height, drawMode, Long.parseLong(backgroundColor, 16));
}
public ymageMatrix(int width, int height, byte drawMode, long backgroundColor) {
if (!(serverMemory.request(1024 * 1024 + 3 * width * height, false))) throw new RuntimeException("ymage: not enough memory (" + serverMemory.available() + ") available");
this.width = width;
this.height = height;
this.backgroundCol = backgroundColor;
this.defaultCol = new int[]{0xFF, 0xFF, 0xFF};
this.defaultMode = drawMode;
this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//this.image = imageFromPool(width, height, 1000);
this.clear();
this.grid = image.getRaster();
}
public void clear() {
// fill grid with background color
int bgR, bgG, bgB;
/*if (drawMode == MODE_SUB) {
bgR = (int) (0xFF - (this.backgroundCol >> 16));
bgG = (int) (0xFF - ((this.backgroundCol >> 8) & 0xff));
bgB = (int) (0xFF - (this.backgroundCol & 0xff));
} else {*/
bgR = (int) (this.backgroundCol >> 16);
bgG = (int) ((this.backgroundCol >> 8) & 0xff);
bgB = (int) (this.backgroundCol & 0xff);
//}
Graphics2D gr = image.createGraphics();
gr.setBackground(new Color(bgR, bgG, bgB));
gr.clearRect(0, 0, width, height);
/*
int[] c = new int[]{bgR, bgG, bgB};
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
grid.setPixel(i, j, c);
}
}
*/
}
public BufferedImage getImage() {
return this.image;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setColor(long c) {
if (this.defaultMode == MODE_SUB) {
int r = (int) (c >> 16);
int g = (int) ((c >> 8) & 0xff);
int b = (int) (c & 0xff);
defaultCol[0] = (g + b) / 2;
defaultCol[1] = (r + b) / 2;
defaultCol[2] = (r + g) / 2;
} else {
defaultCol[0] = (int) (c >> 16);
defaultCol[1] = (int) ((c >> 8) & 0xff);
defaultCol[2] = (int) (c & 0xff);
}
}
public void setColor(String s) {
setColor(Long.parseLong(s, 16));
}
public void plot(int x, int y) {
plot (x, y, 100);
}
private int[] cc = new int[3];
public void plot(int x, int y, int intensity) {
if ((x < 0) || (x >= width)) return;
if ((y < 0) || (y >= height)) return;
synchronized (cc) {
if (this.defaultMode == MODE_REPLACE) {
if (intensity == 100) {
cc[0] = defaultCol[0];
cc[1] = defaultCol[1];
cc[2] = defaultCol[2];
} else {
int[] c = new int[3];
c = grid.getPixel(x, y, c);
cc[0] = (intensity * defaultCol[0] + (100 - intensity) * c[0]) / 100;
cc[1] = (intensity * defaultCol[1] + (100 - intensity) * c[1]) / 100;
cc[2] = (intensity * defaultCol[2] + (100 - intensity) * c[2]) / 100;
}
} else if (this.defaultMode == MODE_ADD) {
int[] c = new int[3];
c = grid.getPixel(x, y, c);
if (intensity == 100) {
cc[0] = (0xff & c[0]) + defaultCol[0]; if (cc[0] > 255) cc[0] = 255;
cc[1] = (0xff & c[1]) + defaultCol[1]; if (cc[1] > 255) cc[1] = 255;
cc[2] = (0xff & c[2]) + defaultCol[2]; if (cc[2] > 255) cc[2] = 255;
} else {
cc[0] = (0xff & c[0]) + (intensity * defaultCol[0] / 100); if (cc[0] > 255) cc[0] = 255;
cc[1] = (0xff & c[1]) + (intensity * defaultCol[1] / 100); if (cc[1] > 255) cc[1] = 255;
cc[2] = (0xff & c[2]) + (intensity * defaultCol[2] / 100); if (cc[2] > 255) cc[2] = 255;
}
} else if (this.defaultMode == MODE_SUB) {
int[] c = new int[3];
c = grid.getPixel(x, y, c);
if (intensity == 100) {
cc[0] = (0xff & c[0]) - defaultCol[0]; if (cc[0] < 0) cc[0] = 0;
cc[1] = (0xff & c[1]) - defaultCol[1]; if (cc[1] < 0) cc[1] = 0;
cc[2] = (0xff & c[2]) - defaultCol[2]; if (cc[2] < 0) cc[2] = 0;
} else {
cc[0] = (0xff & c[0]) - (intensity * defaultCol[0] / 100); if (cc[0] < 0) cc[0] = 0;
cc[1] = (0xff & c[1]) - (intensity * defaultCol[1] / 100); if (cc[1] < 0) cc[1] = 0;
cc[2] = (0xff & c[2]) - (intensity * defaultCol[2] / 100); if (cc[2] < 0) cc[2] = 0;
}
}
grid.setPixel(x, y, cc);
}
}
public void line(int Ax, int Ay, int Bx, int By) {
// Bresenham's line drawing algorithm
int dX = Math.abs(Bx-Ax);
int dY = Math.abs(By-Ay);
int Xincr, Yincr;
if (Ax > Bx) Xincr=-1; else Xincr=1;
if (Ay > By) Yincr=-1; else Yincr=1;
if (dX >= dY) {
int dPr = dY<<1;
int dPru = dPr - (dX<<1);
int P = dPr - dX;
for (; dX>=0; dX--) {
plot(Ax, Ay);
if (P > 0) {
Ax+=Xincr;
Ay+=Yincr;
P+=dPru;
} else {
Ax+=Xincr;
P+=dPr;
}
}
} else {
int dPr = dX<<1;
int dPru = dPr - (dY<<1);
int P = dPr - dY;
for (; dY>=0; dY--) {
plot(Ax, Ay);
if (P > 0) {
Ax+=Xincr;
Ay+=Yincr;
P+=dPru;
} else {
Ay+=Yincr;
P+=dPr;
}
}
}
}
public void lineDot(int x0, int y0, int x1, int y1, int radius, int distance, long lineColor, long dotColor) {
// draw a line with a dot at the end.
// the radius value is the radius of the dot
// the distance value is the distance of the dot border to the endpoint
// compute first the angle of the line between the points
double angle = (x1 - x0 > 0) ? Math.atan(((double) (y0 - y1)) / ((double) (x1 - x0))) : Math.PI - Math.atan(((double) (y0 - y1)) / ((double) (x0 - x1)));
// now find two more points in between
// first calculate the radius' of the points
double ra = Math.sqrt(((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1))); // from a known point x1, y1
double rb = ra - radius - distance;
double rc = rb - radius;
//System.out.println("CONTROL angle = " + angle);
//System.out.println("CONTROL x1 = " + x1 + ", x1calc = " + ((x0 + ((int) ra * Math.cos(angle)))));
//System.out.println("CONTROL y1 = " + y1 + ", y1calc = " + ((y0 - ((int) ra * Math.sin(angle)))));
// the points are on a circle with radius rb and rc
int x2 = x0 + ((int) (rb * Math.cos(angle)));
int y2 = y0 - ((int) (rb * Math.sin(angle)));
int x3 = x0 + ((int) (rc * Math.cos(angle)));
int y3 = y0 - ((int) (rc * Math.sin(angle)));
setColor(lineColor);
line(x0, y0, x3, y3);
setColor(dotColor);
dot(x2, y2, radius, true);
}
public int[] getColor(int x, int y) {
int[] c = new int[3];
return grid.getPixel(x, y, c);
}
public void dot(int x, int y, int radius, boolean filled) {
if (filled) {
for (int r = radius; r >= 0; r--) ymageToolCircle.circle(this, x, y, r);
} else {
ymageToolCircle.circle(this, x, y, radius);
}
}
public void arc(int x, int y, int innerRadius, int outerRadius, int fromArc, int toArc) {
for (int r = innerRadius; r <= outerRadius; r++) ymageToolCircle.circle(this, x, y, r, fromArc, toArc);
}
public void arcLine(int cx, int cy, int innerRadius, int outerRadius, int angle) {
int xi = cx + (int) (innerRadius * Math.cos(Math.PI * angle / 180));
int yi = cy - (int) (innerRadius * Math.sin(Math.PI * angle / 180));
int xo = cx + (int) (outerRadius * Math.cos(Math.PI * angle / 180));
int yo = cy - (int) (outerRadius * Math.sin(Math.PI * angle / 180));
line(xi, yi, xo, yo);
}
public void arcDot(int cx, int cy, int arcRadius, int angle, int dotRadius) {
int x = cx + (int) (arcRadius * Math.cos(Math.PI * angle / 180));
int y = cy - (int) (arcRadius * Math.sin(Math.PI * angle / 180));
dot(x, y, dotRadius, true);
}
public void arcArc(int cx, int cy, int arcRadius, int angle, int innerRadius, int outerRadius, int fromArc, int toArc) {
int x = cx + (int) (arcRadius * Math.cos(Math.PI * angle / 180));
int y = cy - (int) (arcRadius * Math.sin(Math.PI * angle / 180));
arc(x, y, innerRadius, outerRadius, fromArc, toArc);
}
/**
* inserts an image into the ymageMatrix
* @param bitmap the bitmap to be inserted
* @param x the x value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param y the y value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @author Marc Nause
*/
public void insertBitmap(BufferedImage bitmap, int x, int y) {
insertBitmap(bitmap, x, y, -1);
}
/**
* inserts an image into the ymageMatrix
* @param bitmap the bitmap to be inserted
* @param x the x value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param y the y value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param filter chooses filter
* @author Marc Nause
*/
public void insertBitmap(BufferedImage bitmap, int x, int y, byte filter) {
insertBitmap(bitmap, x, y, -1, filter);
}
/**
* inserts an image into the ymageMatrix where all pixels that have the same RGB value as the
* pixel at (xx, yy) are transparent
* @param bitmap the bitmap to be inserted
* @param x the x value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param y the y value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param xx the x value of the pixel that determines which color is transparent
* @param yy the y value of the pixel that determines which color is transparent
* @author Marc Nause
*/
public void insertBitmap(BufferedImage bitmap, int x, int y, int xx, int yy) {
insertBitmap(bitmap, x, y, bitmap.getRGB(xx, yy));
}
/**
* inserts an image into the ymageMatrix where all pixels that have the same RGB value as the
* pixel at (xx, yy) are transparent
* @param bitmap the bitmap to be inserted
* @param x the x value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param y the y value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param xx the x value of the pixel that determines which color is transparent
* @param yy the y value of the pixel that determines which color is transparent
* @param filter chooses filter
* @author Marc Nause
*/
public void insertBitmap(BufferedImage bitmap, int x, int y, int xx, int yy, byte filter) {
insertBitmap(bitmap, x, y, bitmap.getRGB(xx, yy), filter);
}
/**
* inserts an image into the ymageMatrix where all pixels that have a special RGB value
* pixel at (xx, yy) are transparent
* @param bitmap the bitmap to be inserted
* @param x the x value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param y the y value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param rgb the RGB value that will be transparent
* @author Marc Nause
*/
public void insertBitmap(BufferedImage bitmap, int x, int y, int transRGB) {
int heightSrc = bitmap.getHeight();
int widthSrc = bitmap.getWidth();
int heightTgt = height;
int widthTgt = width;
int rgb;
for (int i = 0; i < heightSrc; i++) {
for (int j = 0; j < widthSrc; j++) {
// pixel in legal area?
if (j+x >= 0 && i+y >= 0 && i+y < heightTgt && j+x < widthTgt) {
rgb = bitmap.getRGB(j, i);
if (rgb != transRGB) {
image.setRGB(j+x, i+y, rgb);
}
}
}
}
}
/**
* inserts an image into the ymageMatrix where all pixels that have a special RGB value
* pixel at (xx, yy) are transparent
* @param bitmap the bitmap to be inserted
* @param x the x value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param y the y value of the upper left coordinate in the ymageMatrix where the bitmap will be placed
* @param rgb the RGB value that will be transparent
* @param filter chooses filter
* @author Marc Nause
*/
public void insertBitmap(BufferedImage bitmap, int x, int y, int transRGB, byte filter) {
insertBitmap(bitmap, x, y, transRGB);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
if (filter == FILTER_ANTIALIASING) {
int transX = -1;
int transY = -1;
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// find first pixel in bitmap that equals transRGB
// and also lies in area of image that will be covered by bitmap
int i = 0;
int j = 0;
boolean found = false;
while ((i < bitmapWidth) && (i + x < imageWidth) && !found) {
while ((j < bitmapHeight) && (j + y < imageHeight) && !found) {
if (bitmap.getRGB(i, j) == transRGB) {
transX = i;
transY = j;
}
j++;
}
i++;
}
// if there is a transparent pixel in the bitmap that covers an area
// of the image, the fiter will be used. If no such pixel has been found that
// means that there either is no transparent pixel in the bitmap or part
// of the bitmap that covers part of tha image is not within the borders of
// the image (i.e. bitmap is larger than image)
if (transX != -1) {
filter(x - 1, y - 1, x + bitmapWidth, y + bitmapHeight, filter, image.getRGB(transX + x, transY + y));
}
} else {
filter(x - 1, y - 1, x + bitmapWidth, y + bitmapHeight, filter, -1);
}
}
/**
* antialiasing filter for a square part of the ymageMatrix
* @param lox x value for left upper coordinate
* @param loy y value for left upper coordinate
* @param rux x value for right lower coordinate
* @param ruy y value for right lower coordinate
* @param rgb color of background
* @author Marc Nause
*/
public void antialiasing(int lox, int loy, int rux, int ruy, int bgcolor) {
filter(lox, loy, rux, ruy, FILTER_ANTIALIASING, bgcolor);
}
/**
* blur filter for a square part of the ymageMatrix
* @param lox x value for left upper coordinate
* @param loy y value for left upper coordinate
* @param rux x value for right lower coordinate
* @param ruy y value for right lower coordinate
* @author Marc Nause
*/
public void blur(int lox, int loy, int rux, int ruy) {
filter(lox, loy, rux, ruy, FILTER_BLUR, -1);
}
/**
* invert filter for a square part of the ymageMatrix
* @param lox x value for left upper coordinate
* @param loy y value for left upper coordinate
* @param rux x value for right lower coordinate
* @param ruy y value for right lower coordinate
* @author Marc Nause
*/
public void invert(int lox, int loy, int rux, int ruy) {
filter(lox, loy, rux, ruy, FILTER_INVERT, -1);
}
/**
* filter for a square part of the ymageMatrix
* @param lox x value for left upper coordinate
* @param loy y value for left upper coordinate
* @param rux x value for right lower coordinate
* @param ruy y value for right lower coordinate
* @param filter chooses filter
* @author Marc Nause
*/
private void filter(int lox, int loy, int rux, int ruy, byte filter, int bgcolor) {
// taking care that all values are legal
if (lox < 0) { lox = 0; }
if (loy < 0) { loy = 0; }
if (rux < 0) { rux = 0; }
if (ruy < 0) { ruy = 0; }
if (lox > width) { lox = width - 1; }
if (loy > height){ loy = height - 1; }
if (rux > width) { rux = width - 1; }
if (ruy > height){ ruy = height - 1; }
if (lox > rux) {
int tmp = lox;
lox = rux;
rux = tmp;
}
if (loy > ruy) {
int tmp = loy;
loy = ruy;
ruy = tmp;
}
int numberOfNeighbours = 0;
int rgbR = 0;
int rgbG = 0;
int rgbB = 0;
int rgb = 0;
int width2 = rux - lox + 1;
int height2 = ruy - loy + 1;
boolean border = false;
BufferedImage image2 = new BufferedImage(width2, height2, BufferedImage.TYPE_INT_RGB);
for (int i = lox; i < rux + 1; i++) {
for (int j = loy; j < ruy + 1; j++) {
numberOfNeighbours = 0;
rgbR = 0;
rgbG = 0;
rgbB = 0;
if (filter == FILTER_ANTIALIASING || filter == FILTER_BLUR) {
// taking samples from neighbours of pixel
if (i > lox) {
rgb = image.getRGB(i - 1, j);
if (rgb == bgcolor) {
border = true;
}
rgbR += rgb >> 16 & 0xff;
rgbG += rgb >> 8 & 0xff;
rgbB += rgb & 0xff;
numberOfNeighbours++;
}
if (j > loy) {
rgb = image.getRGB(i, j - 1);
if (rgb == bgcolor) {
border = true;
}
rgbR += rgb >> 16 & 0xff;
rgbG += rgb >> 8 & 0xff;
rgbB += rgb & 0xff;
numberOfNeighbours++;
}
if (i < width - 1) {
rgb = image.getRGB(i + 1, j);
if (rgb == bgcolor) {
border = true;
}
rgbR += rgb >> 16 & 0xff;
rgbG += rgb >> 8 & 0xff;
rgbB += rgb & 0xff;
numberOfNeighbours++;
}
if (i < height - 1) {
rgb = image.getRGB(i, j + 1);
if (rgb == bgcolor) {
border = true;
}
rgbR += rgb >> 16 & 0xff;
rgbG += rgb >> 8 & 0xff;
rgbB += rgb & 0xff;
numberOfNeighbours++;
}
}
rgb = image.getRGB(i, j);
// add value of pixel
// in case filter is used for antialiasing this will only be done if
// the pixel is on the edge to the background color
if ((filter == FILTER_ANTIALIASING && border) || (filter == FILTER_BLUR)) {
rgbR += (rgb >> 16 & 0xff);
rgbG += (rgb >> 8 & 0xff);
rgbB += (rgb & 0xff);
numberOfNeighbours++;
border = false;
}
// set to value of pixel => keep value
else if (filter == FILTER_ANTIALIASING) {
rgbR = (rgb >> 16 & 0xff);
rgbG = (rgb >> 8 & 0xff);
rgbB = (rgb & 0xff);
numberOfNeighbours = 1;
}
// set value of pixel to inverted value (using XOR)
else if (filter == FILTER_INVERT) {
rgb = rgb ^ 0xffffff;
rgbR = (rgb >> 16 & 0xff);
rgbG = (rgb >> 8 & 0xff);
rgbB = (rgb & 0xff);
numberOfNeighbours = 1;
}
// calculating the average
rgbR = (rgbR / numberOfNeighbours);
rgbG = (rgbG / numberOfNeighbours);
rgbB = (rgbB / numberOfNeighbours);
rgb = (rgbR << 16) | (rgbG << 8) | rgbB;
image2.setRGB(i-lox, j-loy, rgb);
}
}
// insert new version of area into image
insertBitmap(image2, lox, loy);
}
public static void demoPaint(ymageMatrix m) {
m.setColor(GREY);
m.line(0, 70, 100, 70); ymageToolPrint.print(m, 0, 65, 0, "Grey", -1);
m.line(65, 0, 65, 300);
m.setColor(RED);
m.line(0, 90, 100, 90); ymageToolPrint.print(m, 0, 85, 0, "Red", -1);
m.line(70, 0, 70, 300);
m.setColor(GREEN);
m.line(0, 110, 100, 110); ymageToolPrint.print(m, 0, 105, 0, "Green", -1);
m.line(75, 0, 75, 300);
m.setColor(BLUE);
m.line(0, 130, 100, 130); ymageToolPrint.print(m, 0, 125, 0, "Blue", -1);
m.line(80, 0, 80, 300);
}
/*
private static class imageBuffer {
protected BufferedImage image;
protected long access;
public imageBuffer(BufferedImage image) {
this.image = image;
this.access = System.currentTimeMillis();
}
public boolean sameSize(int width, int height) {
return (this.image.getWidth() == width) && (this.image.getHeight() == height);
}
public boolean olderThan(long timeout) {
return System.currentTimeMillis() - this.access > timeout;
}
}
private static final ArrayList imagePool = new ArrayList();
private static BufferedImage imageFromPool(int width, int height, long timeout) {
// returns an Image object from the image pool
// if the pooled Image was created recently (before timeout), it is not used
synchronized (imagePool) {
imageBuffer buffer;
for (int i = 0; i < imagePool.size(); i++) {
buffer = (imageBuffer) imagePool.get(i);
if ((buffer.sameSize(width, height)) && (buffer.olderThan(timeout))) {
// use this buffer
System.out.println("### using imageBuffer from pool " + i);
buffer.access = System.currentTimeMillis();
return buffer.image;
}
}
// no buffered image found, create a new one
buffer = new imageBuffer(new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB));
imagePool.add(buffer);
return buffer.image;
}
}
*/
private static class sbbBuffer {
protected serverByteBuffer buffer;
protected int pixel;
protected long access;
public sbbBuffer(int width, int height) {
this.buffer = new serverByteBuffer();
this.access = System.currentTimeMillis();
this.pixel = width * height;
}
public boolean enoughSize(int width, int height) {
return this.pixel >= width * height;
}
public boolean olderThan(long timeout) {
return System.currentTimeMillis() - this.access > timeout;
}
}
private static final ArrayList<sbbBuffer> sbbPool = new ArrayList<sbbBuffer>();
private static serverByteBuffer sbbFromPool(int width, int height, long timeout) {
// returns an Image object from the image pool
// if the pooled Image was created recently (before timeout), it is not used
synchronized (sbbPool) {
sbbBuffer b;
for (int i = 0; i < sbbPool.size(); i++) {
b = sbbPool.get(i);
if ((b.enoughSize(width, height)) && (b.olderThan(timeout))) {
// use this buffer
b.access = System.currentTimeMillis();
b.buffer.clear(); // this makes only sense if the byteBuffer keeps its buffer
return b.buffer;
}
}
// no buffered image found, create a new one
b = new sbbBuffer(width, height);
sbbPool.add(b);
return b.buffer;
}
}
public static serverByteBuffer exportImage(BufferedImage image, String targetExt) {
// generate an byte array from the given image
//serverByteBuffer baos = new serverByteBuffer();
serverByteBuffer baos = sbbFromPool(image.getWidth(), image.getHeight(), 1000);
try {
ImageIO.write(image, targetExt, baos);
return baos;
} catch (IOException e) {
// should not happen
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
// go into headless awt mode
System.setProperty("java.awt.headless", "true");
ymageMatrix m = new ymageMatrix(200, 300, MODE_SUB, "FFFFFF");
demoPaint(m);
File file = new File("/Users/admin/Desktop/testimage.png");
try {
FileOutputStream fos = new FileOutputStream(file);
ImageIO.write(m.getImage(), "png", fos);
fos.close();
} catch (IOException e) {}
// open file automatically, works only on Mac OS X
/*
Process p = null;
try {
p = Runtime.getRuntime().exec(new String[] {"/usr/bin/osascript", "-e", "open \"" + args[0] + "\""});
} catch (java.io.IOException e) {
e.printStackTrace();
}
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
}
}