Skip to content

Commit dd5d726

Browse files
authored
Merge pull request #137 from aferust/master
replace imageformats with gamut. remove alpha
2 parents 158784d + c83ef05 commit dd5d726

File tree

7 files changed

+140
-57
lines changed

7 files changed

+140
-57
lines changed

dub.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
],
5555
"dependencies":{
5656
"dcv:core": "*",
57-
"imageformats": "7.0.2"
57+
"gamut": "~>1.0.0"
5858
},
5959
"buildTypes": {
6060
"unittest-release": {

source/dcv/core/image.d

+29-13
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,24 @@ enum ImageFormat
2727
{
2828
IF_UNASSIGNED = 0, /// Not assigned format.
2929
IF_MONO, /// Mono, single channel format.
30-
IF_MONO_ALPHA, /// Mono with alpha channel.
30+
//IF_MONO_ALPHA, /// Mono with alpha channel.
3131
IF_RGB, /// RGB format.
3232
IF_BGR, /// BGR format.
3333
IF_YUV, /// YUV (YCbCr) format.
34-
IF_RGB_ALPHA, /// RGB format with alpha.
35-
IF_BGR_ALPHA /// BGR format with alpha.
34+
//IF_RGB_ALPHA, /// RGB format with alpha.
35+
//IF_BGR_ALPHA /// BGR format with alpha.
3636
}
3737

38-
immutable size_t[] imageFormatChannelCount = [0, // unassigned
38+
39+
immutable size_t[] imageFormatChannelCount = [
40+
0, // unassigned
3941
1, // mono
40-
2, // mono alpha
42+
//2, // mono alpha
4143
3, // rgb
4244
3, // bgr
4345
3, // yuv
44-
4, // rgba
45-
4 // bgra
46+
//4, // rgba
47+
//4 // bgra
4648
];
4749

4850
/// Bit depth of a pixel in an image.
@@ -379,24 +381,38 @@ public:
379381

380382
if (_depth == BitDepth.BD_8)
381383
{
384+
foreach(i, v; data!ubyte){
385+
newim.data!T[i] = cast(T)v;
386+
}
387+
/*
382388
foreach (v1, ref v2; lockstep(data!ubyte, newim.data!T))
383389
{
384390
v2 = cast(T)v1;
385-
}
391+
}*/
392+
386393
}
387394
else if (_depth == BitDepth.BD_16)
388395
{
396+
foreach(i, v; data!ushort){
397+
newim.data!T[i] = cast(T)v;
398+
}
399+
/*
389400
foreach (v1, ref v2; lockstep(data!ushort, newim.data!T))
390401
{
391402
v2 = cast(T)v1;
392-
}
403+
}*/
393404
}
394405
else if (_depth == BitDepth.BD_32)
395406
{
407+
foreach(i, v; data!float){
408+
newim.data!T[i] = cast(T)v;
409+
}
410+
/*
396411
foreach (v1, ref v2; lockstep(data!float, newim.data!T))
397412
{
398413
v2 = cast(T)v1;
399414
}
415+
*/
400416
}
401417

402418
return newim;
@@ -606,15 +622,15 @@ Image asImage(Iterator, size_t N, SliceKind kind)(Slice!(Iterator, N, kind) slic
606622
case 1:
607623
format = ImageFormat.IF_MONO;
608624
break;
609-
case 2:
625+
/+case 2:
610626
format = ImageFormat.IF_MONO_ALPHA;
611-
break;
627+
break;+/
612628
case 3:
613629
format = ImageFormat.IF_RGB;
614630
break;
615-
case 4:
631+
/+case 4:
616632
format = ImageFormat.IF_RGB_ALPHA;
617-
break;
633+
break;+/
618634
default:
619635
import std.conv : to;
620636

source/dcv/imageio/image.d

+76-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ import std.algorithm : reduce;
1717
import std.string : toLower;
1818
import std.path : extension;
1919

20-
import imageformats;
20+
import gamut : GamutImage = Image,
21+
LOAD_NO_ALPHA,
22+
LOAD_RGB,
23+
LOAD_GREYSCALE,
24+
LOAD_8BIT,
25+
LOAD_16BIT,
26+
PixelType,
27+
LAYOUT_GAPLESS,
28+
LAYOUT_VERT_STRAIGHT;
29+
30+
2131

2232
import mir.ndslice.topology : reshape;
2333
import mir.ndslice.slice;
@@ -130,7 +140,11 @@ bool imwrite(in string path, size_t width, size_t height, ImageFormat format, Bi
130140
assert(width > 0 && height > 0);
131141
if (depth == BitDepth.BD_8)
132142
{
133-
write_image(path, cast(long)width, cast(long)height, data, imageFormatChannelCount[format]);
143+
GamutImage image;
144+
image.loadFromMemory(data, ReadParams(format, depth).readParams2LoadFlags);
145+
if (!image.saveToFile(path))
146+
throw new Exception("Writing " ~ path ~ " failed");
147+
// write_image(path, cast(long)width, cast(long)height, data, imageFormatChannelCount[format]);
134148
}
135149
else if (depth == BitDepth.BD_16)
136150
{
@@ -338,6 +352,25 @@ unittest
338352

339353
private:
340354

355+
int readParams2LoadFlags(ReadParams params){
356+
int ret;
357+
358+
if(params.format == ImageFormat.IF_RGB){
359+
ret |= LOAD_RGB;
360+
}else
361+
if(params.format == ImageFormat.IF_MONO){
362+
ret |= LOAD_GREYSCALE;
363+
}
364+
365+
if(params.depth == BitDepth.BD_8){
366+
ret |= LOAD_8BIT;
367+
}else
368+
if(params.depth == BitDepth.BD_16){
369+
ret |= LOAD_16BIT;
370+
}
371+
return ret | LOAD_NO_ALPHA;
372+
}
373+
341374
Image imreadImpl_imageformats(in string path, ReadParams params)
342375
{
343376
enforce(params.depth != BitDepth.BD_32, "Currenly reading of 32-bit image data is not supported");
@@ -346,18 +379,46 @@ Image imreadImpl_imageformats(in string path, ReadParams params)
346379
params.format = ImageFormat.IF_RGB;
347380

348381
Image im = null;
349-
auto ch = imreadImpl_imageformats_adoptFormat(params.format);
382+
//auto ch = imreadImpl_imageformats_adoptFormat(params.format);
350383

384+
GamutImage gimage;
385+
351386
if (params.depth == BitDepth.BD_UNASSIGNED || params.depth == BitDepth.BD_8)
352387
{
353-
IFImage ifim = read_image(path, ch);
354-
im = new Image(cast(size_t)ifim.w, cast(size_t)ifim.h, params.format, BitDepth.BD_8, ifim.pixels);
388+
//IFImage ifim = read_image(path, ch);
389+
gimage.loadFromFile(path, LOAD_NO_ALPHA | LOAD_RGB | LOAD_8BIT);
390+
//gimage.setSize(gimage.width, gimage.height, PixelType.rgb8, LAYOUT_GAPLESS | LAYOUT_VERT_STRAIGHT); // make contiguous
391+
392+
//ubyte[] allpixels = gimage.allPixelsAtOnce().dup;
393+
394+
ubyte[] allpixels = new ubyte[gimage.width*gimage.height*3];
395+
size_t kk;
396+
for (int y = 0; y < gimage.height(); ++y)
397+
{
398+
ubyte* scan = cast(ubyte*) gimage.scanline(y);
399+
for (int x = 0; x < gimage.width(); ++x)
400+
{
401+
allpixels[kk++] = scan[3*x + 0];
402+
allpixels[kk++] = scan[3*x + 1];
403+
allpixels[kk++] = scan[3*x + 2];
404+
}
405+
}
406+
407+
import std.conv : to;
408+
if (gimage.errored)
409+
throw new Exception(gimage.errorMessage.to!string);
410+
im = new Image(cast(size_t)gimage.width, cast(size_t)gimage.height, params.format, BitDepth.BD_8, allpixels);
355411
}
356412
else if (params.depth == BitDepth.BD_16)
357413
{
414+
// This should be revised according to gamut. Probably will not work as its present form
415+
358416
enforce(path.extension.toLower == ".png", "Reading 16-bit image has to be in PNG format.");
359-
IFImage16 ifim = read_png16(path, ch);
360-
im = new Image(cast(size_t)ifim.w, cast(size_t)ifim.h, params.format, BitDepth.BD_16, cast(ubyte[])ifim.pixels);
417+
gimage.loadFromFile(path, LOAD_NO_ALPHA | LOAD_RGB | LOAD_16BIT);
418+
gimage.setSize(gimage.width, gimage.height, PixelType.rgb16, LAYOUT_GAPLESS | LAYOUT_VERT_STRAIGHT);
419+
ushort[] allpixels = gimage.allPixelsAtOnce().dup;
420+
//IFImage16 ifim = read_png16(path, ch);
421+
im = new Image(cast(size_t)gimage.width, cast(size_t)gimage.height, params.format, BitDepth.BD_16, allpixels);
361422
}
362423
else
363424
{
@@ -430,17 +491,17 @@ int imreadImpl_imageformats_adoptFormat(ImageFormat format)
430491
switch (format)
431492
{
432493
case ImageFormat.IF_RGB:
433-
ch = ColFmt.RGB;
434-
break;
435-
case ImageFormat.IF_RGB_ALPHA:
436-
ch = ColFmt.RGBA;
494+
ch = 3;
437495
break;
496+
//case ImageFormat.IF_RGB_ALPHA:
497+
// ch = 4;
498+
// break;
438499
case ImageFormat.IF_MONO:
439-
ch = ColFmt.Y;
440-
break;
441-
case ImageFormat.IF_MONO_ALPHA:
442-
ch = ColFmt.YA;
500+
ch = 1;
443501
break;
502+
//case ImageFormat.IF_MONO_ALPHA:
503+
// ch = 2;
504+
// break;
444505
default:
445506
throw new Exception("Format not supported");
446507
}

source/dcv/plot/drawprimitives.d

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,18 @@ GLuint loadTexture(ubyte* imptr, uint w, uint h){
7575
glGenTextures(1, &textureId);
7676
glBindTexture(GL_TEXTURE_2D, textureId);
7777

78+
/+
7879
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
7980
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
8081
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
8182
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
82-
/+
83+
+/
8384
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
8485
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
8586
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
8687
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
8788
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
88-
+/
89+
8990
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, imptr);
9091
glGenerateMipmap(GL_TEXTURE_2D);
9192

source/dcv/plot/figure.d

+5-5
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ class Figure
458458

459459
int _width = 0;
460460
int _height = 0;
461-
ubyte[] _data = void;
461+
ubyte[] _data = null;
462462
string _title = "";
463463

464464
MouseCallback _mouseCallback = null;
@@ -1065,9 +1065,9 @@ Image adoptImage(Image image)
10651065
import mir.ndslice.topology;
10661066
switch (showImage.format)
10671067
{
1068-
case ImageFormat.IF_RGB_ALPHA:
1068+
/+case ImageFormat.IF_RGB_ALPHA:
10691069
showImage = showImage.sliced[0 .. $, 0 .. $, 0 .. 2].asImage(ImageFormat.IF_RGB);
1070-
break;
1070+
break;+/
10711071
case ImageFormat.IF_BGR:
10721072
foreach (e; showImage.sliced.pack!1.flattened)
10731073
{
@@ -1076,15 +1076,15 @@ Image adoptImage(Image image)
10761076
e[2] = t;
10771077
}
10781078
break;
1079-
case ImageFormat.IF_BGR_ALPHA:
1079+
/+case ImageFormat.IF_BGR_ALPHA:
10801080
foreach (e; showImage.sliced.pack!1.flattened)
10811081
{
10821082
auto t = e[0];
10831083
e[0] = e[2];
10841084
e[2] = t;
10851085
}
10861086
showImage = showImage.sliced[0 .. $, 0 .. $, 0 .. 2].asImage(ImageFormat.IF_RGB);
1087-
break;
1087+
break;+/
10881088
case ImageFormat.IF_YUV:
10891089
showImage = showImage.sliced.yuv2rgb!ubyte.asImage(ImageFormat.IF_RGB);
10901090
break;

0 commit comments

Comments
 (0)