comparison gui/util/bitmap.c @ 33046:9684ad0e1291

Move files with auxiliary functions to own directory.
author ib
date Mon, 28 Mar 2011 12:37:48 +0000
parents
children f64d41dac10b
comparison
equal deleted inserted replaced
33045:776a676d5fde 33046:9684ad0e1291
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) {
44 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[png] file read error ( %s )\n", fname);
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);
53 return 2;
54 }
55
56 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE);
57
58 fseek(fp, 0, SEEK_SET);
59 fread(data, len, 1, fp);
60 fclose(fp);
61
62 avctx = avcodec_alloc_context();
63 frame = avcodec_alloc_frame();
64 avcodec_register_all();
65 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG));
66 av_init_packet(&pkt);
67 pkt.data = data;
68 pkt.size = len;
69 // HACK: make PNGs decode normally instead of as CorePNG delta frames
70 pkt.flags = AV_PKT_FLAG_KEY;
71 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt);
72 memset(bf, 0, sizeof(*bf));
73
74 switch (avctx->pix_fmt) {
75 case PIX_FMT_GRAY8:
76 bf->BPP = 8;
77 break;
78
79 case PIX_FMT_GRAY16BE:
80 bf->BPP = 16;
81 break;
82
83 case PIX_FMT_RGB24:
84 bf->BPP = 24;
85 break;
86
87 case PIX_FMT_BGRA:
88 case PIX_FMT_ARGB:
89 bf->BPP = 32;
90 break;
91
92 default:
93 bf->BPP = 0;
94 break;
95 }
96
97 if (decode_ok && bf->BPP) {
98 int bpl;
99
100 bf->Width = avctx->width;
101 bf->Height = avctx->height;
102 bpl = bf->Width * (bf->BPP / 8);
103 bf->ImageSize = bpl * bf->Height;
104 bf->Image = malloc(bf->ImageSize);
105 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]);
106 }
107
108 avcodec_close(avctx);
109 av_freep(&frame);
110 av_freep(&avctx);
111 av_freep(&data);
112
113 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[png] filename: %s.\n", fname);
114 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[png] size: %lux%lu bits: %u\n", bf->Width, bf->Height, bf->BPP);
115 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[png] imagesize: %lu\n", bf->ImageSize);
116
117 return !(decode_ok && bf->BPP);
118 }
119
120 static int conv24to32(txSample *bf)
121 {
122 unsigned char *tmpImage;
123 unsigned int i, c;
124
125 if (bf->BPP == 24) {
126 tmpImage = bf->Image;
127 bf->BPP = 32;
128 bf->ImageSize = bf->Width * bf->Height * 4;
129 bf->Image = calloc(1, bf->ImageSize);
130
131 if (!bf->Image) {
132 free(tmpImage);
133 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory for image\n");
134 return 1;
135 }
136
137 for (c = 0, i = 0; c < bf->ImageSize; c += 4, i += 3)
138 *(uint32_t *)&bf->Image[c] = AV_RB24(&tmpImage[i]);
139
140 free(tmpImage);
141 }
142
143 return 0;
144 }
145
146 static void Normalize(txSample *bf)
147 {
148 int i;
149
150 for (i = 0; i < (int)bf->ImageSize; i += 4)
151 #if !HAVE_BIGENDIAN
152 bf->Image[i + 3] = 0;
153 #else
154 bf->Image[i] = 0;
155 #endif
156 }
157
158 static unsigned char *fExist(unsigned char *fname)
159 {
160 static unsigned char tmp[512];
161 FILE *fl;
162 unsigned char ext[][6] = { ".png\0", ".PNG\0" };
163 int i;
164
165 fl = fopen(fname, "rb");
166
167 if (fl != NULL) {
168 fclose(fl);
169 return fname;
170 }
171
172 for (i = 0; i < 2; i++) {
173 snprintf(tmp, sizeof(tmp), "%s%s", fname, ext[i]);
174 fl = fopen(tmp, "rb");
175
176 if (fl != NULL) {
177 fclose(fl);
178 return tmp;
179 }
180 }
181
182 return NULL;
183 }
184
185 int bpRead(char *fname, txSample *bf)
186 {
187 fname = fExist(fname);
188
189 if (fname == NULL)
190 return -2;
191
192 if (pngRead(fname, bf)) {
193 mp_dbg(MSGT_GPLAYER, MSGL_FATAL, "[bitmap] unknown file type ( %s )\n", fname);
194 return -5;
195 }
196
197 if (bf->BPP < 24) {
198 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n");
199 return -1;
200 }
201
202 if (conv24to32(bf))
203 return -8;
204
205 Normalize(bf);
206 return 0;
207 }
208
209 void bpFree(txSample *bf)
210 {
211 free(bf->Image);
212 memset(bf, 0, sizeof(*bf));
213 }
214
215 void Convert32to1(txSample *in, txSample *out, int adaptivlimit)
216 {
217 out->Width = in->Width;
218 out->Height = in->Height;
219 out->BPP = 1;
220 out->ImageSize = (out->Width * out->Height + 7) / 8;
221
222 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[c32to1] imagesize: %lu\n", out->ImageSize);
223
224 out->Image = calloc(1, out->ImageSize);
225
226 if (out->Image == NULL)
227 mp_msg(MSGT_GPLAYER, MSGL_WARN, MSGTR_NotEnoughMemoryC32To1);
228 {
229 int i, b, c = 0;
230 unsigned int *buf = NULL;
231 unsigned char tmp = 0;
232 int nothaveshape = 1;
233
234 buf = (unsigned int *)in->Image;
235
236 for (b = 0, i = 0; i < (int)(out->Width * out->Height); i++) {
237 if ((int)buf[i] != adaptivlimit)
238 tmp = (tmp >> 1) | 128;
239 else {
240 tmp = tmp >> 1;
241 buf[i] = nothaveshape = 0;
242 }
243
244 if (b++ == 7) {
245 out->Image[c++] = tmp;
246 tmp = b = 0;
247 }
248 }
249
250 if (b)
251 out->Image[c] = tmp;
252
253 if (nothaveshape) {
254 free(out->Image);
255 out->Image = NULL;
256 }
257 }
258 }