1 /**
2  * Copyright:       Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3  * Authors:         $(Gabriel Gheorghe)
4  * License:         $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5  * Source:          $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/image/format/png.d)
6  * Documentation:
7  * Coverage:
8 **/
9 module liberty.image.format.bmp;
10 
11 import liberty.image.impl;
12 import liberty.math.vector;
13 
14 /**
15  *
16 **/
17 enum IMPLICIT_BMP_HEADER_DATA_POSITION = 54;
18 
19 /**
20  *
21 **/
22 struct BMPHeader {
23   /**
24    *
25   **/
26   uint dataPosition;
27 
28   /**
29    *
30   **/
31   uint size;
32 
33   /**
34    *
35   **/
36   uint width;
37 
38   /**
39    *
40   **/
41   uint height;
42 }
43 
44 /**
45  *
46 **/
47 final class BMPImage : Image {
48   private {
49     BMPHeader header;
50     ubyte[] data;
51   }
52 
53   /**
54    *
55   **/
56   this(BMPHeader header, ubyte[] pixelData) {
57     this.header = header;
58     this.pixelData = pixelData;
59   }
60 
61   /**
62    *
63   **/
64   uint getSize()   const {
65     return header.size;
66   }
67 
68   /**
69    * Retursn image width in pixels.
70   **/
71   uint getWidth()   const {
72     return header.width;
73   }
74 
75   /**
76    * Retursn image height in pixels.
77   **/
78   uint getHeight()   const {
79     return header.height;
80   }
81 
82   /**
83    *
84   **/
85   float getRGBPixelColor(int x, int y)   {
86     const ubyte b = pixelData[header.width * y * 4 + x * 4 + 0];
87     const ubyte g = pixelData[header.width * y * 4 + x * 4 + 1];
88     const ubyte r = pixelData[header.width * y * 4 + x * 4 + 2];
89 
90     return -((r << 16) | (g << 8) | (b << 0));
91   }
92 
93   /**
94    *
95   **/
96   Color4 getRGBAPixel(int x, int y)   {
97     const ubyte b = pixelData[header.width * y * 4 + x * 4 + 0];
98     const ubyte g = pixelData[header.width * y * 4 + x * 4 + 1];
99     const ubyte r = pixelData[header.width * y * 4 + x * 4 + 2];
100     const ubyte a = pixelData[header.width * y * 4 + x * 4 + 3];
101     return Color4(r, g, b, a);
102   }
103 
104   /**
105    *
106   **/
107   ubyte[] getData()   {
108     return data;
109   }
110 }