Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 33534:22c3241467b3
Remove function Normalize().
This function changed ARGB data as if it had been RGB24 data (not quite
correctly) converted to ARGB with all colors transparent.
Instead now, set the alpha channel for RGB24 data and leave ARGB data
untouched.
For X11, the alpha channel is disregarded, so both approaches are equal,
but the new one is more intelligible as we get correct ARGB PNG data.
For legacy reasons, all kind of fuchsia/magenta must be treated as
transparent, because some skins are using at least both full opaque and
full transparent fuchsia/magenta for transparency.
author | ib |
---|---|
date | Thu, 16 Jun 2011 11:19:15 +0000 |
parents | fd90a10d81a1 |
children | c5a19bbeac2b |
rev | line source |
---|---|
33046 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
4 * MPlayer is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * MPlayer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License along | |
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 */ | |
18 | |
19 #include <stdio.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
33130 | 22 #include <unistd.h> |
33046 | 23 |
24 #include "bitmap.h" | |
25 | |
26 #include "help_mp.h" | |
27 #include "libavcodec/avcodec.h" | |
28 #include "libavutil/intreadwrite.h" | |
29 #include "libvo/fastmemcpy.h" | |
30 #include "mp_msg.h" | |
31 | |
32 static int pngRead(unsigned char *fname, txSample *bf) | |
33 { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
34 FILE *file; |
33134 | 35 long len; |
33046 | 36 void *data; |
33135 | 37 int decode_ok, bpl; |
33046 | 38 AVCodecContext *avctx; |
39 AVFrame *frame; | |
40 AVPacket pkt; | |
41 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
42 file = fopen(fname, "rb"); |
33046 | 43 |
33137 | 44 if (!file) |
45 return 2; | |
33046 | 46 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
47 fseek(file, 0, SEEK_END); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
48 len = ftell(file); |
33046 | 49 |
50 if (len > 50 * 1024 * 1024) { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
51 fclose(file); |
33137 | 52 return 3; |
33046 | 53 } |
54 | |
55 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); | |
56 | |
33135 | 57 if (!data) { |
58 fclose(file); | |
33137 | 59 return 4; |
33135 | 60 } |
61 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
62 fseek(file, 0, SEEK_SET); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
63 fread(data, len, 1, file); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
64 fclose(file); |
33046 | 65 |
66 avctx = avcodec_alloc_context(); | |
67 frame = avcodec_alloc_frame(); | |
33139 | 68 |
69 if (!(avctx && frame)) { | |
70 av_free(frame); | |
71 av_free(avctx); | |
72 av_free(data); | |
73 return 5; | |
74 } | |
75 | |
33046 | 76 avcodec_register_all(); |
77 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG)); | |
33139 | 78 |
33046 | 79 av_init_packet(&pkt); |
80 pkt.data = data; | |
81 pkt.size = len; | |
82 // HACK: make PNGs decode normally instead of as CorePNG delta frames | |
83 pkt.flags = AV_PKT_FLAG_KEY; | |
33139 | 84 |
33046 | 85 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); |
33134 | 86 |
33046 | 87 memset(bf, 0, sizeof(*bf)); |
88 | |
89 switch (avctx->pix_fmt) { | |
90 case PIX_FMT_GRAY8: | |
91 bf->BPP = 8; | |
92 break; | |
93 | |
94 case PIX_FMT_GRAY16BE: | |
95 bf->BPP = 16; | |
96 break; | |
97 | |
98 case PIX_FMT_RGB24: | |
99 bf->BPP = 24; | |
100 break; | |
101 | |
102 case PIX_FMT_BGRA: | |
103 case PIX_FMT_ARGB: | |
104 bf->BPP = 32; | |
105 break; | |
106 | |
107 default: | |
108 bf->BPP = 0; | |
109 break; | |
110 } | |
111 | |
112 if (decode_ok && bf->BPP) { | |
113 bf->Width = avctx->width; | |
114 bf->Height = avctx->height; | |
115 bpl = bf->Width * (bf->BPP / 8); | |
116 bf->ImageSize = bpl * bf->Height; | |
33136 | 117 |
118 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname); | |
119 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %lux%lu, color depth: %u\n", bf->Width, bf->Height, bf->BPP); | |
120 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", bf->ImageSize); | |
121 | |
122 bf->Image = malloc(bf->ImageSize); | |
33135 | 123 |
124 if (bf->Image) | |
125 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]); | |
33137 | 126 else |
127 decode_ok = 0; | |
33046 | 128 } |
129 | |
130 avcodec_close(avctx); | |
33138 | 131 av_free(frame); |
132 av_free(avctx); | |
133 av_free(data); | |
33046 | 134 |
135 return !(decode_ok && bf->BPP); | |
136 } | |
137 | |
33121 | 138 static int Convert24to32(txSample *bf) |
33046 | 139 { |
33132 | 140 char *orgImage; |
141 unsigned long i, c; | |
33046 | 142 |
143 if (bf->BPP == 24) { | |
33132 | 144 orgImage = bf->Image; |
145 | |
33046 | 146 bf->BPP = 32; |
147 bf->ImageSize = bf->Width * bf->Height * 4; | |
148 bf->Image = calloc(1, bf->ImageSize); | |
149 | |
150 if (!bf->Image) { | |
33132 | 151 free(orgImage); |
33112 | 152 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", bf->ImageSize); |
33121 | 153 return 0; |
33046 | 154 } |
155 | |
33112 | 156 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %lu\n", bf->ImageSize); |
157 | |
33046 | 158 for (c = 0, i = 0; c < bf->ImageSize; c += 4, i += 3) |
33534 | 159 *(uint32_t *)&bf->Image[c] = ALPHA_OPAQUE | AV_RB24(&orgImage[i]); |
33046 | 160 |
33132 | 161 free(orgImage); |
33046 | 162 } |
163 | |
33121 | 164 return 1; |
33046 | 165 } |
166 | |
167 static unsigned char *fExist(unsigned char *fname) | |
168 { | |
33141 | 169 static const char ext[][4] = { "png", "PNG" }; |
33130 | 170 static unsigned char buf[512]; |
171 unsigned int i; | |
33046 | 172 |
33130 | 173 if (access(fname, R_OK) == 0) |
33046 | 174 return fname; |
175 | |
33130 | 176 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { |
177 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
33046 | 178 |
33130 | 179 if (access(buf, R_OK) == 0) |
180 return buf; | |
33046 | 181 } |
182 | |
183 return NULL; | |
184 } | |
185 | |
186 int bpRead(char *fname, txSample *bf) | |
187 { | |
33137 | 188 int r; |
189 | |
33046 | 190 fname = fExist(fname); |
191 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
192 if (!fname) |
33046 | 193 return -2; |
194 | |
33137 | 195 r = pngRead(fname, bf); |
196 | |
197 if (r != 0) { | |
198 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error #%d: %s\n", r, fname); | |
33046 | 199 return -5; |
200 } | |
201 | |
202 if (bf->BPP < 24) { | |
33112 | 203 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", bf->BPP); |
33046 | 204 return -1; |
205 } | |
206 | |
33121 | 207 if (!Convert24to32(bf)) |
33046 | 208 return -8; |
209 | |
210 return 0; | |
211 } | |
212 | |
213 void bpFree(txSample *bf) | |
214 { | |
215 free(bf->Image); | |
216 memset(bf, 0, sizeof(*bf)); | |
217 } | |
218 | |
33129 | 219 int Convert32to1(txSample *in, txSample *out) |
33046 | 220 { |
33118 | 221 uint32_t *buf; |
222 unsigned long i; | |
223 int b = 0, c = 0; | |
224 unsigned char tmp = 0; | |
225 int shaped = 0; | |
226 | |
33046 | 227 out->Width = in->Width; |
228 out->Height = in->Height; | |
229 out->BPP = 1; | |
230 out->ImageSize = (out->Width * out->Height + 7) / 8; | |
33118 | 231 out->Image = calloc(1, out->ImageSize); |
33046 | 232 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
233 if (!out->Image) { |
33114 | 234 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
235 return 0; | |
33125 | 236 } |
237 | |
33126 | 238 buf = (uint32_t *)in->Image; |
33046 | 239 |
33126 | 240 for (i = 0; i < out->Width * out->Height; i++) { |
241 tmp >>= 1; | |
33119 | 242 |
33534 | 243 if (!IS_TRANSPARENT(buf[i])) |
33126 | 244 tmp |= 0x80; |
33128 | 245 else { |
246 buf[i] = 0; | |
33126 | 247 shaped = 1; |
33128 | 248 } |
33046 | 249 |
33126 | 250 if (++b == 8) { |
251 out->Image[c++] = tmp; | |
252 tmp = b = 0; | |
253 } | |
254 } | |
33112 | 255 |
33126 | 256 if (b) |
257 out->Image[c] = tmp; | |
258 | |
259 if (!shaped) | |
260 bpFree(out); | |
33114 | 261 |
33133 | 262 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
263 | |
33114 | 264 return 1; |
33046 | 265 } |