Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 33127:6328f0baeaa9
Don't change original image during conversion.
author | ib |
---|---|
date | Mon, 04 Apr 2011 18:35:25 +0000 |
parents | a5dc70e2cda3 |
children | 64d19aaf624a |
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 { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
33 FILE *file; |
33046 | 34 int decode_ok; |
35 void *data; | |
36 int len; | |
37 AVCodecContext *avctx; | |
38 AVFrame *frame; | |
39 AVPacket pkt; | |
40 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
41 file = fopen(fname, "rb"); |
33046 | 42 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
43 if (!file) { |
33112 | 44 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] open error: %s\n", fname); |
33046 | 45 return 1; |
46 } | |
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); |
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 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
59 fseek(file, 0, SEEK_SET); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
60 fread(data, len, 1, file); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
61 fclose(file); |
33046 | 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 | |
33121 | 121 static int Convert24to32(txSample *bf) |
33046 | 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); |
33121 | 135 return 0; |
33046 | 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 | |
33121 | 146 return 1; |
33046 | 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]; | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
164 FILE *file; |
33046 | 165 unsigned char ext[][6] = { ".png\0", ".PNG\0" }; |
166 int i; | |
167 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
168 file = fopen(fname, "rb"); |
33046 | 169 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
170 if (file) { |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
171 fclose(file); |
33046 | 172 return fname; |
173 } | |
174 | |
175 for (i = 0; i < 2; i++) { | |
176 snprintf(tmp, sizeof(tmp), "%s%s", fname, ext[i]); | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
177 file = fopen(tmp, "rb"); |
33046 | 178 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
179 if (file) { |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
180 fclose(file); |
33046 | 181 return tmp; |
182 } | |
183 } | |
184 | |
185 return NULL; | |
186 } | |
187 | |
188 int bpRead(char *fname, txSample *bf) | |
189 { | |
190 fname = fExist(fname); | |
191 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
192 if (!fname) |
33046 | 193 return -2; |
194 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
195 if (pngRead(fname, bf) != 0) { |
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 | |
33121 | 205 if (!Convert24to32(bf)) |
33046 | 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 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
233 if (!out->Image) { |
33114 | 234 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
235 return 0; | |
33125 | 236 } |
237 | |
33126 | 238 buf = (uint32_t *)in->Image; |
33046 | 239 |
33126 | 240 for (i = 0; i < out->Width * out->Height; i++) { |
241 tmp >>= 1; | |
33119 | 242 |
33126 | 243 if (buf[i] != transparent) |
244 tmp |= 0x80; | |
33127 | 245 else |
33126 | 246 shaped = 1; |
33046 | 247 |
33126 | 248 if (++b == 8) { |
249 out->Image[c++] = tmp; | |
250 tmp = b = 0; | |
251 } | |
252 } | |
33112 | 253 |
33126 | 254 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
33046 | 255 |
33126 | 256 if (b) |
257 out->Image[c] = tmp; | |
258 | |
259 if (!shaped) | |
260 bpFree(out); | |
33114 | 261 |
262 return 1; | |
33046 | 263 } |