Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 33985:d99f341d8442
Replace all mp_dbg() calls by mp_msg().
These messages are useful to track down error causes and should
be available even in a non-debug version of the GUI.
Additionally, remove #ifdef MP_DEBUG which isn't longer needed
in skin.c now.
author | ib |
---|---|
date | Tue, 06 Sep 2011 15:28:53 +0000 |
parents | 60449f4234f7 |
children | b0c63b098b6b |
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 | |
33984 | 19 /** |
20 * @file | |
21 * @brief Image loader and bitmap mask rendering | |
22 */ | |
23 | |
33046 | 24 #include <stdio.h> |
25 #include <stdlib.h> | |
26 #include <string.h> | |
33130 | 27 #include <unistd.h> |
33046 | 28 |
29 #include "bitmap.h" | |
30 | |
31 #include "help_mp.h" | |
32 #include "libavcodec/avcodec.h" | |
33690 | 33 #include "libavutil/common.h" |
33046 | 34 #include "libavutil/intreadwrite.h" |
35 #include "libvo/fastmemcpy.h" | |
36 #include "mp_msg.h" | |
37 | |
33984 | 38 /** |
39 * @brief Read and decode a PNG file into bitmap data. | |
40 * | |
41 * @param fname filename (with path) | |
42 * @param img pointer suitable to store the image data | |
43 * | |
44 * @return 0 (ok), 1 (decoding error), 2 (open error), 3 (file too big), | |
45 * 4 (out of memory), 5 (avcodec alloc error) | |
46 */ | |
33983 | 47 static int pngRead(const char *fname, guiImage *img) |
33046 | 48 { |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
49 FILE *file; |
33134 | 50 long len; |
33046 | 51 void *data; |
33135 | 52 int decode_ok, bpl; |
33046 | 53 AVCodecContext *avctx; |
54 AVFrame *frame; | |
55 AVPacket pkt; | |
56 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
57 file = fopen(fname, "rb"); |
33046 | 58 |
33137 | 59 if (!file) |
60 return 2; | |
33046 | 61 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
62 fseek(file, 0, SEEK_END); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
63 len = ftell(file); |
33046 | 64 |
65 if (len > 50 * 1024 * 1024) { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
66 fclose(file); |
33137 | 67 return 3; |
33046 | 68 } |
69 | |
70 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); | |
71 | |
33135 | 72 if (!data) { |
73 fclose(file); | |
33137 | 74 return 4; |
33135 | 75 } |
76 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
77 fseek(file, 0, SEEK_SET); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
78 fread(data, len, 1, file); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
79 fclose(file); |
33046 | 80 |
81 avctx = avcodec_alloc_context(); | |
82 frame = avcodec_alloc_frame(); | |
33139 | 83 |
84 if (!(avctx && frame)) { | |
85 av_free(frame); | |
86 av_free(avctx); | |
87 av_free(data); | |
88 return 5; | |
89 } | |
90 | |
33046 | 91 avcodec_register_all(); |
92 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG)); | |
33139 | 93 |
33046 | 94 av_init_packet(&pkt); |
95 pkt.data = data; | |
96 pkt.size = len; | |
97 // HACK: make PNGs decode normally instead of as CorePNG delta frames | |
98 pkt.flags = AV_PKT_FLAG_KEY; | |
33139 | 99 |
33046 | 100 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); |
33134 | 101 |
33983 | 102 memset(img, 0, sizeof(*img)); |
33046 | 103 |
104 switch (avctx->pix_fmt) { | |
105 case PIX_FMT_GRAY8: | |
33983 | 106 img->Bpp = 8; |
33046 | 107 break; |
108 | |
109 case PIX_FMT_GRAY16BE: | |
33983 | 110 img->Bpp = 16; |
33046 | 111 break; |
112 | |
113 case PIX_FMT_RGB24: | |
33983 | 114 img->Bpp = 24; |
33046 | 115 break; |
116 | |
117 case PIX_FMT_BGRA: | |
118 case PIX_FMT_ARGB: | |
33983 | 119 img->Bpp = 32; |
33046 | 120 break; |
121 | |
122 default: | |
33983 | 123 img->Bpp = 0; |
33046 | 124 break; |
125 } | |
126 | |
33983 | 127 if (decode_ok && img->Bpp) { |
128 img->Width = avctx->width; | |
129 img->Height = avctx->height; | |
130 bpl = img->Width * (img->Bpp / 8); | |
131 img->ImageSize = bpl * img->Height; | |
33136 | 132 |
33985 | 133 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname); |
134 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %lux%lu, color depth: %u\n", img->Width, img->Height, img->Bpp); | |
135 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", img->ImageSize); | |
33136 | 136 |
33983 | 137 img->Image = malloc(img->ImageSize); |
33135 | 138 |
33983 | 139 if (img->Image) |
140 memcpy_pic(img->Image, frame->data[0], bpl, img->Height, bpl, frame->linesize[0]); | |
33137 | 141 else |
142 decode_ok = 0; | |
33046 | 143 } |
144 | |
145 avcodec_close(avctx); | |
33138 | 146 av_free(frame); |
147 av_free(avctx); | |
148 av_free(data); | |
33046 | 149 |
33983 | 150 return !(decode_ok && img->Bpp); |
33046 | 151 } |
152 | |
33984 | 153 /** |
154 * @brief Convert a 24-bit color depth image into an 32-bit one. | |
155 * | |
156 * @param img image to be converted | |
157 * | |
158 * @return 1 (ok) or 0 (error) | |
159 * | |
160 * @note This is an in-place conversion, new memory will be allocated for @a img. | |
161 */ | |
33983 | 162 static int Convert24to32(guiImage *img) |
33046 | 163 { |
33132 | 164 char *orgImage; |
165 unsigned long i, c; | |
33046 | 166 |
33983 | 167 if (img->Bpp == 24) { |
168 orgImage = img->Image; | |
33132 | 169 |
33983 | 170 img->Bpp = 32; |
171 img->ImageSize = img->Width * img->Height * 4; | |
172 img->Image = calloc(1, img->ImageSize); | |
33046 | 173 |
33983 | 174 if (!img->Image) { |
33132 | 175 free(orgImage); |
33985 | 176 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", img->ImageSize); |
33121 | 177 return 0; |
33046 | 178 } |
179 | |
33985 | 180 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %lu\n", img->ImageSize); |
33112 | 181 |
33983 | 182 for (c = 0, i = 0; c < img->ImageSize; c += 4, i += 3) |
183 *(uint32_t *)&img->Image[c] = ALPHA_OPAQUE | AV_RB24(&orgImage[i]); | |
33046 | 184 |
33132 | 185 free(orgImage); |
33046 | 186 } |
187 | |
33121 | 188 return 1; |
33046 | 189 } |
190 | |
33984 | 191 /** |
192 * @brief Check whether a (PNG) file exists. | |
193 * | |
194 * @param fname filename (with path, but may lack extension) | |
195 * | |
196 * @return path including extension (ok) or NULL (not accessible) | |
197 */ | |
33710 | 198 static const char *fExist(const char *fname) |
33046 | 199 { |
33141 | 200 static const char ext[][4] = { "png", "PNG" }; |
33710 | 201 static char buf[512]; |
33130 | 202 unsigned int i; |
33046 | 203 |
33130 | 204 if (access(fname, R_OK) == 0) |
33046 | 205 return fname; |
206 | |
33130 | 207 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { |
208 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
33046 | 209 |
33130 | 210 if (access(buf, R_OK) == 0) |
211 return buf; | |
33046 | 212 } |
213 | |
214 return NULL; | |
215 } | |
216 | |
33984 | 217 /** |
218 * @brief Read a PNG file. | |
219 * | |
220 * @param fname filename (with path, but may lack extension) | |
221 * @param img pointer suitable to store the image data | |
222 * | |
223 * @return 0 (ok), -1 (color depth too low), -2 (not accessible), | |
224 * -5 (#pngRead() error) or -8 (#Convert24to32() error) | |
225 */ | |
33983 | 226 int bpRead(const char *fname, guiImage *img) |
33046 | 227 { |
33137 | 228 int r; |
229 | |
33046 | 230 fname = fExist(fname); |
231 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
232 if (!fname) |
33046 | 233 return -2; |
234 | |
33983 | 235 r = pngRead(fname, img); |
33137 | 236 |
237 if (r != 0) { | |
33985 | 238 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error #%d: %s\n", r, fname); |
33046 | 239 return -5; |
240 } | |
241 | |
33983 | 242 if (img->Bpp < 24) { |
33985 | 243 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", img->Bpp); |
33046 | 244 return -1; |
245 } | |
246 | |
33983 | 247 if (!Convert24to32(img)) |
33046 | 248 return -8; |
249 | |
250 return 0; | |
251 } | |
252 | |
33984 | 253 /** |
254 * @brief Free all memory allocated to an image and set all its pointers to NULL. | |
255 * | |
256 * @param img image to be freed | |
257 */ | |
33983 | 258 void bpFree(guiImage *img) |
33046 | 259 { |
33983 | 260 free(img->Image); |
261 memset(img, 0, sizeof(*img)); | |
33046 | 262 } |
263 | |
33984 | 264 /** |
265 * @brief Render a bitmap mask for an image. | |
266 * | |
267 * @param in image to render a bitmap mask from | |
268 * @param out bitmap mask | |
269 * | |
270 * @return 1 (ok) or 0 (error) | |
271 */ | |
33710 | 272 int bpRenderMask(const guiImage *in, guiImage *out) |
33046 | 273 { |
33118 | 274 uint32_t *buf; |
275 unsigned long i; | |
276 int b = 0, c = 0; | |
277 unsigned char tmp = 0; | |
278 int shaped = 0; | |
279 | |
33046 | 280 out->Width = in->Width; |
281 out->Height = in->Height; | |
33555 | 282 out->Bpp = 1; |
33046 | 283 out->ImageSize = (out->Width * out->Height + 7) / 8; |
33118 | 284 out->Image = calloc(1, out->ImageSize); |
33046 | 285 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
286 if (!out->Image) { |
33985 | 287 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
33114 | 288 return 0; |
33125 | 289 } |
290 | |
33126 | 291 buf = (uint32_t *)in->Image; |
33046 | 292 |
33126 | 293 for (i = 0; i < out->Width * out->Height; i++) { |
294 tmp >>= 1; | |
33119 | 295 |
33534 | 296 if (!IS_TRANSPARENT(buf[i])) |
33126 | 297 tmp |= 0x80; |
33128 | 298 else { |
299 buf[i] = 0; | |
33126 | 300 shaped = 1; |
33128 | 301 } |
33046 | 302 |
33126 | 303 if (++b == 8) { |
304 out->Image[c++] = tmp; | |
305 tmp = b = 0; | |
306 } | |
307 } | |
33112 | 308 |
33126 | 309 if (b) |
310 out->Image[c] = tmp; | |
311 | |
312 if (!shaped) | |
313 bpFree(out); | |
33114 | 314 |
33985 | 315 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
33133 | 316 |
33114 | 317 return 1; |
33046 | 318 } |