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
|
|
219 void Convert32to1(txSample *in, txSample *out, int adaptivlimit)
|
|
220 {
|
|
221 out->Width = in->Width;
|
|
222 out->Height = in->Height;
|
|
223 out->BPP = 1;
|
|
224 out->ImageSize = (out->Width * out->Height + 7) / 8;
|
|
225
|
|
226 out->Image = calloc(1, out->ImageSize);
|
|
227
|
|
228 if (out->Image == NULL)
|
|
229 mp_msg(MSGT_GPLAYER, MSGL_WARN, MSGTR_NotEnoughMemoryC32To1);
|
|
230 {
|
|
231 int i, b, c = 0;
|
|
232 unsigned int *buf = NULL;
|
|
233 unsigned char tmp = 0;
|
|
234 int nothaveshape = 1;
|
|
235
|
|
236 buf = (unsigned int *)in->Image;
|
|
237
|
|
238 for (b = 0, i = 0; i < (int)(out->Width * out->Height); i++) {
|
|
239 if ((int)buf[i] != adaptivlimit)
|
|
240 tmp = (tmp >> 1) | 128;
|
|
241 else {
|
|
242 tmp = tmp >> 1;
|
|
243 buf[i] = nothaveshape = 0;
|
|
244 }
|
|
245
|
|
246 if (b++ == 7) {
|
|
247 out->Image[c++] = tmp;
|
|
248 tmp = b = 0;
|
|
249 }
|
|
250 }
|
|
251
|
33112
|
252 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize);
|
|
253
|
33046
|
254 if (b)
|
|
255 out->Image[c] = tmp;
|
|
256
|
|
257 if (nothaveshape) {
|
|
258 free(out->Image);
|
|
259 out->Image = NULL;
|
|
260 }
|
|
261 }
|
|
262 }
|