@@ -17,7 +17,17 @@ import std.algorithm : reduce;
17
17
import std.string : toLower;
18
18
import std.path : extension;
19
19
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
+
21
31
22
32
import mir.ndslice.topology : reshape;
23
33
import mir.ndslice.slice;
@@ -130,7 +140,11 @@ bool imwrite(in string path, size_t width, size_t height, ImageFormat format, Bi
130
140
assert (width > 0 && height > 0 );
131
141
if (depth == BitDepth.BD_8 )
132
142
{
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]);
134
148
}
135
149
else if (depth == BitDepth.BD_16 )
136
150
{
@@ -338,6 +352,25 @@ unittest
338
352
339
353
private :
340
354
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
+
341
374
Image imreadImpl_imageformats (in string path, ReadParams params)
342
375
{
343
376
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)
346
379
params.format = ImageFormat.IF_RGB ;
347
380
348
381
Image im = null ;
349
- auto ch = imreadImpl_imageformats_adoptFormat(params.format);
382
+ // auto ch = imreadImpl_imageformats_adoptFormat(params.format);
350
383
384
+ GamutImage gimage;
385
+
351
386
if (params.depth == BitDepth.BD_UNASSIGNED || params.depth == BitDepth.BD_8 )
352
387
{
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);
355
411
}
356
412
else if (params.depth == BitDepth.BD_16 )
357
413
{
414
+ // This should be revised according to gamut. Probably will not work as its present form
415
+
358
416
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);
361
422
}
362
423
else
363
424
{
@@ -430,17 +491,17 @@ int imreadImpl_imageformats_adoptFormat(ImageFormat format)
430
491
switch (format)
431
492
{
432
493
case ImageFormat.IF_RGB :
433
- ch = ColFmt.RGB ;
434
- break ;
435
- case ImageFormat.IF_RGB_ALPHA :
436
- ch = ColFmt.RGBA ;
494
+ ch = 3 ;
437
495
break ;
496
+ // case ImageFormat.IF_RGB_ALPHA:
497
+ // ch = 4;
498
+ // break;
438
499
case ImageFormat.IF_MONO :
439
- ch = ColFmt.Y;
440
- break ;
441
- case ImageFormat.IF_MONO_ALPHA :
442
- ch = ColFmt.YA ;
500
+ ch = 1 ;
443
501
break ;
502
+ // case ImageFormat.IF_MONO_ALPHA:
503
+ // ch = 2;
504
+ // break;
444
505
default :
445
506
throw new Exception (" Format not supported" );
446
507
}
0 commit comments