annotate libmpcodecs/vf_palette.c @ 7134:75ff2ec3b086

arrg 100l in the 10l
author rfelker
date Thu, 29 Aug 2002 02:20:00 +0000
parents 1e47c2e7aa8e
children 447066802e64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5774
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
1 #include <stdio.h>
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
2 #include <stdlib.h>
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
3 #include <string.h>
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
4 #include <inttypes.h>
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
5
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
6 #include "../config.h"
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
7 #include "../mp_msg.h"
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
8
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
9 #include "img_format.h"
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
10 #include "mp_image.h"
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
11 #include "vf.h"
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
12
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
13 #include "../postproc/rgb2rgb.h"
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
14
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
15 //===========================================================================//
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
16
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
17 static unsigned int bgr_list[]={
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
18 IMGFMT_BGR32,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
19 IMGFMT_BGR24,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
20 IMGFMT_BGR16,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
21 IMGFMT_BGR15,
6188
39a9515c633a warning fixes, patch by Ulrich Hecht <uli@suse.de>
arpi
parents: 5774
diff changeset
22 0
5774
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
23 };
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
24 static unsigned int rgb_list[]={
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
25 IMGFMT_RGB32,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
26 IMGFMT_RGB24,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
27 IMGFMT_RGB16,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
28 IMGFMT_RGB15,
6188
39a9515c633a warning fixes, patch by Ulrich Hecht <uli@suse.de>
arpi
parents: 5774
diff changeset
29 0
5774
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
30 };
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
31
6232
f8e0eac1256c grayscaled palette support
arpi
parents: 6188
diff changeset
32 static unsigned int gray_pal[256];
f8e0eac1256c grayscaled palette support
arpi
parents: 6188
diff changeset
33
5774
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
34 static unsigned int find_best(struct vf_instance_s* vf, unsigned int fmt){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
35 unsigned int best=0;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
36 int ret;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
37 unsigned int* p;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
38 if(fmt==IMGFMT_BGR8) p=bgr_list;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
39 else if(fmt==IMGFMT_RGB8) p=rgb_list;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
40 else return 0;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
41 while(*p){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
42 ret=vf->next->query_format(vf->next,*p);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
43 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
44 if(ret&2){ best=*p; break;} // no conversion -> bingo!
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
45 if(ret&1 && !best) best=*p; // best with conversion
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
46 ++p;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
47 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
48 return best;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
49 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
50
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
51 //===========================================================================//
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
52
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
53 struct vf_priv_s {
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
54 unsigned int fmt;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
55 };
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
56
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
57 static int config(struct vf_instance_s* vf,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
58 int width, int height, int d_width, int d_height,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
59 unsigned int flags, unsigned int outfmt){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
60 vf->priv->fmt=find_best(vf,outfmt);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
61 if(!vf->priv->fmt){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
62 // no matching fmt, so force one...
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
63 if(outfmt==IMGFMT_RGB8) vf->priv->fmt=IMGFMT_RGB32;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
64 else if(outfmt==IMGFMT_BGR8) vf->priv->fmt=IMGFMT_BGR32;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
65 else return 0;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
66 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
67 return vf_next_config(vf,width,height,d_width,d_height,flags,vf->priv->fmt);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
68 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
69
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
70 static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
71 mp_image_t *dmpi;
6481
24465b749d2a report if no palette
alex
parents: 6232
diff changeset
72
24465b749d2a report if no palette
alex
parents: 6232
diff changeset
73 if (!mpi->planes[1])
24465b749d2a report if no palette
alex
parents: 6232
diff changeset
74 {
24465b749d2a report if no palette
alex
parents: 6232
diff changeset
75 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] no palette given\n",vf->info->name);
24465b749d2a report if no palette
alex
parents: 6232
diff changeset
76 return;
24465b749d2a report if no palette
alex
parents: 6232
diff changeset
77 }
5774
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
78
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
79 // hope we'll get DR buffer:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
80 dmpi=vf_get_image(vf->next,vf->priv->fmt,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
81 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
82 mpi->w, mpi->h);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
83
7127
1e47c2e7aa8e mostly compiler warning fixes, some small bugfix
arpi
parents: 6481
diff changeset
84 if(!mpi->planes[1]) mpi->planes[1]=(unsigned char*)gray_pal;
6232
f8e0eac1256c grayscaled palette support
arpi
parents: 6188
diff changeset
85
5774
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
86 if(mpi->w==mpi->stride[0] && dmpi->w*(dmpi->bpp>>3)==dmpi->stride[0]){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
87 // no stride conversion needed
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
88 switch(dmpi->imgfmt&255){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
89 case 15:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
90 palette8torgb15(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
91 break;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
92 case 16:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
93 palette8torgb16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
94 break;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
95 case 24:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
96 palette8torgb24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
97 break;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
98 case 32:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
99 palette8torgb32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
100 break;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
101 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
102 } else {
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
103 int y;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
104 for(y=0;y<mpi->h;y++){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
105 unsigned char* src=mpi->planes[0]+y*mpi->stride[0];
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
106 unsigned char* dst=dmpi->planes[0]+y*dmpi->stride[0];
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
107 switch(dmpi->imgfmt&255){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
108 case 15:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
109 palette8torgb15(src,dst,mpi->w,mpi->planes[1]);break;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
110 case 16:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
111 palette8torgb16(src,dst,mpi->w,mpi->planes[1]);break;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
112 case 24:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
113 palette8torgb24(src,dst,mpi->w,mpi->planes[1]);break;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
114 case 32:
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
115 palette8torgb32(src,dst,mpi->w,mpi->planes[1]);break;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
116 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
117 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
118 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
119
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
120 vf_next_put_image(vf,dmpi);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
121 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
122
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
123 //===========================================================================//
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
124
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
125 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
126 int best=find_best(vf,fmt);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
127 if(!best) return 0; // no match
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
128 return vf->next->query_format(vf->next,best);
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
129 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
130
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
131 static int open(vf_instance_t *vf, char* args){
6232
f8e0eac1256c grayscaled palette support
arpi
parents: 6188
diff changeset
132 unsigned int i;
5774
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
133 vf->config=config;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
134 vf->put_image=put_image;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
135 vf->query_format=query_format;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
136 vf->priv=malloc(sizeof(struct vf_priv_s));
6232
f8e0eac1256c grayscaled palette support
arpi
parents: 6188
diff changeset
137 for(i=0;i<256;i++) gray_pal[i]=0x01010101*i;
5774
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
138 return 1;
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
139 }
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
140
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
141 vf_info_t vf_info_palette = {
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
142 "8bpp indexed (using palette) -> BGR 15/16/24/32 conversion",
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
143 "palette",
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
144 "A'rpi",
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
145 "",
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
146 open
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
147 };
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
148
a300966abeb6 8bpp paletted -> 15/16/24/32 converter
arpi
parents:
diff changeset
149 //===========================================================================//