Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 33130:4f60cd16ac04
Clean up and simplify fExist().
Replace fopen() to check file existence by access(), define variables
differently and use FF_ARRAY_ELEMS macro in loop test expression.
author | ib |
---|---|
date | Tue, 05 Apr 2011 09:12:35 +0000 |
parents | 5f3171d4d7a4 |
children | aa753565a901 |
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; |
33046 | 35 int decode_ok; |
36 void *data; | |
37 int len; | |
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 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
44 if (!file) { |
33112 | 45 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] open error: %s\n", fname); |
33046 | 46 return 1; |
47 } | |
48 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
49 fseek(file, 0, SEEK_END); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
50 len = ftell(file); |
33046 | 51 |
52 if (len > 50 * 1024 * 1024) { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
53 fclose(file); |
33112 | 54 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file too big: %s\n", fname); |
33046 | 55 return 2; |
56 } | |
57 | |
58 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); | |
59 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
60 fseek(file, 0, SEEK_SET); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
61 fread(data, len, 1, file); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
62 fclose(file); |
33046 | 63 |
64 avctx = avcodec_alloc_context(); | |
65 frame = avcodec_alloc_frame(); | |
66 avcodec_register_all(); | |
67 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG)); | |
68 av_init_packet(&pkt); | |
69 pkt.data = data; | |
70 pkt.size = len; | |
71 // HACK: make PNGs decode normally instead of as CorePNG delta frames | |
72 pkt.flags = AV_PKT_FLAG_KEY; | |
73 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); | |
74 memset(bf, 0, sizeof(*bf)); | |
75 | |
76 switch (avctx->pix_fmt) { | |
77 case PIX_FMT_GRAY8: | |
78 bf->BPP = 8; | |
79 break; | |
80 | |
81 case PIX_FMT_GRAY16BE: | |
82 bf->BPP = 16; | |
83 break; | |
84 | |
85 case PIX_FMT_RGB24: | |
86 bf->BPP = 24; | |
87 break; | |
88 | |
89 case PIX_FMT_BGRA: | |
90 case PIX_FMT_ARGB: | |
91 bf->BPP = 32; | |
92 break; | |
93 | |
94 default: | |
95 bf->BPP = 0; | |
96 break; | |
97 } | |
98 | |
99 if (decode_ok && bf->BPP) { | |
100 int bpl; | |
101 | |
102 bf->Width = avctx->width; | |
103 bf->Height = avctx->height; | |
104 bpl = bf->Width * (bf->BPP / 8); | |
105 bf->ImageSize = bpl * bf->Height; | |
106 bf->Image = malloc(bf->ImageSize); | |
107 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]); | |
108 } | |
109 | |
110 avcodec_close(avctx); | |
111 av_freep(&frame); | |
112 av_freep(&avctx); | |
113 av_freep(&data); | |
114 | |
33112 | 115 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname); |
116 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %lux%lu, color depth: %u\n", bf->Width, bf->Height, bf->BPP); | |
117 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", bf->ImageSize); | |
33046 | 118 |
119 return !(decode_ok && bf->BPP); | |
120 } | |
121 | |
33121 | 122 static int Convert24to32(txSample *bf) |
33046 | 123 { |
124 unsigned char *tmpImage; | |
125 unsigned int i, c; | |
126 | |
127 if (bf->BPP == 24) { | |
128 tmpImage = bf->Image; | |
129 bf->BPP = 32; | |
130 bf->ImageSize = bf->Width * bf->Height * 4; | |
131 bf->Image = calloc(1, bf->ImageSize); | |
132 | |
133 if (!bf->Image) { | |
134 free(tmpImage); | |
33112 | 135 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", bf->ImageSize); |
33121 | 136 return 0; |
33046 | 137 } |
138 | |
33112 | 139 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %lu\n", bf->ImageSize); |
140 | |
33046 | 141 for (c = 0, i = 0; c < bf->ImageSize; c += 4, i += 3) |
142 *(uint32_t *)&bf->Image[c] = AV_RB24(&tmpImage[i]); | |
143 | |
144 free(tmpImage); | |
145 } | |
146 | |
33121 | 147 return 1; |
33046 | 148 } |
149 | |
150 static void Normalize(txSample *bf) | |
151 { | |
152 int i; | |
153 | |
154 for (i = 0; i < (int)bf->ImageSize; i += 4) | |
155 #if !HAVE_BIGENDIAN | |
156 bf->Image[i + 3] = 0; | |
157 #else | |
158 bf->Image[i] = 0; | |
159 #endif | |
160 } | |
161 | |
162 static unsigned char *fExist(unsigned char *fname) | |
163 { | |
33130 | 164 unsigned char *ext[] = { "png", "PNG" }; |
165 static unsigned char buf[512]; | |
166 unsigned int i; | |
33046 | 167 |
33130 | 168 if (access(fname, R_OK) == 0) |
33046 | 169 return fname; |
170 | |
33130 | 171 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { |
172 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
33046 | 173 |
33130 | 174 if (access(buf, R_OK) == 0) |
175 return buf; | |
33046 | 176 } |
177 | |
178 return NULL; | |
179 } | |
180 | |
181 int bpRead(char *fname, txSample *bf) | |
182 { | |
183 fname = fExist(fname); | |
184 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
185 if (!fname) |
33046 | 186 return -2; |
187 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
188 if (pngRead(fname, bf) != 0) { |
33112 | 189 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error: %s\n", fname); |
33046 | 190 return -5; |
191 } | |
192 | |
193 if (bf->BPP < 24) { | |
33112 | 194 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", bf->BPP); |
33046 | 195 return -1; |
196 } | |
197 | |
33121 | 198 if (!Convert24to32(bf)) |
33046 | 199 return -8; |
200 | |
201 Normalize(bf); | |
33053 | 202 |
33046 | 203 return 0; |
204 } | |
205 | |
206 void bpFree(txSample *bf) | |
207 { | |
208 free(bf->Image); | |
209 memset(bf, 0, sizeof(*bf)); | |
210 } | |
211 | |
33129 | 212 int Convert32to1(txSample *in, txSample *out) |
33046 | 213 { |
33118 | 214 uint32_t *buf; |
215 unsigned long i; | |
216 int b = 0, c = 0; | |
217 unsigned char tmp = 0; | |
218 int shaped = 0; | |
219 | |
33046 | 220 out->Width = in->Width; |
221 out->Height = in->Height; | |
222 out->BPP = 1; | |
223 out->ImageSize = (out->Width * out->Height + 7) / 8; | |
33118 | 224 out->Image = calloc(1, out->ImageSize); |
33046 | 225 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
226 if (!out->Image) { |
33114 | 227 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
228 return 0; | |
33125 | 229 } |
230 | |
33126 | 231 buf = (uint32_t *)in->Image; |
33046 | 232 |
33126 | 233 for (i = 0; i < out->Width * out->Height; i++) { |
234 tmp >>= 1; | |
33119 | 235 |
33129 | 236 if (buf[i] != TRANSPARENT) |
33126 | 237 tmp |= 0x80; |
33128 | 238 else { |
239 buf[i] = 0; | |
33126 | 240 shaped = 1; |
33128 | 241 } |
33046 | 242 |
33126 | 243 if (++b == 8) { |
244 out->Image[c++] = tmp; | |
245 tmp = b = 0; | |
246 } | |
247 } | |
33112 | 248 |
33126 | 249 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
33046 | 250 |
33126 | 251 if (b) |
252 out->Image[c] = tmp; | |
253 | |
254 if (!shaped) | |
255 bpFree(out); | |
33114 | 256 |
257 return 1; | |
33046 | 258 } |