-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatImage.cs
100 lines (85 loc) · 3.13 KB
/
FloatImage.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
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ColorMatch3D
{
class FloatImage
{
public float[] imageData = new float[0];
public int stride=0;
public int width=0, height=0;
public PixelFormat pixelFormat;
public FloatImage(float[] imageDataA, int strideA, int widthA, int heightA, PixelFormat pixelFormatA)
{
imageData = imageDataA;
stride = strideA;
width = widthA;
height = heightA;
pixelFormat = pixelFormatA;
}
public int Length
{
get { return imageData.Length; }
}
public float this[int index]
{
get
{
return imageData[index];
}
set
{
imageData[index] = value;
}
}
public ByteImage ToByteImage()
{
float[] inputImageData = imageData;
byte[] output = new byte[inputImageData.Length];
int strideHere = 0;
int xX4;
int offsetHere;
for (int y = 0; y < height; y++)
{
strideHere = stride * y;
for (int x = 0; x < width; x++) // 4 bc RGBA
{
xX4 = x * 4;
offsetHere = strideHere + xX4;
output[offsetHere] = (byte)Math.Max(0,Math.Min(255,inputImageData[offsetHere]));
output[offsetHere + 1] = (byte)Math.Max(0, Math.Min(255, inputImageData[offsetHere +1 ]));
output[offsetHere + 2] = (byte)Math.Max(0, Math.Min(255, inputImageData[offsetHere + 2]));
output[offsetHere + 3] = (byte)Math.Max(0, Math.Min(255, inputImageData[offsetHere + 3]));
}
}
return new ByteImage(output, stride, width, height, pixelFormat);
}
static public FloatImage FromByteImage(ByteImage inputImage)
{
byte[] inputImageData = inputImage.imageData;
int width = inputImage.width, height = inputImage.height, stride = inputImage.stride;
PixelFormat pixelFormat = inputImage.pixelFormat;
float[] output = new float[inputImageData.Length];
int strideHere = 0;
int xX4;
int offsetHere;
for (int y = 0; y < height; y++)
{
strideHere = stride * y;
for (int x = 0; x < width; x++) // 4 bc RGBA
{
xX4 = x * 4;
offsetHere = strideHere + xX4;
output[offsetHere] = (float)inputImageData[offsetHere];
output[offsetHere + 1] = (float)inputImageData[offsetHere +1];
output[offsetHere + 2] = (float)inputImageData[offsetHere +2 ];
output[offsetHere + 3] = (float)inputImageData[offsetHere +3];
}
}
return new FloatImage(output, stride, width, height, pixelFormat);
}
}
}