1 /*
2 Copyright (c) 2019 Ferhat Kurtulmuş
3 Boost Software License - Version 1.0 - August 17th, 2003
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10 The copyright notices in the Software and this entire statement, including
11 the above license grant, this restriction and the following disclaimer,
12 must be included in all copies of the Software, in whole or in part, and
13 all derivative works of the Software, unless such copies or derivative
14 works are solely in the form of machine-executable object code generated by
15 a source language processor.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
19 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
20 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24 
25 module opencvd.imgcodecs;
26 
27 import std..string;
28 
29 import opencvd.cvcore;
30 
31 private {
32     extern (C) @nogc nothrow {
33         Mat Image_IMRead(const char* filename, int flags);
34         bool Image_IMWrite(const char* filename, Mat img);
35         bool Image_IMWrite_WithParams(const char* filename, Mat img, IntVector params);
36         ByteArray Image_IMEncode(const char* fileExt, Mat img);
37         ByteArray Image_IMEncode_WithParams(const char* fileExt, Mat img, IntVector params);
38         Mat Image_IMDecode(ByteArray buf, int flags);
39     }
40 }
41 
42 enum: int { // ImreadModes
43     IMREAD_UNCHANGED = -1, 
44     IMREAD_GRAYSCALE = 0, 
45     IMREAD_COLOR = 1, 
46     IMREAD_ANYDEPTH = 2, 
47     IMREAD_ANYCOLOR = 4, 
48     IMREAD_LOAD_GDAL = 8, 
49     IMREAD_REDUCED_GRAYSCALE_2 = 16, 
50     IMREAD_REDUCED_COLOR_2 = 17, 
51     IMREAD_REDUCED_GRAYSCALE_4 = 32, 
52     IMREAD_REDUCED_COLOR_4 = 33, 
53     IMREAD_REDUCED_GRAYSCALE_8 = 64, 
54     IMREAD_REDUCED_COLOR_8 = 65, 
55     IMREAD_IGNORE_ORIENTATION = 128 
56 }
57 
58 enum: int { // 	ImwriteFlags
59     IMWRITE_JPEG_QUALITY = 1, 
60     IMWRITE_JPEG_PROGRESSIVE = 2, 
61     IMWRITE_JPEG_OPTIMIZE = 3, 
62     IMWRITE_JPEG_RST_INTERVAL = 4, 
63     IMWRITE_JPEG_LUMA_QUALITY = 5, 
64     IMWRITE_JPEG_CHROMA_QUALITY = 6, 
65     IMWRITE_PNG_COMPRESSION = 16, 
66     IMWRITE_PNG_STRATEGY = 17, 
67     IMWRITE_PNG_BILEVEL = 18, 
68     IMWRITE_PXM_BINARY = 32, 
69     IMWRITE_EXR_TYPE = (3 << 4) + 0, 
70     IMWRITE_WEBP_QUALITY = 64, 
71     IMWRITE_PAM_TUPLETYPE = 128, 
72     IMWRITE_TIFF_RESUNIT = 256, 
73     IMWRITE_TIFF_XDPI = 257, 
74     IMWRITE_TIFF_YDPI = 258, 
75     IMWRITE_TIFF_COMPRESSION = 259, 
76     IMWRITE_JPEG2000_COMPRESSION_X1000 = 272 
77 }
78 
79 enum: int { // ImwriteEXRTypeFlags
80     IMWRITE_EXR_TYPE_HALF = 1, 
81     IMWRITE_EXR_TYPE_FLOAT = 2 
82 }
83 
84 enum: int { // ImwritePAMFlags
85     IMWRITE_PAM_FORMAT_NULL = 0, 
86     IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1, 
87     IMWRITE_PAM_FORMAT_GRAYSCALE = 2, 
88     IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA = 3, 
89     IMWRITE_PAM_FORMAT_RGB = 4, 
90     IMWRITE_PAM_FORMAT_RGB_ALPHA = 5 
91 }
92  
93 enum: int { // ImwritePNGFlags
94     IMWRITE_PNG_STRATEGY_DEFAULT = 0, 
95     IMWRITE_PNG_STRATEGY_FILTERED = 1, 
96     IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY = 2, 
97     IMWRITE_PNG_STRATEGY_RLE = 3, 
98     IMWRITE_PNG_STRATEGY_FIXED = 4 
99 }
100 
101 Mat imread(const char* filename, int flags) @nogc nothrow {
102     return Image_IMRead(filename, flags);
103 }
104 
105 Mat imread(string filename, int flags = IMREAD_UNCHANGED) @nogc nothrow {
106     return Image_IMRead(filename.ptr, flags);
107 }
108 
109 bool imwrite(const char* filename, Mat img) @nogc nothrow {
110     return Image_IMWrite(filename, img);
111 }
112 
113 bool imwrite(string filename, Mat img) @nogc nothrow {
114     return Image_IMWrite(filename.ptr, img);
115 }
116 
117 bool imwrite(string filename, Mat img, int[] params) @nogc nothrow {
118     return Image_IMWrite_WithParams(filename.ptr, img, IntVector(params.ptr, cast(int)params.length));
119 }
120 
121 ubyte[] imencode(string fileExt, Mat img) @nogc nothrow {
122     ByteArray ba = Image_IMEncode(fileExt.ptr, img);
123     return ba.data[0..ba.length];
124 }
125 
126 ubyte[] imencode(string fileExt, Mat img, int[] params) @nogc nothrow {
127     ByteArray ba = Image_IMEncode_WithParams(fileExt.ptr, img, IntVector(params.ptr, cast(int)params.length));
128     return ba.data[0..ba.length];
129 }
130 
131 Mat imdecode(ubyte[] buf, int flags) @nogc nothrow {
132     return Image_IMDecode(ByteArray(buf.ptr, cast(int)buf.length), flags);
133 }
134