World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
ImageFormatConverter.h
См. документацию.
1 #pragma once
2 
3 typedef bool (*ImageFormatConverter)(unsigned int num_pixels, const void *src,
4  void *dst);
5 
6 inline uint32_t R5G6B5_to_A8R8G8B8(uint16_t color16, unsigned char alpha) {
7  uint32_t c = color16;
8  unsigned int b = (c & 31) * 8;
9  unsigned int g = ((c >> 5) & 63) * 4;
10  unsigned int r = ((c >> 11) & 31) * 8;
11 
12  return ((unsigned int)alpha << 24) | (r << 16) | (g << 8) | b;
13 }
14 
15 inline uint32_t R5G6B5_to_R8G8B8A8(uint16_t color16, unsigned char alpha) { // eh what?? ABGR
16  uint32_t c = color16;
17  unsigned int b = (c & 31) * 8;
18  unsigned int g = ((c >> 5) & 63) * 4;
19  unsigned int r = ((c >> 11) & 31) * 8;
20 
21  return (((unsigned int)alpha << 24) & 0xFF000000) | ((b << 16) & 0x00FF0000) | ((g << 8) & 0x0000FF00) | (r & 0x000000FF);
22 }
23 
25  uint32_t b = ((c & 0xFF) / 8) & 31;
26  uint32_t g = (((c >> 8) & 0xFF) / 4) & 63;
27  uint32_t r = (((c >> 16) & 0xFF) / 8) & 31;
28 
29  return (uint16_t)((r << 11) | (g << 5) | b);
30 }
31 
33  uint32_t b = ((c & 0xFF) / 8) & 31;
34  uint32_t g = (((c >> 8) & 0xFF) / 8) & 31;
35  uint32_t r = (((c >> 16) & 0xFF) / 8) & 31;
36  uint32_t a = (((c >> 24) & 0xFF)) == 255;
37 
38  return (uint16_t)( (a << 15) |(r << 10) | (g << 5) | b);
39 }
40 
41 
42 inline uint32_t A8R8G8B8_to_R8G8B8A8(uint32_t c) { // eh waht ?? ABGR
43  return (c & 0xFF000000) | (c & 0x000000FF) << 16 | (c & 0x0000FF00) | (c & 0x00FF0000) >> 16;
44 }
45 
46 inline bool Image_A8R8G8B8_to_R8G8B8A8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels) {
47  auto src = (uint32_t*)src_pixels;
48  auto dst = (uint32_t*)dst_pixels;
49 
50  for (unsigned int i = 0; i < num_pixels; ++i) {
51  dst[i] = A8R8G8B8_to_R8G8B8A8(src[i]);
52  }
53 
54  return true;
55 }
56 
57 inline bool Image_A8R8G8B8_to_R5G6B5(unsigned int num_pixels,
58  const void *src_pixels, void *dst_pixels) {
59  auto src = (uint32_t*)src_pixels;
60  auto dst = (uint16_t*)dst_pixels;
61 
62  for (unsigned int i = 0; i < num_pixels; ++i) {
63  dst[i] = A8R8G8B8_to_R5G6B5(src[i]);
64  }
65 
66  return true;
67 }
68 
69 inline bool Image_A8R8G8B8_to_A1R5G5B5(unsigned int num_pixels,
70  const void *src_pixels, void *dst_pixels) {
71 
72  auto src = (uint32_t*)src_pixels;
73  auto dst = (uint16_t*)dst_pixels;
74 
75  for (unsigned int i = 0; i < num_pixels; ++i) {
76  dst[i] = A8R8G8B8_to_A1R5G5B5(src[i]);
77  }
78 
79  return true;
80 }
81 
82 inline bool Image_R5G6B5_to_A8R8G8B8(unsigned int num_pixels,
83  const void *src_pixels, void *dst_pixels) {
84  auto src = (uint16_t*)src_pixels;
85  auto dst = (uint32_t*)dst_pixels;
86 
87  for (unsigned int i = 0; i < num_pixels; ++i) {
88  dst[i] = R5G6B5_to_A8R8G8B8(src[i], 0xFF);
89  }
90 
91  return true;
92 }
93 
94 inline bool Image_R5G6B5_to_R8G8B8A8(unsigned int num_pixels,
95  const void *src_pixels, void *dst_pixels) {
96  auto src = (uint16_t*)src_pixels;
97  auto dst = (uint32_t*)dst_pixels;
98 
99  for (unsigned int i = 0; i < num_pixels; ++i) {
100  dst[i] = R5G6B5_to_R8G8B8A8(src[i], 255);
101  }
102 
103  return true;
104 }
105 
106 inline unsigned int R5G6B5_extract_R(uint16_t c) {
107  return 8 * ((c >> 11) & 0x1F);
108 }
109 inline unsigned int R5G6B5_extract_G(uint16_t c) {
110  return 4 * ((c >> 5) & 0x3F);
111 }
112 inline unsigned int R5G6B5_extract_B(uint16_t c) {
113  return 8 * ((c >> 0) & 0x1F);
114 }
115 
116 inline bool Image_R5G6B5_to_R8G8B8(unsigned int num_pixels,
117  const void *src_pixels, void *dst_pixels) {
118  auto src = (uint16_t*)src_pixels;
119  auto dst = (uint8_t*)dst_pixels;
120 
121  for (unsigned int i = 0; i < num_pixels; ++i) {
122  dst[i * 3 + 0] = R5G6B5_extract_R(src[i]);
123  dst[i * 3 + 1] = R5G6B5_extract_G(src[i]);
124  dst[i * 3 + 2] = R5G6B5_extract_B(src[i]);
125  }
126 
127  return true;
128 }
129 
130 inline unsigned int A1R5G5B5_extract_A(uint16_t c) {
131  return c & 0x8000 ? 255 : 0;
132 }
133 inline unsigned int A1R5G5B5_extract_R(uint16_t c) {
134  return 8 * ((c >> 10) & 0x1F);
135 }
136 inline unsigned int A1R5G5B5_extract_G(uint16_t c) {
137  return 8 * ((c >> 5) & 0x1F);
138 }
139 inline unsigned int A1R5G5B5_extract_B(uint16_t c) {
140  return 8 * ((c >> 0) & 0x1F);
141 }
142 
143 inline bool Image_A1R5G5B5_to_R8G8B8A8(unsigned int num_pixels,
144  const void *src_pixels,
145  void *dst_pixels) {
146  auto src = (uint16_t*)src_pixels;
147  auto dst = (uint8_t*)dst_pixels;
148 
149  for (unsigned int i = 0; i < num_pixels; ++i) {
150  dst[i * 4 + 0] = A1R5G5B5_extract_R(src[i]);
151  dst[i * 4 + 1] = A1R5G5B5_extract_G(src[i]);
152  dst[i * 4 + 2] = A1R5G5B5_extract_B(src[i]);
153  dst[i * 4 + 3] = A1R5G5B5_extract_A(src[i]);
154  }
155 
156  return true;
157 }
uint16_t
unsigned __int16 uint16_t
Definition: SDL_config.h:37
A1R5G5B5_extract_A
unsigned int A1R5G5B5_extract_A(uint16_t c)
Definition: ImageFormatConverter.h:130
Image_A1R5G5B5_to_R8G8B8A8
bool Image_A1R5G5B5_to_R8G8B8A8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:143
ImageFormatConverter
bool(* ImageFormatConverter)(unsigned int num_pixels, const void *src, void *dst)
Definition: ImageFormatConverter.h:3
A1R5G5B5_extract_B
unsigned int A1R5G5B5_extract_B(uint16_t c)
Definition: ImageFormatConverter.h:139
A8R8G8B8_to_R5G6B5
uint16_t A8R8G8B8_to_R5G6B5(uint32_t c)
Definition: ImageFormatConverter.h:24
A8R8G8B8_to_A1R5G5B5
uint16_t A8R8G8B8_to_A1R5G5B5(uint32_t c)
Definition: ImageFormatConverter.h:32
Image_R5G6B5_to_A8R8G8B8
bool Image_R5G6B5_to_A8R8G8B8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:82
Image_R5G6B5_to_R8G8B8A8
bool Image_R5G6B5_to_R8G8B8A8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:94
alpha
GLfloat GLfloat GLfloat alpha
Definition: SDL_opengl_glext.h:415
Image_A8R8G8B8_to_R8G8B8A8
bool Image_A8R8G8B8_to_R8G8B8A8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:46
Image_A8R8G8B8_to_A1R5G5B5
bool Image_A8R8G8B8_to_A1R5G5B5(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:69
src
GLenum src
Definition: SDL_opengl_glext.h:1740
A1R5G5B5_extract_R
unsigned int A1R5G5B5_extract_R(uint16_t c)
Definition: ImageFormatConverter.h:133
R5G6B5_extract_R
unsigned int R5G6B5_extract_R(uint16_t c)
Definition: ImageFormatConverter.h:106
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1740
R5G6B5_to_A8R8G8B8
uint32_t R5G6B5_to_A8R8G8B8(uint16_t color16, unsigned char alpha)
Definition: ImageFormatConverter.h:6
A8R8G8B8_to_R8G8B8A8
uint32_t A8R8G8B8_to_R8G8B8A8(uint32_t c)
Definition: ImageFormatConverter.h:42
Image_R5G6B5_to_R8G8B8
bool Image_R5G6B5_to_R8G8B8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:116
uint8_t
unsigned __int8 uint8_t
Definition: SDL_config.h:35
R5G6B5_to_R8G8B8A8
uint32_t R5G6B5_to_R8G8B8A8(uint16_t color16, unsigned char alpha)
Definition: ImageFormatConverter.h:15
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1112
r
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
Image_A8R8G8B8_to_R5G6B5
bool Image_A8R8G8B8_to_R5G6B5(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:57
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11096
R5G6B5_extract_B
unsigned int R5G6B5_extract_B(uint16_t c)
Definition: ImageFormatConverter.h:112
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1112
R5G6B5_extract_G
unsigned int R5G6B5_extract_G(uint16_t c)
Definition: ImageFormatConverter.h:109
g
GLboolean GLboolean g
Definition: SDL_opengl_glext.h:1112
uint32_t
unsigned __int32 uint32_t
Definition: SDL_config.h:39
A1R5G5B5_extract_G
unsigned int A1R5G5B5_extract_G(uint16_t c)
Definition: ImageFormatConverter.h:136