Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 33191:32238139a2ff
Hacks to allow compilation to go further.
author | reimar |
---|---|
date | Fri, 22 Apr 2011 06:19:14 +0000 |
parents | fd90a10d81a1 |
children | 22c3241467b3 |
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) |
33132 | 159 *(uint32_t *)&bf->Image[c] = AV_RB24(&orgImage[i]); |
33046 | 160 |
33132 | 161 free(orgImage); |
33046 | 162 } |
163 | |
33121 | 164 return 1; |
33046 | 165 } |
166 | |
167 static void Normalize(txSample *bf) | |
168 { | |
33131 | 169 unsigned long i; |
33046 | 170 |
33131 | 171 for (i = 0; i < bf->ImageSize; i += 4) |
172 #if HAVE_BIGENDIAN | |
173 bf->Image[i] = 0; | |
174 #else | |
33046 | 175 bf->Image[i + 3] = 0; |
176 #endif | |
177 } | |
178 | |
179 static unsigned char *fExist(unsigned char *fname) | |
180 { | |
33141 | 181 static const char ext[][4] = { "png", "PNG" }; |
33130 | 182 static unsigned char buf[512]; |
183 unsigned int i; | |
33046 | 184 |
33130 | 185 if (access(fname, R_OK) == 0) |
33046 | 186 return fname; |
187 | |
33130 | 188 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { |
189 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
33046 | 190 |
33130 | 191 if (access(buf, R_OK) == 0) |
192 return buf; | |
33046 | 193 } |
194 | |
195 return NULL; | |
196 } | |
197 | |
198 int bpRead(char *fname, txSample *bf) | |
199 { | |
33137 | 200 int r; |
201 | |
33046 | 202 fname = fExist(fname); |
203 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
204 if (!fname) |
33046 | 205 return -2; |
206 | |
33137 | 207 r = pngRead(fname, bf); |
208 | |
209 if (r != 0) { | |
210 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error #%d: %s\n", r, fname); | |
33046 | 211 return -5; |
212 } | |
213 | |
214 if (bf->BPP < 24) { | |
33112 | 215 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", bf->BPP); |
33046 | 216 return -1; |
217 } | |
218 | |
33121 | 219 if (!Convert24to32(bf)) |
33046 | 220 return -8; |
221 | |
222 Normalize(bf); | |
33053 | 223 |
33046 | 224 return 0; |
225 } | |
226 | |
227 void bpFree(txSample *bf) | |
228 { | |
229 free(bf->Image); | |
230 memset(bf, 0, sizeof(*bf)); | |
231 } | |
232 | |
33129 | 233 int Convert32to1(txSample *in, txSample *out) |
33046 | 234 { |
33118 | 235 uint32_t *buf; |
236 unsigned long i; | |
237 int b = 0, c = 0; | |
238 unsigned char tmp = 0; | |
239 int shaped = 0; | |
240 | |
33046 | 241 out->Width = in->Width; |
242 out->Height = in->Height; | |
243 out->BPP = 1; | |
244 out->ImageSize = (out->Width * out->Height + 7) / 8; | |
33118 | 245 out->Image = calloc(1, out->ImageSize); |
33046 | 246 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
247 if (!out->Image) { |
33114 | 248 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
249 return 0; | |
33125 | 250 } |
251 | |
33126 | 252 buf = (uint32_t *)in->Image; |
33046 | 253 |
33126 | 254 for (i = 0; i < out->Width * out->Height; i++) { |
255 tmp >>= 1; | |
33119 | 256 |
33129 | 257 if (buf[i] != TRANSPARENT) |
33126 | 258 tmp |= 0x80; |
33128 | 259 else { |
260 buf[i] = 0; | |
33126 | 261 shaped = 1; |
33128 | 262 } |
33046 | 263 |
33126 | 264 if (++b == 8) { |
265 out->Image[c++] = tmp; | |
266 tmp = b = 0; | |
267 } | |
268 } | |
33112 | 269 |
33126 | 270 if (b) |
271 out->Image[c] = tmp; | |
272 | |
273 if (!shaped) | |
274 bpFree(out); | |
33114 | 275 |
33133 | 276 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
277 | |
33114 | 278 return 1; |
33046 | 279 } |