1// Aseprite Document Library
2// Copyright (c) 2018-2019 Igara Studio S.A.
3// Copyright (c) 2001-2015 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifndef DOC_IMAGE_TRAITS_H_INCLUDED
9#define DOC_IMAGE_TRAITS_H_INCLUDED
10#pragma once
11
12#include "doc/blend_funcs.h"
13#include "doc/color.h"
14#include "doc/color_mode.h"
15#include "doc/pixel_format.h"
16
17namespace doc {
18
19 struct RgbTraits {
20 static const ColorMode color_mode = ColorMode::RGB;
21 static const PixelFormat pixel_format = IMAGE_RGB;
22
23 enum {
24 bits_per_pixel = 32,
25 bytes_per_pixel = 4,
26 pixels_per_byte = 0,
27 channels = 4,
28 has_alpha = true,
29 };
30
31 typedef uint32_t pixel_t;
32 typedef pixel_t* address_t;
33 typedef const pixel_t* const_address_t;
34
35 static const pixel_t min_value = 0x00000000l;
36 static const pixel_t max_value = 0xffffffffl;
37
38 static inline int getRowStrideBytes(int pixels_per_row) {
39 return bytes_per_pixel * pixels_per_row;
40 }
41
42 static inline BlendFunc get_blender(BlendMode blend_mode, bool newBlend) {
43 return get_rgba_blender(blend_mode, newBlend);
44 }
45
46 static inline bool same_color(const pixel_t a, const pixel_t b) {
47 if (rgba_geta(a) == 0) {
48 if (rgba_geta(b) == 0)
49 return true;
50 else
51 return false;
52 }
53 else if (rgba_geta(b) == 0)
54 return false;
55 else
56 return a == b;
57 }
58 };
59
60 struct GrayscaleTraits {
61 static const ColorMode color_mode = ColorMode::GRAYSCALE;
62 static const PixelFormat pixel_format = IMAGE_GRAYSCALE;
63
64 enum {
65 bits_per_pixel = 16,
66 bytes_per_pixel = 2,
67 pixels_per_byte = 0,
68 channels = 2,
69 has_alpha = true,
70 };
71
72 typedef uint16_t pixel_t;
73 typedef pixel_t* address_t;
74 typedef const pixel_t* const_address_t;
75
76 static const pixel_t min_value = 0x0000;
77 static const pixel_t max_value = 0xffff;
78
79 static inline int getRowStrideBytes(int pixels_per_row) {
80 return bytes_per_pixel * pixels_per_row;
81 }
82
83 static inline BlendFunc get_blender(BlendMode blend_mode, bool newBlend) {
84 return get_graya_blender(blend_mode, newBlend);
85 }
86
87 static inline bool same_color(const pixel_t a, const pixel_t b) {
88 if (graya_geta(a) == 0) {
89 if (graya_geta(b) == 0)
90 return true;
91 else
92 return false;
93 }
94 else if (graya_geta(b) == 0)
95 return false;
96 else
97 return a == b;
98 }
99 };
100
101 struct IndexedTraits {
102 static const ColorMode color_mode = ColorMode::INDEXED;
103 static const PixelFormat pixel_format = IMAGE_INDEXED;
104
105 enum {
106 bits_per_pixel = 8,
107 bytes_per_pixel = 1,
108 pixels_per_byte = 1,
109 channels = 1,
110 has_alpha = false,
111 };
112
113 typedef uint8_t pixel_t;
114 typedef pixel_t* address_t;
115 typedef const pixel_t* const_address_t;
116
117 static const pixel_t min_value = 0x00;
118 static const pixel_t max_value = 0xff;
119
120 static inline int getRowStrideBytes(int pixels_per_row) {
121 return bytes_per_pixel * pixels_per_row;
122 }
123
124 static inline BlendFunc get_blender(BlendMode blend_mode, bool newBlend) {
125 return get_indexed_blender(blend_mode, newBlend);
126 }
127
128 static inline bool same_color(const pixel_t a, const pixel_t b) {
129 return a == b;
130 }
131 };
132
133 struct BitmapTraits {
134 static const ColorMode color_mode = ColorMode::BITMAP;
135 static const PixelFormat pixel_format = IMAGE_BITMAP;
136
137 enum {
138 bits_per_pixel = 1,
139 bytes_per_pixel = 1,
140 pixels_per_byte = 8,
141 channels = 1,
142 has_alpha = false,
143 };
144
145 typedef uint8_t pixel_t;
146 typedef pixel_t* address_t;
147 typedef const pixel_t* const_address_t;
148
149 static const pixel_t min_value = 0;
150 static const pixel_t max_value = 1;
151
152 static inline int getRowStrideBytes(int pixels_per_row) {
153 return ((pixels_per_row+7) / 8);
154 }
155
156 static inline bool same_color(const pixel_t a, const pixel_t b) {
157 return a == b;
158 }
159 };
160
161 struct TilemapTraits {
162 static const ColorMode color_mode = ColorMode::TILEMAP;
163 static const PixelFormat pixel_format = IMAGE_TILEMAP;
164
165 enum {
166 bits_per_pixel = 32,
167 bytes_per_pixel = 4,
168 pixels_per_byte = 0,
169 channels = 3, // Tile Index + Tile Set + Flags
170 has_alpha = false,
171 };
172
173 typedef uint32_t pixel_t;
174 typedef pixel_t* address_t;
175 typedef const pixel_t* const_address_t;
176
177 static const pixel_t min_value = 0x00000000l;
178 static const pixel_t max_value = 0xffffffffl;
179
180 static inline int getRowStrideBytes(int pixels_per_row) {
181 return bytes_per_pixel * pixels_per_row;
182 }
183
184 static inline BlendFunc get_blender(BlendMode blend_mode, bool newBlend) {
185 return get_indexed_blender(blend_mode, newBlend);
186 }
187
188 static inline bool same_color(const pixel_t a, const pixel_t b) {
189 return a == b;
190 }
191 };
192
193} // namespace doc
194
195#endif
196