]> git.sesse.net Git - vlc/blob - modules/codec/ogt/pixmap.h
Big/little-endian bug.
[vlc] / modules / codec / ogt / pixmap.h
1 /*****************************************************************************
2  * Common pixel/chroma manipulation routines.
3  *****************************************************************************
4  * Copyright (C) 2003, 2004 VideoLAN
5  * $Id: pixmap.h,v 1.6 2004/01/31 23:33:02 rocky Exp $
6  *
7  * Author: Rocky Bernstein
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifndef PIXMAP_H
25 #define PIXMAP_H 
26
27 /** Color and transparency of a pixel or a palette (CLUT) entry 
28 */
29 typedef union {
30   uint8_t plane[4];
31   struct {
32     uint8_t y;
33     uint8_t v;
34     uint8_t u;
35     uint8_t t;
36   } s;
37 } ogt_yuvt_t;
38
39 /** An undefined or invalid colormap index. */
40 #define INVALID_CMAP_ENTRY -1
41
42 /** Type of a palette/colormap index*/
43 typedef int16_t cmap_t;
44
45 /** Number of entries in RGB palette/colormap*/
46 #define CMAP_RGB2_SIZE 256
47
48 /**
49    Force v in the range 0..255. In video_chroma/i420_rgb.c, this
50    is macro is called CLIP. FIXME: Combine with that.
51 */
52 #define clip_8_bit(v) \
53   ((v < 0) ? 0 : (v > 255) ? 255 : v)
54
55 /**
56    Color conversion from
57     http://www.inforamp.net/~poynton/notes/colour_and_gamma/ColorFAQ.html#RTFToC30
58     http://people.ee.ethz.ch/~buc/brechbuehler/mirror/color/ColorFAQ.html
59  
60     Thanks to Billy Biggs <vektor@dumbterm.net> for the pointer and
61     the following conversion.
62  
63     R' = [ 1.1644         0    1.5960 ]   ([ Y' ]   [  16 ])
64     G' = [ 1.1644   -0.3918   -0.8130 ] * ([ Cb ] - [ 128 ])
65     B' = [ 1.1644    2.0172         0 ]   ([ Cr ]   [ 128 ])
66
67   See also vlc/modules/video_chroma/i420_rgb.h and
68   vlc/modules/video_chroma/i420_rgb_c.h for a way to do this in a way
69   more optimized for integer arithmetic. Would be nice to merge the
70   two routines.
71  
72 */
73
74 /**
75    Convert a YUV pixel into an RGB pixel.
76  */
77 static inline void
78 yuv2rgb(const ogt_yuvt_t *p_yuv, uint8_t *p_rgb_out )
79 {
80   
81   int i_Y  = p_yuv->s.y - 16;
82   int i_Cb = p_yuv->s.v - 128;
83   int i_Cr = p_yuv->s.u - 128;
84   
85   int i_red   = (1.1644 * i_Y) + (1.5960 * i_Cr);
86   int i_green = (1.1644 * i_Y) - (0.3918 * i_Cb) - (0.8130 * i_Cr);
87   int i_blue  = (1.1644 * i_Y) + (2.0172 * i_Cb);
88   
89   i_red   = clip_8_bit( i_red );
90   i_green = clip_8_bit( i_green );
91   i_blue  = clip_8_bit( i_blue );
92   
93 #ifdef WORDS_BIGENDIAN
94   *p_rgb_out++ = i_red;
95   *p_rgb_out++ = i_green;
96   *p_rgb_out++ = i_blue;
97 #else
98   *p_rgb_out++ = i_blue;
99   *p_rgb_out++ = i_green;
100   *p_rgb_out++ = i_red;
101 #endif
102   
103 }
104
105 /* The byte storage of an RGB pixel. */ 
106 #define RGB_SIZE 3
107
108 #define GREEN_PIXEL 1
109 #ifdef WORDS_BIGENDIAN
110 #define RED_PIXEL   0
111 #define BLUE_PIXEL  2
112 #else
113 #define RED_PIXEL   2
114 #define BLUE_PIXEL  0
115 #endif
116
117 /**
118    Store an RGB pixel into the location of p_pixel, taking into
119    account the "Endian"-ness of the underlying machine.
120
121    (N.B. Not sure if I've got this right or this is the right thing
122    to do.)
123  */
124 static inline void
125 put_rgb24_pixel(const uint8_t *rgb, /*out*/ uint8_t *p_pixel)
126 {
127 #ifdef WORDS_BIGENDIAN
128   *p_pixel++;
129 #endif
130   *p_pixel++ = rgb[RED_PIXEL];
131   *p_pixel++ = rgb[GREEN_PIXEL];
132   *p_pixel++ = rgb[BLUE_PIXEL];
133 }
134
135 /**
136    Find the nearest colormap entry in p_vout (assumed to have RGB2
137    chroma, i.e. 256 RGB 8bpp entries) that is closest in color to p_rgb.  Set
138    out_rgb to the color found and return the colormap index. 
139    INVALID_CMAP_ENTRY is returned if there is some error.
140 */
141 cmap_t
142 find_cmap_rgb8_nearest(const vout_thread_t *p_vout, const uint8_t *p_rgb,
143                        /*out*/ uint8_t *out_rgb);
144
145 /**
146    Get the the rgb value for a given colormap entry for p_vout (which is'
147    assumed to have RGB2 chroma). 
148    
149    VLC_FALSE is returned if there was some error.
150 */
151 vlc_bool_t
152 query_color(const vout_thread_t *p_vout, cmap_t i_cmap,
153             /*out*/ uint8_t *rgb);
154
155 #endif /* PIXMAP_H */
156
157 \f
158 /* 
159  * Local variables:
160  *  c-file-style: "gnu"
161  *  tab-width: 8
162  *  indent-tabs-mode: nil
163  * End:
164  */