Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 33137:f884fb919ac5
Remove various pngRead() debug messages.
Instead, issue the return code in the debug message after calling pngRead()
and increase return code values to allow exact error assignments.
author | ib |
---|---|
date | Tue, 05 Apr 2011 13:44:02 +0000 |
parents | 07be88814256 |
children | 3335d117f6e6 |
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(); | |
68 avcodec_register_all(); | |
69 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG)); | |
70 av_init_packet(&pkt); | |
71 pkt.data = data; | |
72 pkt.size = len; | |
73 // HACK: make PNGs decode normally instead of as CorePNG delta frames | |
74 pkt.flags = AV_PKT_FLAG_KEY; | |
75 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); | |
33134 | 76 |
33046 | 77 memset(bf, 0, sizeof(*bf)); |
78 | |
79 switch (avctx->pix_fmt) { | |
80 case PIX_FMT_GRAY8: | |
81 bf->BPP = 8; | |
82 break; | |
83 | |
84 case PIX_FMT_GRAY16BE: | |
85 bf->BPP = 16; | |
86 break; | |
87 | |
88 case PIX_FMT_RGB24: | |
89 bf->BPP = 24; | |
90 break; | |
91 | |
92 case PIX_FMT_BGRA: | |
93 case PIX_FMT_ARGB: | |
94 bf->BPP = 32; | |
95 break; | |
96 | |
97 default: | |
98 bf->BPP = 0; | |
99 break; | |
100 } | |
101 | |
102 if (decode_ok && bf->BPP) { | |
103 bf->Width = avctx->width; | |
104 bf->Height = avctx->height; | |
105 bpl = bf->Width * (bf->BPP / 8); | |
106 bf->ImageSize = bpl * bf->Height; | |
33136 | 107 |
108 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname); | |
109 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %lux%lu, color depth: %u\n", bf->Width, bf->Height, bf->BPP); | |
110 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", bf->ImageSize); | |
111 | |
112 bf->Image = malloc(bf->ImageSize); | |
33135 | 113 |
114 if (bf->Image) | |
115 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]); | |
33137 | 116 else |
117 decode_ok = 0; | |
33046 | 118 } |
119 | |
120 avcodec_close(avctx); | |
121 av_freep(&frame); | |
122 av_freep(&avctx); | |
123 av_freep(&data); | |
124 | |
125 return !(decode_ok && bf->BPP); | |
126 } | |
127 | |
33121 | 128 static int Convert24to32(txSample *bf) |
33046 | 129 { |
33132 | 130 char *orgImage; |
131 unsigned long i, c; | |
33046 | 132 |
133 if (bf->BPP == 24) { | |
33132 | 134 orgImage = bf->Image; |
135 | |
33046 | 136 bf->BPP = 32; |
137 bf->ImageSize = bf->Width * bf->Height * 4; | |
138 bf->Image = calloc(1, bf->ImageSize); | |
139 | |
140 if (!bf->Image) { | |
33132 | 141 free(orgImage); |
33112 | 142 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", bf->ImageSize); |
33121 | 143 return 0; |
33046 | 144 } |
145 | |
33112 | 146 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %lu\n", bf->ImageSize); |
147 | |
33046 | 148 for (c = 0, i = 0; c < bf->ImageSize; c += 4, i += 3) |
33132 | 149 *(uint32_t *)&bf->Image[c] = AV_RB24(&orgImage[i]); |
33046 | 150 |
33132 | 151 free(orgImage); |
33046 | 152 } |
153 | |
33121 | 154 return 1; |
33046 | 155 } |
156 | |
157 static void Normalize(txSample *bf) | |
158 { | |
33131 | 159 unsigned long i; |
33046 | 160 |
33131 | 161 for (i = 0; i < bf->ImageSize; i += 4) |
162 #if HAVE_BIGENDIAN | |
163 bf->Image[i] = 0; | |
164 #else | |
33046 | 165 bf->Image[i + 3] = 0; |
166 #endif | |
167 } | |
168 | |
169 static unsigned char *fExist(unsigned char *fname) | |
170 { | |
33130 | 171 unsigned char *ext[] = { "png", "PNG" }; |
172 static unsigned char buf[512]; | |
173 unsigned int i; | |
33046 | 174 |
33130 | 175 if (access(fname, R_OK) == 0) |
33046 | 176 return fname; |
177 | |
33130 | 178 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { |
179 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
33046 | 180 |
33130 | 181 if (access(buf, R_OK) == 0) |
182 return buf; | |
33046 | 183 } |
184 | |
185 return NULL; | |
186 } | |
187 | |
188 int bpRead(char *fname, txSample *bf) | |
189 { | |
33137 | 190 int r; |
191 | |
33046 | 192 fname = fExist(fname); |
193 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
194 if (!fname) |
33046 | 195 return -2; |
196 | |
33137 | 197 r = pngRead(fname, bf); |
198 | |
199 if (r != 0) { | |
200 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error #%d: %s\n", r, fname); | |
33046 | 201 return -5; |
202 } | |
203 | |
204 if (bf->BPP < 24) { | |
33112 | 205 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", bf->BPP); |
33046 | 206 return -1; |
207 } | |
208 | |
33121 | 209 if (!Convert24to32(bf)) |
33046 | 210 return -8; |
211 | |
212 Normalize(bf); | |
33053 | 213 |
33046 | 214 return 0; |
215 } | |
216 | |
217 void bpFree(txSample *bf) | |
218 { | |
219 free(bf->Image); | |
220 memset(bf, 0, sizeof(*bf)); | |
221 } | |
222 | |
33129 | 223 int Convert32to1(txSample *in, txSample *out) |
33046 | 224 { |
33118 | 225 uint32_t *buf; |
226 unsigned long i; | |
227 int b = 0, c = 0; | |
228 unsigned char tmp = 0; | |
229 int shaped = 0; | |
230 | |
33046 | 231 out->Width = in->Width; |
232 out->Height = in->Height; | |
233 out->BPP = 1; | |
234 out->ImageSize = (out->Width * out->Height + 7) / 8; | |
33118 | 235 out->Image = calloc(1, out->ImageSize); |
33046 | 236 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
237 if (!out->Image) { |
33114 | 238 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
239 return 0; | |
33125 | 240 } |
241 | |
33126 | 242 buf = (uint32_t *)in->Image; |
33046 | 243 |
33126 | 244 for (i = 0; i < out->Width * out->Height; i++) { |
245 tmp >>= 1; | |
33119 | 246 |
33129 | 247 if (buf[i] != TRANSPARENT) |
33126 | 248 tmp |= 0x80; |
33128 | 249 else { |
250 buf[i] = 0; | |
33126 | 251 shaped = 1; |
33128 | 252 } |
33046 | 253 |
33126 | 254 if (++b == 8) { |
255 out->Image[c++] = tmp; | |
256 tmp = b = 0; | |
257 } | |
258 } | |
33112 | 259 |
33126 | 260 if (b) |
261 out->Image[c] = tmp; | |
262 | |
263 if (!shaped) | |
264 bpFree(out); | |
33114 | 265 |
33133 | 266 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
267 | |
33114 | 268 return 1; |
33046 | 269 } |