11package com .bobrust .generator ;
22
33class BorstCore {
4- static BorstColor computeColor (BorstImage target , BorstImage current , Scanline [] lines , int alpha ) {
4+ static BorstColor computeColor (BorstImage target , BorstImage current , int alpha , int size , int x_offset , int y_offset ) {
55 long rsum_1 = 0 ;
66 long gsum_1 = 0 ;
77 long bsum_1 = 0 ;
8-
8+
99 long rsum_2 = 0 ;
1010 long gsum_2 = 0 ;
1111 long bsum_2 = 0 ;
12-
12+
1313 int count = 0 ;
1414 int w = target .width ;
15+ int h = target .height ;
1516
17+ final Scanline [] lines = CircleCache .CIRCLE_CACHE [size ];
1618 final int len = lines .length ;
17- for (int i = 0 ; i < len ; i ++) {
19+ for (int i = 0 ; i < len ; i ++) {
1820 Scanline line = lines [i ];
19- int idx = line .y * w ;
20-
21- for (int x = line .x1 ; x <= line .x2 ; x ++) {
21+ int y = line .y + y_offset ;
22+ if (y < 0 || y >= h ) {
23+ continue ;
24+ }
25+
26+ int xs = Math .max (line .x1 + x_offset , 0 );
27+ int xe = Math .min (line .x2 + x_offset , w - 1 );
28+ int idx = y * w ;
29+
30+ for (int x = xs ; x <= xe ; x ++) {
2231 int tt = target .pixels [idx + x ];
2332 int cc = current .pixels [idx + x ];
2433
@@ -30,7 +39,7 @@ static BorstColor computeColor(BorstImage target, BorstImage current, Scanline[]
3039 gsum_2 += (cc >>> 8 ) & 0xff ;
3140 bsum_2 += (cc ) & 0xff ;
3241 }
33-
42+
3443 count += (line .x2 - line .x1 + 1 );
3544 }
3645
@@ -48,30 +57,30 @@ static BorstColor computeColor(BorstImage target, BorstImage current, Scanline[]
4857
4958 return BorstUtils .getClosestColor ((alpha << 24 ) | (r << 16 ) | (g << 8 ) | (b ));
5059 }
51-
52- static void copyLinesReplaceRegion (BorstImage dst , BorstImage src , Scanline [] lines ) {
53- int w = dst .width ;
54- int len = lines .length ;
55- for (int i = 0 ; i < len ; i ++) {
56- Scanline line = lines [i ];
57- int idx = line .x1 + line .y * w ;
58-
59- System .arraycopy (src .pixels , idx , dst .pixels , idx , line .x2 - line .x1 + 1 );
60- }
61- }
62-
63- static void drawLines (BorstImage im , BorstColor c , Scanline [] lines , int alpha ) {
60+
61+ static void drawLines (BorstImage im , BorstColor c , int alpha , int size , int x_offset , int y_offset ) {
6462 int cr = c .r * alpha ;
6563 int cg = c .g * alpha ;
6664 int cb = c .b * alpha ;
6765 int pa = 255 - alpha ;
6866 int w = im .width ;
67+ int h = im .height ;
68+
69+ final Scanline [] lines = CircleCache .CIRCLE_CACHE [size ];
6970 final int len = lines .length ;
70- for (int i = 0 ; i < len ; i ++) {
71+ for (int i = 0 ; i < len ; i ++) {
7172 Scanline line = lines [i ];
73+ int y = line .y + y_offset ;
74+ if (y < 0 || y >= h ) {
75+ continue ;
76+ }
77+
78+ int xs = Math .max (line .x1 + x_offset , 0 );
79+ int xe = Math .min (line .x2 + x_offset , w - 1 );
80+ int idx = y * w ;
7281
73- for (int x = line . x1 ; x <= line . x2 ; x ++) {
74- int a = im .pixels [line . y * w + x ];
82+ for (int x = xs ; x <= xe ; x ++) {
83+ int a = im .pixels [idx + x ];
7584 int a_a = (a >>> 24 ) & 0xff ;
7685 int a_r = (a >>> 16 ) & 0xff ;
7786 int a_g = (a >>> 8 ) & 0xff ;
@@ -82,7 +91,7 @@ static void drawLines(BorstImage im, BorstColor c, Scanline[] lines, int alpha)
8291 int ab = (cb + (a_b * pa )) >>> 8 ;
8392 int aa = 255 - (((255 - a_a ) * pa ) >>> 8 );
8493
85- im .pixels [line . y * w + x ] = (aa << 24 ) | (ar << 16 ) | (ag << 8 ) | (ab );
94+ im .pixels [idx + x ] = (aa << 24 ) | (ar << 16 ) | (ag << 8 ) | (ab );
8695 }
8796 }
8897 }
@@ -119,18 +128,26 @@ static float differenceFull(BorstImage a, BorstImage b) {
119128 return (float )(Math .sqrt (total / (w * h * 4.0 )) / 255.0 );
120129 }
121130
122- static float differencePartial (BorstImage target , BorstImage before , BorstImage after , float score , Scanline [] lines ) {
131+ static float differencePartial (BorstImage target , BorstImage before , BorstImage after , float score , int size , int x_offset , int y_offset ) {
123132 int w = target .width ;
124133 int h = target .height ;
125134 double denom = (w * h * 4.0 );
126135 long total = (long )(Math .pow (score * 255 , 2 ) * denom );
127136
137+ final Scanline [] lines = CircleCache .CIRCLE_CACHE [size ];
128138 final int len = lines .length ;
129- for (int i = 0 ; i < len ; i ++) {
139+ for (int i = 0 ; i < len ; i ++) {
130140 Scanline line = lines [i ];
131- int idx = line .y * w ;
141+ int y = line .y + y_offset ;
142+ if (y < 0 || y >= h ) {
143+ continue ;
144+ }
145+
146+ int xs = Math .max (line .x1 + x_offset , 0 );
147+ int xe = Math .min (line .x2 + x_offset , w - 1 );
148+ int idx = y * w ;
132149
133- for (int x = line . x1 ; x <= line . x2 ; x ++) {
150+ for (int x = xs ; x <= xe ; x ++) {
134151 int tt = target .pixels [idx + x ];
135152 int bb = before .pixels [idx + x ];
136153 int aa = after .pixels [idx + x ];
@@ -168,10 +185,9 @@ static float differencePartial(BorstImage target, BorstImage before, BorstImage
168185 return (float )(Math .sqrt (total / denom ) / 255.0 );
169186 }
170187
171- static float differencePartialThread (BorstImage target , BorstImage before , float score , int alpha , Scanline [] lines ) {
172- BorstColor color = BorstCore .computeColor (target , before , lines , alpha );
188+ static float differencePartialThread (BorstImage target , BorstImage before , float score , int alpha , int size , int x_offset , int y_offset ) {
189+ BorstColor color = BorstCore .computeColor (target , before , alpha , size , x_offset , y_offset );
173190
174- final int len = lines .length ;
175191 final int h = target .height ;
176192 final int w = target .width ;
177193
@@ -183,11 +199,21 @@ static float differencePartialThread(BorstImage target, BorstImage before, float
183199 final int cb = color .b * alpha ;
184200 final int pa = 255 - alpha ;
185201
186- for (int i = 0 ; i < len ; i ++) {
202+ final Scanline [] lines = CircleCache .CIRCLE_CACHE [size ];
203+ final int len = lines .length ;
204+
205+ for (int i = 0 ; i < len ; i ++) {
187206 Scanline line = lines [i ];
188- int idx = line .y * w ;
207+ int y = line .y + y_offset ;
208+ if (y < 0 || y >= h ) {
209+ continue ;
210+ }
211+
212+ int xs = Math .max (line .x1 + x_offset , 0 );
213+ int xe = Math .min (line .x2 + x_offset , w - 1 );
214+ int idx = y * w ;
189215
190- for (int x = line . x1 ; x <= line . x2 ; x ++) {
216+ for (int x = xs ; x <= xe ; x ++) {
191217 int tt = target .pixels [idx + x ];
192218 int bb = before .pixels [idx + x ];
193219
0 commit comments