1 /*
2  * dimage - util.d
3  * by Laszlo Szeremi
4  *
5  * Copyright under Boost Software License.
6  *
7  * Note: Many elements of this file might be outsourced to an external library.
8  */
9 
10 module dimage.util;
11 
12 public import bitleveld.reinterpret;
13 
14 /**
15  * Copies the content of a string array into a static char array
16  */
17 void stringCpy(CR)(ref CR target, string input) {
18 	for(size_t i ; i < input.length ; i++){
19 		target[i] = input[i];
20 	}
21 }
22 /**
23  * Converts a Big Endian stream to native.
24  */
25 T[] bigEndianStreamToNative(T)(T[] source) @nogc @safe pure nothrow {
26 	version (LittleEndian) {
27 		import std.bitmanip : swapEndian;
28 		foreach(ref T elem ; source) 
29 			elem = swapEndian(elem);
30 	}
31 	return source;
32 }
33 /**
34  * Converts a native stream to Big Endian.
35  */
36 T[] nativeStreamToBigEndian(T)(T[] source) @nogc @safe pure nothrow {
37 	version (LittleEndian) {
38 		import std.bitmanip : swapEndian;
39 		foreach(ref T elem ; source) 
40 			elem = swapEndian(elem);
41 	}
42 	return source;
43 }
44 /**
45  * Separates the first string out from the datastream.
46  */
47 string getFirstString(ref ubyte[] stream) @safe pure {
48 	string result;
49 	for (size_t i ; i < stream.length ; i++) {
50 		if (stream[i] == 0) {
51 			ubyte[] slice = stream[0..i];
52 			result = reinterpretCast!char(slice).idup;
53 			stream = stream[i+1..$];
54 			return result;
55 		}
56 	}
57 	return result;
58 }
59 /**
60  * Adam7 deinterlacing algorithm
61  */
62 ubyte[] adam7(ubyte[] input, size_t bytedepth) {
63 	return null;
64 }
65 /**
66  * Image compatison for unittests
67  */
68 version(unittest) {
69 	import dimage.base;
70 	import std.conv : to;
71 	void compareImages(bool ignoreAlpha = false) (Image a, Image b) {
72 		assert (a.height == b.height);
73 		assert (a.width == b.width);
74 		for(int y ; y < a.height ; y++) {
75 			for(int x ; x < a.width ; x++) {
76 				auto pixelA = a.readPixel(x,y);
77 				auto pixelB = b.readPixel(x,y);
78 				static if(ignoreAlpha) {
79 					assert(pixelA.r == pixelB.r && pixelA.g == pixelB.g && pixelA.b == pixelB.b, "Pixel mismatch at position " ~ 
80 							to!string(x) ~ ";" ~ to!string(y) ~ "\nA = " ~ pixelA.toString ~ "; B = " ~ pixelB.toString);
81 				} else {
82 					assert(pixelA == pixelB, "Pixel mismatch at position " ~ 
83 							to!string(x) ~ ";" ~ to!string(y) ~ "\nA = " ~ pixelA.toString ~ "; B = " ~ pixelB.toString);
84 				}
85 			}
86 		}
87 	}
88 }