Libraryless. Click here for Pure Java version (10815L/62K).
1 | // The main idea behind this data structure is that it should |
2 | // allow finding regions significantly faster than directly from |
3 | // the Hi15Image. |
4 | // |
5 | // Making this index takes about 2ms per megapixel. |
6 | |
7 | sclass Hi15ScanlineIndex { |
8 | Hi15Image image; |
9 | int w, h; |
10 | int[] indexed; |
11 | |
12 | // scanlines contains 2 consecutive ints for each scanline: |
13 | // -index of first pixel in scanline |
14 | // -index of last pixel in scanline + 1 |
15 | // |
16 | // Colors are not stored and have to be gathered from the |
17 | // original image. |
18 | |
19 | IntBuffer scanlines; |
20 | |
21 | *() {} |
22 | *(Hi15Image *image) {} |
23 | |
24 | run { |
25 | int w = this.w = image.getWidth(); |
26 | int h = this.h = image.getHeight(); |
27 | short[] pixels = image.pixels; |
28 | |
29 | int lineStart = 0; |
30 | var indexed = this.indexed = new int[w*h]; |
31 | int guess = 128*1024; |
32 | var scanlines = this.scanlines = new IntBuffer(guess); |
33 | |
34 | for y to h: { |
35 | int i = lineStart; |
36 | int lineEnd = lineStart+w; |
37 | |
38 | while (i < lineEnd) { |
39 | short color = pixels[i]; |
40 | int j = i+1; |
41 | if (j < lineEnd && pixels[j] == color) { |
42 | // multi-pixel scanline found |
43 | |
44 | do |
45 | ++j; |
46 | while (j < lineEnd && pixels[j] == color); |
47 | scanlines.add(i); |
48 | scanlines.add(j); |
49 | } |
50 | |
51 | int iScanline = scanlines.size(); |
52 | while (i < j) |
53 | indexed[i++] = iScanline; |
54 | } |
55 | |
56 | lineStart = lineEnd; |
57 | } |
58 | } |
59 | |
60 | int nScanlines() { ret scanlines.size()/2; } |
61 | |
62 | // Render all scanlines in an image. |
63 | // Isolated pixels (those different from their left and right |
64 | // neighbor) will be left transparent. |
65 | |
66 | BufferedImage scanlinesAsImage() { |
67 | int[] pixels = new[w*h]; |
68 | int n = nScanlines(); |
69 | for i to n: { |
70 | int from = scanlines.get(i*2); |
71 | int to = scanlines.get(i*2+1); |
72 | int color = withFullAlpha(hi15ToRGBInt_clean(image.getHi15Pixel_noRangeCheck(from))); |
73 | for (int pixel = from; pixel < to; pixel++) |
74 | pixels[pixel] = color; |
75 | } |
76 | ret bufferedImageWithAlpha(w, h, pixels); |
77 | } |
78 | |
79 | // run this after run() to save some memory |
80 | void compact { |
81 | scanlines.trimToSize(); |
82 | } |
83 | } |
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035898 |
Snippet name: | Hi15ScanlineIndex - a fast index of all single-color multi-pixel horizontal lines in an image |
Eternal ID of this version: | #1035898/14 |
Text MD5: | 3c448de8d75bf392caa3488669c2a59b |
Transpilation MD5: | c4ca45f9ae0462afbf3d23a1adc65397 |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-08-11 18:57:00 |
Source code size: | 2226 bytes / 83 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 154 / 264 |
Version history: | 13 change(s) |
Referenced in: | [show references] |