FreeBASIC  0.91.0
gfx_image_convert.c
Go to the documentation of this file.
1 /* bpp conversion */
2 
3 #include "fb_gfx.h"
4 
5 /* !!!FIXME!!! little-endian only */
6 
7 /*:::::*/
8 void fb_image_convert_8to8(const unsigned char *src, unsigned char *dest, int w)
9 {
10  for (; w; w--)
11  *dest++ = *src++ & __fb_gfx->color_mask;
12 }
13 
14 
15 /*:::::*/
16 void fb_image_convert_8to16(const unsigned char *src, unsigned char *dest, int w)
17 {
18  int r, g, b;
19  unsigned short *d = (unsigned short *)dest;
20 
21  for (; w; w--) {
22  r = __fb_gfx->device_palette[*src] & 0xFF;
23  g = (__fb_gfx->device_palette[*src] >> 8) & 0xFF;
24  b = (__fb_gfx->device_palette[*src] >> 16) & 0xFF;
25  *d++ = (b >> 3) | ((g << 3) & 0x07E0) | ((r << 8) & 0xF800);
26  src++;
27  }
28 }
29 
30 
31 /*:::::*/
32 void fb_image_convert_8to32(const unsigned char *src, unsigned char *dest, int w)
33 {
34  int r, g, b;
35  unsigned int *d = (unsigned int *)dest;
36 
37  for (; w; w--) {
38  r = __fb_gfx->device_palette[*src] & 0xFF;
39  g = (__fb_gfx->device_palette[*src] >> 8) & 0xFF;
40  b = (__fb_gfx->device_palette[*src] >> 16) & 0xFF;
41  *d++ = 0xFF000000 | b | (g << 8) | (r << 16);
42  src++;
43  }
44 }
45 
46 /*:::::*/
47 void fb_image_convert_24to16(const unsigned char *src, unsigned char *dest, int w)
48 {
49  unsigned short *d = (unsigned short *)dest;
50  for (; w; w--) {
51  *d++ = (((unsigned short)src[0] << 8) & 0xF800) | (((unsigned short)src[1] << 3) & 0x07E0) | ((unsigned short)src[2] >> 3);
52  src += 3;
53  }
54 }
55 
56 
57 /*:::::*/
58 void fb_image_convert_24to32(const unsigned char *src, unsigned char *dest, int w)
59 {
60  unsigned int *d = (unsigned int *)dest;
61 
62  for (; w; w--) {
63  *d++ = 0xFF000000 | ((unsigned int)src[0] << 16) | ((unsigned int)src[1] << 8) | ((unsigned int)src[2]);
64  src += 3;
65  }
66 }
67 
68 /*:::::*/
69 void fb_image_convert_32to16(const unsigned char *src, unsigned char *dest, int w)
70 {
71  unsigned short *d = (unsigned short *)dest;
72  unsigned int c;
73 
74  for (; w; w--)
75  {
76  c = *(unsigned int *)src & 0x00FFFFFF;
77  *d++ = (unsigned short)((c >> (16+3)) | ((c >> 5) & 0x07E0) | ((c << 8) & 0xF800));
78  src += sizeof( unsigned int );
79  }
80 }
81 
82 /*:::::*/
83 void fb_image_convert_32to32(const unsigned char *src, unsigned char *dest, int w)
84 {
85  unsigned int *d = (unsigned int *)dest;
86  unsigned int c;
87 
88  for (; w; w--)
89  {
90  c = *(unsigned int *)src;
91  *d++ = (c >> 16) | (c & 0xFF00FF00) | (c << 16);
92  src += sizeof( unsigned int );
93  }
94 }
95 
96 /*:::::*/
97 void fb_image_convert_24bgrto16(const unsigned char *src, unsigned char *dest, int w)
98 {
99  unsigned short *d = (unsigned short *)dest;
100  for (; w; w--) {
101  *d++ = ((unsigned short)src[0] >> 3) | (((unsigned short)src[1] << 3) & 0x07E0) | (((unsigned short)src[2] << 8) & 0xF800);
102  src += 3;
103  }
104 }
105 
106 
107 /*:::::*/
108 void fb_image_convert_24bgrto32(const unsigned char *src, unsigned char *dest, int w)
109 {
110  unsigned int *d = (unsigned int *)dest;
111 
112  for (; w; w--) {
113  *d++ = 0xFF000000 | (*(unsigned int *)src & 0xFFFFFF);
114  src += 3;
115  }
116 }
117 
118 /*:::::*/
119 void fb_image_convert_32bgrto16(const unsigned char *src, unsigned char *dest, int w)
120 {
121  unsigned short *d = (unsigned short *)dest;
122  const unsigned int *s = (const unsigned int *)src;
123 
124  for (; w; w--) {
125  *d++ = (unsigned short)(((*s & 0xFF) >> 3) | ((*s >> 5) & 0x07E0) | ((*s >> 8) & 0xF800));
126  s++;
127  }
128 }
129 
130 /*:::::*/
131 void fb_image_convert_32bgrto32(const unsigned char *src, unsigned char *dest, int w)
132 {
133  fb_hMemCpy(dest, src, w << 2);
134 }
135 
136 /*:::::*/
137 FBCALL void fb_GfxImageConvertRow( const unsigned char *src, int src_bpp,
138  unsigned char *dest, int dst_bpp,
139  int width, int isrgb )
140 {
141  FBGFX_IMAGE_CONVERT convert = NULL;
142 
143  if (src_bpp <= 8)
144  {
145  switch (BYTES_PER_PIXEL( dst_bpp ))
146  {
147  case 1: convert = fb_image_convert_8to8; break;
148  case 2: convert = fb_image_convert_8to16; break;
149  case 3:
150  case 4: convert = fb_image_convert_8to32; break;
151  }
152  }
153  else if (src_bpp == 24)
154  {
155  switch (BYTES_PER_PIXEL( dst_bpp ))
156  {
157  case 1: return;
158  case 2: convert = (isrgb != 0? fb_image_convert_24to16: fb_image_convert_24bgrto16); break;
159  case 3:
160  case 4: convert = (isrgb != 0? fb_image_convert_24to32: fb_image_convert_24bgrto32); break;
161  }
162  }
163  else if (src_bpp == 32)
164  {
165  switch (BYTES_PER_PIXEL( dst_bpp ))
166  {
167  case 1: return;
168  case 2: convert = (isrgb != 0? fb_image_convert_32to16: fb_image_convert_32bgrto16); break;
169  case 3:
170  case 4: convert = (isrgb != 0? fb_image_convert_32to32: fb_image_convert_32bgrto32); break;
171  }
172  }
173  else
174  return;
175 
176  convert( src, dest, width );
177 }