forked from leap71/PicoGK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PicoGK_Image.cs
448 lines (382 loc) · 12.5 KB
/
PicoGK_Image.cs
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
//
// SPDX-License-Identifier: Apache-2.0
//
// PicoGK ("peacock") is a compact software kernel for computational geometry,
// specifically for use in Computational Engineering Models (CEM).
//
// For more information, please visit https://picogk.org
//
// PicoGK is developed and maintained by LEAP 71 - © 2023 by LEAP 71
// https://leap71.com
//
// Computational Engineering will profoundly change our physical world in the
// years ahead. Thank you for being part of the journey.
//
// We have developed this library to be used widely, for both commercial and
// non-commercial projects alike. Therefore, we have released it under a
// permissive open-source license.
//
// The foundation of PicoGK is a thin layer on top of the powerful open-source
// OpenVDB project, which in turn uses many other Free and Open Source Software
// libraries. We are grateful to be able to stand on the shoulders of giants.
//
// LEAP 71 licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, THE SOFTWARE IS
// PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PicoGK
{
public abstract partial class Image
{
public enum EType
{
BW,
GRAY,
COLOR
};
public readonly int nWidth;
public readonly int nHeight;
public readonly EType eType;
public abstract ColorFloat clrValue(int x, int y);
public abstract float fValue(int x, int y);
public abstract bool bValue(int x, int y);
public abstract void SetValue(int x, int y, in ColorFloat clr);
public abstract void SetValue(int x, int y, float fGray);
public abstract void SetValue(int x, int y, bool bValue);
public byte byGetValue(int x, int y)
{
return (byte)(Math.Clamp(fValue(x, y), 0.0f, 1.0f) * 255.0f);
}
public void SetValue(int x, int y, byte byValue)
{
SetValue(x, y, byValue / 255.0f);
}
public ColorBgr24 sGetBgr24(int x, int y)
{
ColorFloat clr = clrValue(x, y);
ColorBgr24 sClr;
sClr.B = (byte)(Math.Clamp(clr.B, 0.0f, 1.0f) * 255.0f);
sClr.G = (byte)(Math.Clamp(clr.G, 0.0f, 1.0f) * 255.0f);
sClr.R = (byte)(Math.Clamp(clr.R, 0.0f, 1.0f) * 255.0f);
return sClr;
}
public void SetBgr24(int x, int y, ColorBgr24 sClr)
{
SetValue(x, y, new ColorFloat(sClr));
}
public ColorRgb24 sGetRgb24(int x, int y)
{
ColorFloat clr = clrValue(x, y);
ColorRgb24 sClr;
sClr.R = (byte)(Math.Clamp(clr.R, 0.0f, 1.0f) * 255.0f);
sClr.G = (byte)(Math.Clamp(clr.G, 0.0f, 1.0f) * 255.0f);
sClr.B = (byte)(Math.Clamp(clr.B, 0.0f, 1.0f) * 255.0f);
return sClr;
}
public void SetRgb24(int x, int y, ColorRgb24 sClr)
{
SetValue(x, y, new ColorFloat(sClr));
}
public void DrawLine(int x0, int y0, int x1, int y1, ColorFloat clr)
{
int dx = Math.Abs(x1 - x0);
int dy = Math.Abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;
while (true)
{
SetValue(x0, y0, clr);
if (x0 == x1 && y0 == y1)
{
break;
}
int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
x0 += sx;
}
if (e2 < dx)
{
err += dx;
y0 += sy;
}
}
}
public void DrawLine(int x0, int y0, int x1, int y1, float fGrayscale)
{
int dx = Math.Abs(x1 - x0);
int dy = Math.Abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;
while (true)
{
SetValue(x0, y0, fGrayscale);
if (x0 == x1 && y0 == y1)
{
break;
}
int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
x0 += sx;
}
if (e2 < dx)
{
err += dx;
y0 += sy;
}
}
}
public void DrawLine(int x0, int y0, int x1, int y1, bool bValue)
{
int dx = Math.Abs(x1 - x0);
int dy = Math.Abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;
while (true)
{
SetValue(x0, y0, bValue);
if (x0 == x1 && y0 == y1)
{
break;
}
int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
x0 += sx;
}
if (e2 < dx)
{
err += dx;
y0 += sy;
}
}
}
protected Image( int _nWidth,
int _nHeight,
EType _eType)
{
Debug.Assert(_nWidth > 0);
Debug.Assert(_nHeight > 0);
nWidth = _nWidth;
nHeight = _nHeight;
eType = _eType;
}
}
public abstract partial class ImageBWAbstract : Image
{
public ImageBWAbstract( int _nWidth,
int _nHeight)
: base(_nWidth,
_nHeight,
EType.BW)
{
}
public override float fValue(int x, int y)
{
return bValue(x, y) ? 1.0f : 0.0f;
}
public override ColorFloat clrValue(int x, int y)
{
return new ColorFloat(bValue(x, y) ? 1.0f : 0.0f);
}
public override void SetValue(int x, int y, float fValue)
{
SetValue(x, y, fValue > 0.5f);
}
public override void SetValue(int x, int y, in ColorFloat clr)
{
SetValue(x, y, ((clr.R + clr.G + clr.B) / 3.0f) > 0.5f);
}
}
public abstract partial class ImageGrayscaleAbstract : Image
{
public ImageGrayscaleAbstract( int _nWidth,
int _nHeight)
: base(_nWidth,
_nHeight,
EType.GRAY)
{
}
public override ColorFloat clrValue(int x, int y)
{
float f = fValue(x, y);
return new ColorFloat(f, f, f);
}
public override bool bValue(int x, int y)
{
return fValue(x, y) > 0.5f;
}
public override void SetValue(int x, int y, bool bValue)
{
SetValue(x, y, bValue ? 1.0f : 0.0f);
}
public override void SetValue(int x, int y, in ColorFloat clr)
{
SetValue(x, y, (clr.R + clr.G + clr.B) / 3.0f);
}
}
public abstract partial class ImageColorAbstract : Image
{
public ImageColorAbstract( int _iWidth,
int _iHeight)
: base( _iWidth,
_iHeight,
EType.COLOR)
{
}
public override float fValue(int x, int y)
{
ColorFloat clr = clrValue(x, y);
return (clr.R + clr.G + clr.B) / 3.0f;
}
public override bool bValue(int x, int y)
{
return fValue(x, y) > 0.5f;
}
public override void SetValue(int x, int y, float f)
{
SetValue(x, y, new ColorFloat(f));
}
public override void SetValue(int x, int y, bool bValue)
{
SetValue(x, y, new ColorFloat(bValue ? 1.0f : 0.0f));
}
}
public partial class ImageGrayScale : ImageGrayscaleAbstract
{
public ImageGrayScale( int _nWidth,
int _nHeight)
: base( _nWidth,
_nHeight)
{
m_afValues = new float[nWidth * nHeight];
}
public override void SetValue(int x, int y, float fGray)
{
if ( (x < 0) ||
(y < 0) ||
(x >= nWidth) ||
(y >= nHeight))
{
return;
}
m_afValues[x + (y * nWidth)] = fGray;
}
public override float fValue(int x, int y)
{
if ( (x < 0) ||
(y < 0) ||
(x >= nWidth) ||
(y >= nHeight))
{
return 0.0f;
}
return m_afValues[x + (y * nWidth)];
}
public ImageColor imgGetColorCodedSDF(float fBackground)
{
ImageColor img = new(nWidth, nHeight);
ColorFloat clrInsideBackground = new("006600");
ColorFloat clrOutsideBackground = new("00");
for (int x=0; x<nWidth; x++)
{
for (int y = 0; y < nHeight; y++)
{
float fV = fValue(x, y);
if (fV < 0f)
{
// Inside
if (fV <= -fBackground)
img.SetValue(x, y, clrInsideBackground);
else
{
fV /= -fBackground;
img.SetValue(x, y, new ColorFloat(0f, 1f - fV, 0f));
}
}
else
{
// Outside
if (fV >= fBackground)
img.SetValue(x, y, clrOutsideBackground);
else
{
fV /= fBackground;
img.SetValue(x, y, new ColorFloat(1f - fV, 1f - fV, 1f - fV));
}
}
}
}
return img;
}
public static ImageGrayScale imgGetInterpolated( ImageGrayScale oImg1,
ImageGrayScale oImg2,
float fWeight = 0.5f)
{
Debug.Assert(oImg1.nWidth == oImg2.nWidth);
Debug.Assert(oImg1.nHeight == oImg2.nHeight);
if (fWeight <= 0.0f)
return oImg1;
if (fWeight >= 1.0f)
return oImg2;
float fFac1 = 1.0f - fWeight;
ImageGrayScale oNew = new(oImg1.nWidth, oImg1.nHeight);
for (int n=0; n<oNew.nWidth * oNew.nHeight; n++)
{
oNew.m_afValues[n] = (fFac1 * oImg1.m_afValues[n]) + (fWeight * oImg2.m_afValues[n]);
}
return oNew;
}
public float[] m_afValues;
}
public partial class ImageColor : ImageColorAbstract
{
public ImageColor( int _nWidth,
int _nHeight)
: base( _nWidth,
_nHeight)
{
m_aclrValues = new ColorFloat[nWidth * nHeight];
}
public override void SetValue(int x, int y, in ColorFloat clr)
{
if ( (x < 0) ||
(y < 0) ||
(x >= nWidth) ||
(y >= nHeight))
{
return;
}
m_aclrValues[x + (y * nWidth)] = clr;
}
public override ColorFloat clrValue(int x, int y)
{
if ( (x < 0) ||
(y < 0) ||
(x >= nWidth) ||
(y >= nHeight))
{
return new ColorFloat(0, 0, 0);
}
return m_aclrValues[x + (y * nWidth)];
}
ColorFloat[] m_aclrValues;
}
}