Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 1310a67

Browse files
committed
clean code in load()
1 parent 13a3153 commit 1310a67

2 files changed

Lines changed: 33 additions & 16 deletions

File tree

png_img/encoded_lena_1.png

0 Bytes
Loading

source/img4d/img4d.d

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ class Img4d
255255
{
256256
Header header;
257257

258-
bool isColorNoneAlpha(int colorType)
258+
bool isAlpha(int colorType)
259259
{
260260
alias type = colorType;
261261
with (colorTypes)
262262
{
263-
return (type == trueColor || type == indexColor) ? true : false;
263+
return (type == trueColor || type == indexColor) ? false : true;
264264
}
265265
}
266266

@@ -287,19 +287,37 @@ class Img4d
287287
alias grayscale = data;
288288
return Pixel(grayscale);
289289
}
290+
ubyte[][] R;
291+
ubyte[][] G;
292+
ubyte[][] B;
293+
ubyte[][] A;
294+
R.length = data.length;
295+
G.length = data.length;
296+
B.length = data.length;
297+
A.length = data.length;
298+
bool isAlpha = this.isAlpha(this.header.colorType);
290299

291-
data.each!(a => rgb ~= [a.chunks(lengthPerPixel).array]);
292-
rgb.each!(a => joinRGB ~= a.joinVertical);
293-
auto pix = joinRGB.transposed;
294-
ubyte[][] R = pix[R].array.to!(ubyte[][]);
295-
ubyte[][] G = pix[G].array.to!(ubyte[][]);
296-
ubyte[][] B = pix[B].array.to!(ubyte[][]);
297-
ubyte[][] A = pix[A].array.to!(ubyte[][]);
300+
for (int i = 0; i < data.length; i++)
301+
{
302+
ulong len = data[i].length;
303+
while (data[i].length > 0)
304+
{
305+
R[i] ~= data[i][0];
306+
G[i] ~= data[i][1];
307+
B[i] ~= data[i][2];
308+
data[i] = data[i][3 .. $];
309+
if (isAlpha)
310+
{
311+
A[i] ~= data[i][0];
312+
data[i] = data[i][1 .. $];
313+
}
314+
}
315+
}
298316

299-
return (this.isColorNoneAlpha(this.header.colorType)) ? Pixel(R, G, B) : Pixel(R, G, B, A);
317+
return (isAlpha) ? Pixel(R, G, B, A) : Pixel(R, G, B);
300318
}
301319

302-
bool save(ref Pixel pix, string filename)
320+
bool save(ref Pixel pix, in string filename)
303321
{
304322

305323
Encode encode = new Encode(this.header, pix);
@@ -311,7 +329,7 @@ class Img4d
311329
return true;
312330
}
313331

314-
bool save(ref Pixel pix, string filename, ubyte[] ancillary_chunks)
332+
bool save(ref Pixel pix, in string filename, in ubyte[] ancillary_chunks)
315333
{
316334
Encode encode = new Encode(this.header, pix);
317335
ubyte[] data = encode.makeIHDR ~ ancillary_chunks ~ encode.makeIDAT ~ encode.makeIEND;
@@ -489,7 +507,7 @@ class Img4d
489507

490508
enum ThresholdType
491509
{
492-
simple,// adaptive,
510+
simple, // adaptive,
493511
}
494512

495513
auto threshold(ubyte[][] grayscale, ThresholdType type, int thresholdValue = 127)
@@ -538,7 +556,7 @@ class Img4d
538556
return hist;
539557
}
540558

541-
ubyte[][] gammaCorrection(ubyte[][] data, double gamma)
559+
void gammaCorrection(ref ubyte[][] data, in double gamma)
542560
{
543561
double pixel_max = data.join.maxElement.to!double;
544562

@@ -550,10 +568,9 @@ class Img4d
550568
.to!ubyte;
551569
}
552570
}
553-
return data;
554571
}
555572

556-
auto rectangle(ref ubyte[][] src, int[] pos, int[] size)
573+
void rectangle(ref ubyte[][] src, int[] pos, int[] size)
557574
{
558575
for (int i = pos[0]; i < pos[0] + size[0]; i++)
559576
{

0 commit comments

Comments
 (0)