Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 33983:2218c589f9ab
Cosmetic: Rename parameters bf img in bitmap.c.
author | ib |
---|---|
date | Tue, 06 Sep 2011 14:59:04 +0000 |
parents | fd498969e72d |
children | 60449f4234f7 |
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" | |
33690 | 28 #include "libavutil/common.h" |
33046 | 29 #include "libavutil/intreadwrite.h" |
30 #include "libvo/fastmemcpy.h" | |
31 #include "mp_msg.h" | |
32 | |
33983 | 33 static int pngRead(const char *fname, guiImage *img) |
33046 | 34 { |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
35 FILE *file; |
33134 | 36 long len; |
33046 | 37 void *data; |
33135 | 38 int decode_ok, bpl; |
33046 | 39 AVCodecContext *avctx; |
40 AVFrame *frame; | |
41 AVPacket pkt; | |
42 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
43 file = fopen(fname, "rb"); |
33046 | 44 |
33137 | 45 if (!file) |
46 return 2; | |
33046 | 47 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
48 fseek(file, 0, SEEK_END); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
49 len = ftell(file); |
33046 | 50 |
51 if (len > 50 * 1024 * 1024) { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
52 fclose(file); |
33137 | 53 return 3; |
33046 | 54 } |
55 | |
56 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); | |
57 | |
33135 | 58 if (!data) { |
59 fclose(file); | |
33137 | 60 return 4; |
33135 | 61 } |
62 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
63 fseek(file, 0, SEEK_SET); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
64 fread(data, len, 1, file); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
65 fclose(file); |
33046 | 66 |
67 avctx = avcodec_alloc_context(); | |
68 frame = avcodec_alloc_frame(); | |
33139 | 69 |
70 if (!(avctx && frame)) { | |
71 av_free(frame); | |
72 av_free(avctx); | |
73 av_free(data); | |
74 return 5; | |
75 } | |
76 | |
33046 | 77 avcodec_register_all(); |
78 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG)); | |
33139 | 79 |
33046 | 80 av_init_packet(&pkt); |
81 pkt.data = data; | |
82 pkt.size = len; | |
83 // HACK: make PNGs decode normally instead of as CorePNG delta frames | |
84 pkt.flags = AV_PKT_FLAG_KEY; | |
33139 | 85 |
33046 | 86 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); |
33134 | 87 |
33983 | 88 memset(img, 0, sizeof(*img)); |
33046 | 89 |
90 switch (avctx->pix_fmt) { | |
91 case PIX_FMT_GRAY8: | |
33983 | 92 img->Bpp = 8; |
33046 | 93 break; |
94 | |
95 case PIX_FMT_GRAY16BE: | |
33983 | 96 img->Bpp = 16; |
33046 | 97 break; |
98 | |
99 case PIX_FMT_RGB24: | |
33983 | 100 img->Bpp = 24; |
33046 | 101 break; |
102 | |
103 case PIX_FMT_BGRA: | |
104 case PIX_FMT_ARGB: | |
33983 | 105 img->Bpp = 32; |
33046 | 106 break; |
107 | |
108 default: | |
33983 | 109 img->Bpp = 0; |
33046 | 110 break; |
111 } | |
112 | |
33983 | 113 if (decode_ok && img->Bpp) { |
114 img->Width = avctx->width; | |
115 img->Height = avctx->height; | |
116 bpl = img->Width * (img->Bpp / 8); | |
117 img->ImageSize = bpl * img->Height; | |
33136 | 118 |
119 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname); | |
33983 | 120 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %lux%lu, color depth: %u\n", img->Width, img->Height, img->Bpp); |
121 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", img->ImageSize); | |
33136 | 122 |
33983 | 123 img->Image = malloc(img->ImageSize); |
33135 | 124 |
33983 | 125 if (img->Image) |
126 memcpy_pic(img->Image, frame->data[0], bpl, img->Height, bpl, frame->linesize[0]); | |
33137 | 127 else |
128 decode_ok = 0; | |
33046 | 129 } |
130 | |
131 avcodec_close(avctx); | |
33138 | 132 av_free(frame); |
133 av_free(avctx); | |
134 av_free(data); | |
33046 | 135 |
33983 | 136 return !(decode_ok && img->Bpp); |
33046 | 137 } |
138 | |
33983 | 139 static int Convert24to32(guiImage *img) |
33046 | 140 { |
33132 | 141 char *orgImage; |
142 unsigned long i, c; | |
33046 | 143 |
33983 | 144 if (img->Bpp == 24) { |
145 orgImage = img->Image; | |
33132 | 146 |
33983 | 147 img->Bpp = 32; |
148 img->ImageSize = img->Width * img->Height * 4; | |
149 img->Image = calloc(1, img->ImageSize); | |
33046 | 150 |
33983 | 151 if (!img->Image) { |
33132 | 152 free(orgImage); |
33983 | 153 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", img->ImageSize); |
33121 | 154 return 0; |
33046 | 155 } |
156 | |
33983 | 157 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %lu\n", img->ImageSize); |
33112 | 158 |
33983 | 159 for (c = 0, i = 0; c < img->ImageSize; c += 4, i += 3) |
160 *(uint32_t *)&img->Image[c] = ALPHA_OPAQUE | AV_RB24(&orgImage[i]); | |
33046 | 161 |
33132 | 162 free(orgImage); |
33046 | 163 } |
164 | |
33121 | 165 return 1; |
33046 | 166 } |
167 | |
33710 | 168 static const char *fExist(const char *fname) |
33046 | 169 { |
33141 | 170 static const char ext[][4] = { "png", "PNG" }; |
33710 | 171 static char buf[512]; |
33130 | 172 unsigned int i; |
33046 | 173 |
33130 | 174 if (access(fname, R_OK) == 0) |
33046 | 175 return fname; |
176 | |
33130 | 177 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { |
178 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
33046 | 179 |
33130 | 180 if (access(buf, R_OK) == 0) |
181 return buf; | |
33046 | 182 } |
183 | |
184 return NULL; | |
185 } | |
186 | |
33983 | 187 int bpRead(const char *fname, guiImage *img) |
33046 | 188 { |
33137 | 189 int r; |
190 | |
33046 | 191 fname = fExist(fname); |
192 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
193 if (!fname) |
33046 | 194 return -2; |
195 | |
33983 | 196 r = pngRead(fname, img); |
33137 | 197 |
198 if (r != 0) { | |
199 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error #%d: %s\n", r, fname); | |
33046 | 200 return -5; |
201 } | |
202 | |
33983 | 203 if (img->Bpp < 24) { |
204 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", img->Bpp); | |
33046 | 205 return -1; |
206 } | |
207 | |
33983 | 208 if (!Convert24to32(img)) |
33046 | 209 return -8; |
210 | |
211 return 0; | |
212 } | |
213 | |
33983 | 214 void bpFree(guiImage *img) |
33046 | 215 { |
33983 | 216 free(img->Image); |
217 memset(img, 0, sizeof(*img)); | |
33046 | 218 } |
219 | |
33710 | 220 int bpRenderMask(const guiImage *in, guiImage *out) |
33046 | 221 { |
33118 | 222 uint32_t *buf; |
223 unsigned long i; | |
224 int b = 0, c = 0; | |
225 unsigned char tmp = 0; | |
226 int shaped = 0; | |
227 | |
33046 | 228 out->Width = in->Width; |
229 out->Height = in->Height; | |
33555 | 230 out->Bpp = 1; |
33046 | 231 out->ImageSize = (out->Width * out->Height + 7) / 8; |
33118 | 232 out->Image = calloc(1, out->ImageSize); |
33046 | 233 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
234 if (!out->Image) { |
33114 | 235 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
236 return 0; | |
33125 | 237 } |
238 | |
33126 | 239 buf = (uint32_t *)in->Image; |
33046 | 240 |
33126 | 241 for (i = 0; i < out->Width * out->Height; i++) { |
242 tmp >>= 1; | |
33119 | 243 |
33534 | 244 if (!IS_TRANSPARENT(buf[i])) |
33126 | 245 tmp |= 0x80; |
33128 | 246 else { |
247 buf[i] = 0; | |
33126 | 248 shaped = 1; |
33128 | 249 } |
33046 | 250 |
33126 | 251 if (++b == 8) { |
252 out->Image[c++] = tmp; | |
253 tmp = b = 0; | |
254 } | |
255 } | |
33112 | 256 |
33126 | 257 if (b) |
258 out->Image[c] = tmp; | |
259 | |
260 if (!shaped) | |
261 bpFree(out); | |
33114 | 262 |
33133 | 263 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
264 | |
33114 | 265 return 1; |
33046 | 266 } |