-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cs
More file actions
458 lines (409 loc) · 8.62 KB
/
Image.cs
File metadata and controls
458 lines (409 loc) · 8.62 KB
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
using System;
using System.Threading;
using UnityEngine;
public class Image
{
private const int INTERVAL = 5;
private const int MAXTIME = 500;
public Texture2D texture = new Texture2D(1, 1);
public static Image imgTemp;
public static string filenametemp;
public static byte[] datatemp;
public static Image imgSrcTemp;
public static int xtemp;
public static int ytemp;
public static int wtemp;
public static int htemp;
public static int transformtemp;
public int w;
public int h;
public static int status;
public Color colorBlend = Color.black;
public static Image createEmptyImage()
{
return __createEmptyImage();
}
public static Image createImage(string filename)
{
return __createImage(filename);
}
public static Image createImage(byte[] imageData)
{
return __createImage(imageData);
}
public static Image createImage(Image src, int x, int y, int w, int h, int transform)
{
return __createImage(src, x, y, w, h, transform);
}
public static Image createImage(int w, int h)
{
return __createImage(w, h);
}
public static Image createImage(Image img)
{
Image image = createImage(img.w, img.h);
image.texture = img.texture;
image.texture.Apply();
return image;
}
public static Image createImage(sbyte[] imageData, int offset, int lenght)
{
if (offset + lenght > imageData.Length)
{
return null;
}
byte[] array = new byte[lenght];
for (int i = 0; i < lenght; i++)
{
array[i] = convertSbyteToByte(imageData[i + offset]);
}
return createImage(array);
}
public static byte convertSbyteToByte(sbyte var)
{
if (var > 0)
{
return (byte)var;
}
return (byte)(var + 256);
}
public static byte[] convertArrSbyteToArrByte(sbyte[] var)
{
byte[] array = new byte[var.Length];
for (int i = 0; i < var.Length; i++)
{
if (var[i] > 0)
{
array[i] = (byte)var[i];
}
else
{
array[i] = (byte)(var[i] + 256);
}
}
return array;
}
public static Image createRGBImage(int[] rbg, int w, int h, bool bl)
{
Image image = createImage(w, h);
Color[] array = new Color[rbg.Length];
for (int i = 0; i < array.Length; i++)
{
ref Color reference = ref array[i];
reference = setColorFromRBG(rbg[i]);
}
image.texture.SetPixels(0, 0, w, h, array);
image.texture.Apply();
return image;
}
public static Color setColorFromRBG(int rgb)
{
int num = rgb & 0xFF;
int num2 = (rgb >> 8) & 0xFF;
int num3 = (rgb >> 16) & 0xFF;
float b = (float)num / 256f;
float g = (float)num2 / 256f;
float r = (float)num3 / 256f;
return new Color(r, g, b);
}
public static void update()
{
if (status == 2)
{
status = 1;
imgTemp = __createEmptyImage();
status = 0;
}
else if (status == 3)
{
status = 1;
imgTemp = __createImage(filenametemp);
status = 0;
}
else if (status == 4)
{
status = 1;
imgTemp = __createImage(datatemp);
status = 0;
}
else if (status == 5)
{
status = 1;
imgTemp = __createImage(imgSrcTemp, xtemp, ytemp, wtemp, htemp, transformtemp);
status = 0;
}
else if (status == 6)
{
status = 1;
imgTemp = __createImage(wtemp, htemp);
status = 0;
}
}
private static Image _createEmptyImage()
{
if (status != 0)
{
Cout.LogError("CANNOT CREATE EMPTY IMAGE WHEN CREATING OTHER IMAGE");
return null;
}
imgTemp = null;
status = 2;
int i;
for (i = 0; i < 500; i++)
{
Thread.Sleep(5);
if (status == 0)
{
break;
}
}
if (i == 500)
{
Cout.LogError("TOO LONG FOR CREATE EMPTY IMAGE");
status = 0;
}
return imgTemp;
}
private static Image _createImage(string filename)
{
if (status != 0)
{
Cout.LogError("CANNOT CREATE IMAGE " + filename + " WHEN CREATING OTHER IMAGE");
return null;
}
imgTemp = null;
filenametemp = filename;
status = 3;
int i;
for (i = 0; i < 500; i++)
{
Thread.Sleep(5);
if (status == 0)
{
break;
}
}
if (i == 500)
{
Cout.LogError("TOO LONG FOR CREATE IMAGE " + filename);
status = 0;
}
return imgTemp;
}
private static Image _createImage(byte[] imageData)
{
if (status != 0)
{
Cout.LogError("CANNOT CREATE IMAGE(FromArray) WHEN CREATING OTHER IMAGE");
return null;
}
imgTemp = null;
datatemp = imageData;
status = 4;
int i;
for (i = 0; i < 500; i++)
{
Thread.Sleep(5);
if (status == 0)
{
break;
}
}
if (i == 500)
{
Cout.LogError("TOO LONG FOR CREATE IMAGE(FromArray)");
status = 0;
}
return imgTemp;
}
private static Image _createImage(Image src, int x, int y, int w, int h, int transform)
{
if (status != 0)
{
Cout.LogError("CANNOT CREATE IMAGE(FromSrcPart) WHEN CREATING OTHER IMAGE");
return null;
}
imgTemp = null;
imgSrcTemp = src;
xtemp = x;
ytemp = y;
wtemp = w;
htemp = h;
transformtemp = transform;
status = 5;
int i;
for (i = 0; i < 500; i++)
{
Thread.Sleep(5);
if (status == 0)
{
break;
}
}
if (i == 500)
{
Cout.LogError("TOO LONG FOR CREATE IMAGE(FromSrcPart)");
status = 0;
}
return imgTemp;
}
private static Image _createImage(int w, int h)
{
if (status != 0)
{
Cout.LogError("CANNOT CREATE IMAGE(w,h) WHEN CREATING OTHER IMAGE");
return null;
}
imgTemp = null;
wtemp = w;
htemp = h;
status = 6;
int i;
for (i = 0; i < 500; i++)
{
Thread.Sleep(5);
if (status == 0)
{
break;
}
}
if (i == 500)
{
Cout.LogError("TOO LONG FOR CREATE IMAGE(w,h)");
status = 0;
}
return imgTemp;
}
public static byte[] loadData(string filename)
{
Image image = new Image();
TextAsset textAsset = (TextAsset)Resources.Load(filename, typeof(TextAsset));
if (textAsset == null || textAsset.bytes == null || textAsset.bytes.Length == 0)
{
throw new Exception("NULL POINTER EXCEPTION AT Image __createImage " + filename);
}
sbyte[] array = ArrayCast.cast(textAsset.bytes);
Debug.LogError("CHIEU DAI MANG BYTE IMAGE CREAT = " + array.Length);
return textAsset.bytes;
}
private static Image __createImage(string filename)
{
Image image = new Image();
Texture2D texture2D = Resources.Load(filename) as Texture2D;
if (texture2D == null)
{
throw new Exception("NULL POINTER EXCEPTION AT Image __createImage " + filename);
}
image.texture = texture2D;
image.w = image.texture.width;
image.h = image.texture.height;
setTextureQuality(image);
return image;
}
private static Image __createImage(byte[] imageData)
{
if (imageData == null || imageData.Length == 0)
{
Cout.LogError("Create Image from byte array fail");
return null;
}
Image image = new Image();
try
{
image.texture.LoadImage(imageData);
image.w = image.texture.width;
image.h = image.texture.height;
setTextureQuality(image);
}
catch (Exception)
{
Cout.LogError("CREAT IMAGE FROM ARRAY FAIL \n" + Environment.StackTrace);
}
return image;
}
private static Image __createImage(Image src, int x, int y, int w, int h, int transform)
{
Image image = new Image();
image.texture = new Texture2D(w, h);
y = src.texture.height - y - h;
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
int num = i;
if (transform == 2)
{
num = w - i;
}
int num2 = j;
image.texture.SetPixel(i, j, src.texture.GetPixel(x + num, y + num2));
}
}
image.texture.Apply();
image.w = image.texture.width;
image.h = image.texture.height;
setTextureQuality(image);
return image;
}
private static Image __createEmptyImage()
{
return new Image();
}
public static Image __createImage(int w, int h)
{
Image image = new Image();
image.texture = new Texture2D(w, h, TextureFormat.RGBA32, mipmap: false);
setTextureQuality(image);
image.w = w;
image.h = h;
image.texture.Apply();
return image;
}
public static int getImageWidth(Image image)
{
return image.getWidth();
}
public static int getImageHeight(Image image)
{
return image.getHeight();
}
public int getWidth()
{
return w / mGraphics.zoomLevel;
}
public int getHeight()
{
return h / mGraphics.zoomLevel;
}
private static void setTextureQuality(Image img)
{
setTextureQuality(img.texture);
}
public static void setTextureQuality(Texture2D texture)
{
texture.anisoLevel = 0;
texture.filterMode = FilterMode.Point;
texture.mipMapBias = 0f;
texture.wrapMode = TextureWrapMode.Clamp;
}
public Color[] getColor()
{
return texture.GetPixels();
}
public int getRealImageWidth()
{
return w;
}
public int getRealImageHeight()
{
return h;
}
public void getRGB(ref int[] data, int x1, int x2, int x, int y, int w, int h)
{
Color[] pixels = texture.GetPixels(x, this.h - 1 - y, w, h);
for (int i = 0; i < pixels.Length; i++)
{
data[i] = mGraphics.getIntByColor(pixels[i]);
}
}
}