COLOR.C
[目次 | 関数]
1|/**************************************************************************
2| 1. <<< 色 (Color) >>>
3|
4|・色コード。RGB カラーコード、YUV カラーコード。
5|・BPP の変換や、RGB-YUV 変換をします。
6|・色の厳密さ(量子化の度合)は、bpp (Bit Per Pixel) という単位です。
7|**************************************************************************/
8|
9|#include "mixer_precomp.h" /* Auto precompiled header, Look at mixer-... folder */
10|// #pragma hdrstop
11|
12|#if defined(USES_MXP_AUTOINC)
13| #include "color.ah" /* Auto include header, Look at mixer-... folder */
14|#endif
15|
16|
17|/*--------------------------------------------------------------------*/
18|/* 2. <<<◆ カラーコード定数 >>> */
19|/*--------------------------------------------------------------------*/
20|Color Color_Black( int type, int bpp )
21|{
22| ASSERT( type == Color_RGBType );
23|
24| switch ( bpp ) {
25| case 8: return Color_RGB8_Black;
26| case 16: return Color_RGB16_Black;
27| case 24: return Color_RGB24_Black;
28| }
29| ASSERT(1);
30| return 0;
31|}
32|
33|
34|Color Color_Blue( int type, int bpp )
35|{
36| ASSERT( type == Color_RGBType );
37|
38| switch ( bpp ) {
39| case 8: return Color_RGB8_Blue;
40| case 16: return Color_RGB16_Blue;
41| case 24: return Color_RGB24_Blue;
42| }
43| ASSERT(1);
44| return 0;
45|}
46|
47|
48|Color Color_Green( int type, int bpp )
49|{
50| ASSERT( type == Color_RGBType );
51|
52| switch ( bpp ) {
53| case 8: return Color_RGB8_Green;
54| case 16: return Color_RGB16_Green;
55| case 24: return Color_RGB24_Green;
56| }
57| ASSERT(1);
58| return 0;
59|}
60|
61|
62|Color Color_Red( int type, int bpp )
63|{
64| ASSERT( type == Color_RGBType );
65|
66| switch ( bpp ) {
67| case 8: return Color_RGB8_Red;
68| case 16: return Color_RGB16_Red;
69| case 24: return Color_RGB24_Red;
70| }
71| ASSERT(1);
72| return 0;
73|}
74|
75|
76|Color Color_Cyan( int type, int bpp )
77|{
78| ASSERT( type == Color_RGBType );
79|
80| switch ( bpp ) {
81| case 8: return Color_RGB8_Cyan;
82| case 16: return Color_RGB16_Cyan;
83| case 24: return Color_RGB24_Cyan;
84| }
85| ASSERT(1);
86| return 0;
87|}
88|
89|
90|Color Color_Mazenta( int type, int bpp )
91|{
92| ASSERT( type == Color_RGBType );
93|
94| switch ( bpp ) {
95| case 8: return Color_RGB8_Mazenta;
96| case 16: return Color_RGB16_Mazenta;
97| case 24: return Color_RGB24_Mazenta;
98| }
99| ASSERT(1);
100| return 0;
101|}
102|
103|
104|Color Color_Yellow( int type, int bpp )
105|{
106| ASSERT( type == Color_RGBType );
107|
108| switch ( bpp ) {
109| case 8: return Color_RGB8_Yellow;
110| case 16: return Color_RGB16_Yellow;
111| case 24: return Color_RGB24_Yellow;
112| }
113| ASSERT(1);
114| return 0;
115|}
116|
117|
118|Color Color_White( int type, int bpp )
119|{
120| ASSERT( type == Color_RGBType );
121|
122| switch ( bpp ) {
123| case 8: return Color_RGB8_White;
124| case 16: return Color_RGB16_White;
125| case 24: return Color_RGB24_White;
126| }
127| ASSERT(1);
128| return 0;
129|}
130|
131|
132|Color Color_Gray( int type, int bpp )
133|{
134| ASSERT( type == Color_RGBType );
135|
136| switch ( bpp ) {
137| case 8: return Color_RGB8_Gray;
138| case 16: return Color_RGB16_Gray;
139| case 24: return Color_RGB24_Gray;
140| }
141| ASSERT(1);
142| return 0;
143|}
144|
145|
146|
147|/*--------------------------------------------------------------------*/
148|/* 3. <<<◆ (Color_Tester) カラーテスター >>> */
149|/*--------------------------------------------------------------------*/
150|
151|/***********************************************************************
152| 4. <<< [Color_Tester_init] 初期化する >>>
153|************************************************************************/
154|void Color_Tester_init( Color_Tester* m, int bpp )
155|{
156| ASSERT( bpp == 8 || bpp == 16 );
157|
158| m->count = 0;
159| m->bpp = bpp;
160|}
161|
162|
163|/***********************************************************************
164| 5. <<< [Color_Tester_getNextColor] 次の色を返す >>>
165|************************************************************************/
166|Color Color_Tester_getNextColor( Color_Tester* m )
167|{
168| Color ret;
169|
170| if ( m->bpp == 8 ) {
171| ret = 0xFF - (m->count & 0xFF);
172| }
173| else { /* bpp == 16 */
174| int c = m->count % 0x10008;
175|
176| if ( c < 8 )
177| ret = Color_RGB3_getRGB16( 7 - c );
178| else
179| ret = 0xFFFF - (c - 8);
180| }
181|
182| m->count ++;
183|
184| return ret;
185|}
186|
187|
188|
189|/*--------------------------------------------------------------------*/
190|/* 6. <<<◆ (Color_RGB24) RGB カラー 24bpp >>> */
191|/*--------------------------------------------------------------------*/
192|
193|/***********************************************************************
194| 7. <<< [Color_RGB24_init] 24bpp カラーを得る(関数版)>>>
195|************************************************************************/
196|#ifndef NDEBUG
197|Color_RGB24 Color_RGB24_init( int r, int g, int b )
198|{
199| ASSERT( r >= 0 && r <= 255 );
200| ASSERT( g >= 0 && g <= 255 );
201| ASSERT( b >= 0 && b <= 255 );
202|
203| return Color_RGB24_init_m( r, g, b );
204|}
205|#endif
206|
207|
208|/**************************************************************************
209| 8. <<< YUV カラーの Y を得る(0〜255)(固定小数版)[Color_RGB24_getY2()] >>>
210|***************************************************************************/
211|int Color_RGB24_getY2( Color_RGB24 color )
212|{
213| int y =
214| ( ( 306 * (int)Color_RGB24_getR(color) + 512 ) >> 10 )
215| + ( ( 601 * (int)Color_RGB24_getG(color) + 512 ) >> 10 )
216| + ( ( 117 * (int)Color_RGB24_getB(color) + 512 ) >> 10 );
217|
218| if ( y < 0x00 ) y = 0x00;
219| if ( y > 0xFF ) y = 0xFF;
220| return y;
221|}
222|
223|
224|/**************************************************************************
225| 9. <<< YUV カラーの U(Cb) を得る(-128〜127)(固定小数版)[Color_RGB24_getU2()] >>>
226|***************************************************************************/
227|int Color_RGB24_getU2( Color_RGB24 color )
228|{
229| int u =
230| - ( ( 172 * (int)Color_RGB24_getR(color) + 512 ) >> 10 )
231| - ( ( 340 * (int)Color_RGB24_getG(color) + 512 ) >> 10 )
232| + ( ( 512 * (int)Color_RGB24_getB(color) + 512 ) >> 10 );
233|
234| if ( u < -128 ) u = -128;
235| if ( u > 127 ) u = 127;
236| return u;
237|}
238|
239|
240|/**************************************************************************
241| 10. <<< YUV カラーの V(Cr) を得る(-128〜127)(固定小数版)[Color_RGB24_getV2()] >>>
242|***************************************************************************/
243|int Color_RGB24_getV2( Color_RGB24 color )
244|{
245| int v =
246| ( ( 512 * (int)Color_RGB24_getR(color) + 512 ) >> 10 )
247| - ( ( 429 * (int)Color_RGB24_getG(color) + 512 ) >> 10 )
248| - ( ( 83 * (int)Color_RGB24_getB(color) + 512 ) >> 10 );
249|
250| if ( v < -128 ) v = -128;
251| if ( v > 127 ) v = 127;
252| return v;
253|}
254|
255|
256|/**************************************************************************
257| 11. <<< RGB カラーから YUV カラーに変換(浮動小数版)[Color_RGB24_getYUV()] >>>
258|***************************************************************************/
259|#ifndef FOR_NO_FLOAT
260|Color_YUV Color_RGB24_getYUV( Color_RGB24 color )
261|{
262| return ( ((int)Color_RGB24_getV( color ) + 0x80) << 16 ) +
263| ( ((int)Color_RGB24_getU( color ) + 0x80) << 8 ) +
264| (int)Color_RGB24_getY( color );
265|}
266|#endif
267|
268|
269|
270|/**************************************************************************
271| 12. <<< [Color_RGB24_makeShadPalet] シェーディングパレット設定用の色の配列を作る >>>
272|【引数】
273| ・Color_RGB24* palet; 色の配列(出力、要素数は palet_n 以上を用意)
274| ・Color_RGB24 start, end; シェーディングの開始色、終了色
275| ・int palet_n; 出力する色の数
276|***************************************************************************/
277|#ifdef USES_BRESEN
278|void Color_RGB24_makeShadPalet( Color_RGB24* palet,
279| Color_RGB24 start, Color_RGB24 end, int palet_n )
280|{
281| int r, g, b;
282| Bresen_Ex red_plus, green_plus, blue_plus;
283| int palet_diff = palet_n - 1;
284| Color_RGB24* palet_last = palet + palet_diff;
285|
286| ASSERT( palet_n > 0 );
287|
288| r = Color_RGB24_getR( start );
289| g = Color_RGB24_getG( start );
290| b = Color_RGB24_getB( start );
291| Bresen_Ex_init( &red_plus, palet_diff, Color_RGB24_getR( end ) - r );
292| Bresen_Ex_init( &green_plus, palet_diff, Color_RGB24_getG( end ) - g );
293| Bresen_Ex_init( &blue_plus, palet_diff, Color_RGB24_getB( end ) - b );
294|
295| for (;;) {
296| *palet = Color_RGB24_init( r, g, b );
297| if ( palet >= palet_last ) break;
298|
299| palet ++;
300| r += Bresen_Ex_next( &red_plus );
301| g += Bresen_Ex_next( &green_plus );
302| b += Bresen_Ex_next( &blue_plus );
303| }
304|}
305|#endif
306|
307|/*--------------------------------------------------------------------*/
308|/* 13. <<<◆ (Color_YUV) YUV カラー 24bpp >>> */
309|/*--------------------------------------------------------------------*/
310|
311|/**************************************************************************
312| 14. <<< RGB カラーの Red を得る(固定小数版)[Color_YUV_getR2()] >>>
313|***************************************************************************/
314|int Color_YUV_getR2( Color_YUV color )
315|{
316| int r = Color_YUV_getY(color)
317| + ( (1436 * Color_YUV_getV(color) + 512) >> 10 );
318|
319| if ( r > 0xFF ) r = 0xFF;
320| if ( r < 0x00 ) r = 0x00;
321| return r;
322|}
323|
324|
325|/**************************************************************************
326| 15. <<< RGB カラーの Green を得る(固定小数版)[Color_YUV_getG2()] >>>
327|***************************************************************************/
328|int Color_YUV_getG2( Color_YUV color )
329|{
330| int g = Color_YUV_getY(color)
331| - ( ( 352 * Color_YUV_getU(color) + 512 ) >> 10 )
332| - ( ( 731 * Color_YUV_getV(color) + 512 ) >> 10 );
333|
334| if ( g < 0x00 ) g = 0x00;
335| if ( g > 0xFF ) g = 0xFF;
336| return g;
337|}
338|
339|
340|/**************************************************************************
341| 16. <<< RGB カラーの Blue を得る(固定小数版)[Color_YUV_getB2()] >>>
342|***************************************************************************/
343|int Color_YUV_getB2( Color_YUV color )
344|{
345| int b = Color_YUV_getY(color)
346| + ( ( 1814 * Color_YUV_getU(color) + 512 ) >> 10 )
347| - ( ( Color_YUV_getV(color) + 512 ) >> 10 );
348|
349| if ( b < 0x00 ) b = 0x00;
350| if ( b > 0xFF ) b = 0xFF;
351| return b;
352|}
353|
354|
355|/**************************************************************************
356| 17. <<< YUV カラーから RGB カラーに変換(浮動小数版)[Color_YUV_getRGB()] >>>
357|***************************************************************************/
358|#ifndef FOR_NO_FLOAT
359|Color_RGB24 Color_YUV_getRGB24( Color_YUV color )
360|{
361| int r, g, b;
362|
363| r = (int)Color_YUV_getR( color );
364| g = (int)Color_YUV_getG( color );
365| b = (int)Color_YUV_getB( color );
366|
367| if ( r < 0 ) r = 0;
368| else if ( r > 255 ) r = 255;
369| if ( g < 0 ) g = 0;
370| else if ( g > 255 ) g = 255;
371| if ( b < 0 ) b = 0;
372| else if ( b > 255 ) b = 255;
373|
374| return (r << 16) + (g << 8) + b;
375|}
376|#endif
377|
378|
379|
380|/*--------------------------------------------------------------------*/
381|/* 18. <<<◆ (Color_RGB16) RGB カラー 16bpp >>> */
382|/*--------------------------------------------------------------------*/
383|
384|/***********************************************************************
385| 19. <<< 16bpp カラーを得る [Color_RGB16_init] >>>
386|************************************************************************/
387|#ifndef NDEBUG
388|Color_RGB16 Color_RGB16_init( int r, int g, int b )
389|{
390| ASSERT( r >= 0 && r <= 31 && g >= 0 && g <= 63 && b >= 0 && b <= 31 );
391| return ( ((r) << 11) | ((g) << 5) | (b) );
392|}
393|#endif
394|
395|
396|/***********************************************************************
397| 20. <<< Red を得る(0〜31)[Color_RGB16_getR] >>>
398|************************************************************************/
399|#ifndef NDEBUG
400|int Color_RGB16_getR( Color_RGB16 color )
401|{
402| ASSERT( color >= 0 && color <= 0xFFFF );
403| return ( (color) >> 11 );
404|}
405|#endif
406|
407|/*--------------------------------------------------------------------*/
408|/* 21. <<<◆ (Color_RGB3) RGB カラー 3bpp >>> */
409|/*--------------------------------------------------------------------*/
410|
411|/**************************************************************************
412| 22. <<< 3bpp(8色) を 8bpp(256色,RRRGGGBB) に変換する [Color_getBps3to8()] >>>
413|***************************************************************************/
414|Color Color_getBps3to8( Color c3 )
415|{
416| Color c = 0;
417|
418| if ( c3 & 0x4 ) c += 0xE0;
419| if ( c3 & 0x2 ) c += 0x1C;
420| if ( c3 & 0x1 ) c += 0x03;
421|
422| return c;
423|}
424|
425|
426|
427|