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