annotate gui/util/bitmap.c @ 37185:917a3c4c2d39

lavf: fix AVSEEK_SIZE when STREAM_CTRL_GET_SIZE is not implemented. Would set the size to an uninitialized value before.
author reimar
date Wed, 17 Sep 2014 17:49:54 +0000
parents 15efbc138bd3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
1 /*
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
2 * This file is part of MPlayer.
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
3 *
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
7 * (at your option) any later version.
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
8 *
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
12 * GNU General Public License for more details.
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
13 *
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License along
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
17 */
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
18
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
19 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
20 * @file
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
21 * @brief Image loader and bitmap mask rendering
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
22 */
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
23
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
24 #include <stdio.h>
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
25 #include <stdlib.h>
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
26 #include <string.h>
33130
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
27 #include <unistd.h>
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
28
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
29 #include "bitmap.h"
35525
e27855a45128 Rebuild GUI directory structure.
ib
parents: 35493
diff changeset
30 #include "gui/app/gui.h"
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
31
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
32 #include "help_mp.h"
36032
c667e34fb941 Cosmetic: Rearrange #includes.
ib
parents: 35715
diff changeset
33 #include "mp_msg.h"
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
34 #include "libavcodec/avcodec.h"
33690
76342ee379eb Add missing #include "libavutil/common.h".
ib
parents: 33555
diff changeset
35 #include "libavutil/common.h"
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
36 #include "libavutil/intreadwrite.h"
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
37 #include "libvo/fastmemcpy.h"
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
38
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
39 /**
35662
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
40 * @brief Check whether a (PNG) file exists.
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
41 *
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
42 * @param fname filename (with path, but may lack extension)
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
43 *
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
44 * @return path including extension (ok) or NULL (not accessible)
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
45 */
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
46 static const char *fExist(const char *fname)
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
47 {
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
48 static const char ext[][4] = { "png", "PNG" };
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
49 static char buf[512];
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
50 unsigned int i;
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
51
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
52 if (access(fname, R_OK) == 0)
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
53 return fname;
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
54
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
55 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) {
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
56 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]);
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
57
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
58 if (access(buf, R_OK) == 0)
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
59 return buf;
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
60 }
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
61
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
62 return NULL;
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
63 }
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
64
782461b26312 Relocate some functions.
ib
parents: 35525
diff changeset
65 /**
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
66 * @brief Read and decode a PNG file into bitmap data.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
67 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
68 * @param fname filename (with path)
37065
b28b632efeef Cosmetic: Revise a few comments.
ib
parents: 36996
diff changeset
69 * @param img memory location to store the image data
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
70 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
71 * @return 0 (ok), 1 (decoding error), 2 (open error), 3 (file too big),
35359
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
72 * 4 (out of memory), 5 (read error), 6 (avcodec alloc error)
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
73 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
74 static int pngRead(const char *fname, guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
75 {
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
76 FILE *file;
35359
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
77 size_t len, l;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
78 void *data;
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
79 int decode_ok, bpl;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
80 AVCodecContext *avctx;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
81 AVFrame *frame;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
82 AVPacket pkt;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
83
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
84 file = fopen(fname, "rb");
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
85
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
86 if (!file)
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
87 return 2;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
88
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
89 fseek(file, 0, SEEK_END);
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
90 len = ftell(file);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
91
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
92 if (len > 50 * 1024 * 1024) {
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
93 fclose(file);
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
94 return 3;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
95 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
96
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
97 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE);
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
98
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
99 if (!data) {
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
100 fclose(file);
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
101 return 4;
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
102 }
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
103
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
104 fseek(file, 0, SEEK_SET);
35359
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
105 l = fread(data, len, 1, file);
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
106 fclose(file);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
107
35359
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
108 if (l != 1) {
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
109 av_free(data);
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
110 return 5;
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
111 }
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
112
34468
ef94d1d0e265 Replace deprecated avcodec functions by new ones.
ib
parents: 34132
diff changeset
113 avctx = avcodec_alloc_context3(NULL);
37097
15efbc138bd3 Replace deprecated avcodec_alloc_frame().
ib
parents: 37065
diff changeset
114 frame = av_frame_alloc();
33139
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
115
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
116 if (!(avctx && frame)) {
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
117 av_free(frame);
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
118 av_free(avctx);
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
119 av_free(data);
35359
4bdb1cc07953 Check return value after reading file.
ib
parents: 35358
diff changeset
120 return 6;
33139
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
121 }
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
122
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
123 avcodec_register_all();
35715
8517826b0dbd Replace CODEC_IDs their modern AV_-prefixed counterparts.
diego
parents: 35662
diff changeset
124 avcodec_open2(avctx, avcodec_find_decoder(AV_CODEC_ID_PNG), NULL);
33139
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
125
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
126 av_init_packet(&pkt);
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
127 pkt.data = data;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
128 pkt.size = len;
34684
b03481253518 Cosmetic: Prefer C style for single line comments.
ib
parents: 34517
diff changeset
129 /* HACK: Make PNGs decode normally instead of as CorePNG delta frames. */
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
130 pkt.flags = AV_PKT_FLAG_KEY;
33139
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
131
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
132 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt);
33134
ffe524b8b8f3 Make len match ftell() return value type.
ib
parents: 33133
diff changeset
133
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
134 memset(img, 0, sizeof(*img));
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
135
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
136 switch (avctx->pix_fmt) {
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
137 case PIX_FMT_GRAY8:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
138 img->Bpp = 8;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
139 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
140
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
141 case PIX_FMT_GRAY16BE:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
142 img->Bpp = 16;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
143 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
144
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
145 case PIX_FMT_RGB24:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
146 img->Bpp = 24;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
147 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
148
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
149 case PIX_FMT_RGBA:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
150 img->Bpp = 32;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
151 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
152
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
153 default:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
154 img->Bpp = 0;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
155 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
156 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
157
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
158 if (decode_ok && img->Bpp) {
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
159 img->Width = avctx->width;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
160 img->Height = avctx->height;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
161 bpl = img->Width * (img->Bpp / 8);
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
162 img->ImageSize = bpl * img->Height;
33136
07be88814256 Move debug messages.
ib
parents: 33135
diff changeset
163
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
164 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname);
36996
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
165 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %ux%u, color depth: %u\n", img->Width, img->Height, img->Bpp);
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
166 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %u\n", img->ImageSize);
33136
07be88814256 Move debug messages.
ib
parents: 33135
diff changeset
167
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
168 img->Image = malloc(img->ImageSize);
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
169
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
170 if (img->Image)
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
171 memcpy_pic(img->Image, frame->data[0], bpl, img->Height, bpl, frame->linesize[0]);
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
172 else
35493
411875efca3f Introduce boolean symbolic constants.
ib
parents: 35359
diff changeset
173 decode_ok = False;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
174 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
175
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
176 avcodec_close(avctx);
33138
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
177 av_free(frame);
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
178 av_free(avctx);
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
179 av_free(data);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
180
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
181 return !(decode_ok && img->Bpp);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
182 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
183
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
184 /**
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
185 * @brief Convert a 24-bit RGB or 32-bit RGBA image into a 32-bit ARGB image.
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
186 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
187 * @param img image to be converted
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
188 *
35493
411875efca3f Introduce boolean symbolic constants.
ib
parents: 35359
diff changeset
189 * @return #True (ok) or #False (error)
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
190 *
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
191 * @note This is an in-place conversion,
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
192 * new memory will be allocated for @a img if necessary.
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
193 */
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
194 static int convert_ARGB(guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
195 {
36996
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
196 unsigned char *orgImage;
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
197 unsigned int i, c;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
198
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
199 if (img->Bpp == 24) {
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
200 orgImage = img->Image;
33132
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
201
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
202 img->Bpp = 32;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
203 img->ImageSize = img->Width * img->Height * 4;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
204 img->Image = calloc(1, img->ImageSize);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
205
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
206 if (!img->Image) {
33132
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
207 free(orgImage);
36996
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
208 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %u\n", img->ImageSize);
35493
411875efca3f Introduce boolean symbolic constants.
ib
parents: 35359
diff changeset
209 return False;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
210 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
211
36996
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
212 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %u\n", img->ImageSize);
33112
654dfdf176ac Revise debug messages.
ib
parents: 33053
diff changeset
213
34517
03a6ae3bee1e Cosmetic: Rename variables.
ib
parents: 34515
diff changeset
214 for (i = 0, c = 0; i < img->ImageSize; i += 4, c += 3)
03a6ae3bee1e Cosmetic: Rename variables.
ib
parents: 34515
diff changeset
215 *(uint32_t *)&img->Image[i] = ALPHA_OPAQUE | AV_RB24(&orgImage[c]);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
216
33132
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
217 free(orgImage);
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
218 } else if (img->Bpp == 32) {
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
219 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp ARGB conversion\n");
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
220
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
221 for (i = 0; i < img->ImageSize; i += 4)
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
222 *(uint32_t *)&img->Image[i] = (img->Image[i + 3] << 24) | AV_RB24(&img->Image[i]);
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
223 } else
35493
411875efca3f Introduce boolean symbolic constants.
ib
parents: 35359
diff changeset
224 return False;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
225
35493
411875efca3f Introduce boolean symbolic constants.
ib
parents: 35359
diff changeset
226 return True;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
227 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
228
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
229 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
230 * @brief Read a PNG file.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
231 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
232 * @param fname filename (with path, but may lack extension)
37065
b28b632efeef Cosmetic: Revise a few comments.
ib
parents: 36996
diff changeset
233 * @param img memory location to store the image data
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
234 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
235 * @return 0 (ok), -1 (color depth too low), -2 (not accessible),
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
236 * -5 (#pngRead() error) or -8 (#convert_ARGB() error)
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
237 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
238 int bpRead(const char *fname, guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
239 {
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
240 int r;
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
241
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
242 fname = fExist(fname);
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
243
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
244 if (!fname)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
245 return -2;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
246
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
247 r = pngRead(fname, img);
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
248
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
249 if (r != 0) {
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
250 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error #%d: %s\n", r, fname);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
251 return -5;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
252 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
253
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
254 if (img->Bpp < 24) {
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
255 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", img->Bpp);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
256 return -1;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
257 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
258
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
259 if (!convert_ARGB(img))
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
260 return -8;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
261
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
262 return 0;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
263 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
264
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
265 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
266 * @brief Free all memory allocated to an image and set all its pointers to NULL.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
267 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
268 * @param img image to be freed
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
269 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
270 void bpFree(guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
271 {
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
272 free(img->Image);
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
273 memset(img, 0, sizeof(*img));
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
274 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
275
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
276 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
277 * @brief Render a bitmap mask for an image.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
278 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
279 * @param in image to render a bitmap mask from
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
280 * @param out bitmap mask
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
281 *
35493
411875efca3f Introduce boolean symbolic constants.
ib
parents: 35359
diff changeset
282 * @return #True (ok) or #False (error)
34132
085b62ad0cf9 Add comment to bpRenderMask().
ib
parents: 34131
diff changeset
283 *
085b62ad0cf9 Add comment to bpRenderMask().
ib
parents: 34131
diff changeset
284 * @note As a side effect, transparent pixels of @a in will be rendered black.
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
285 */
33710
fd498969e72d Clean up some function prototypes.
reimar
parents: 33690
diff changeset
286 int bpRenderMask(const guiImage *in, guiImage *out)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
287 {
33118
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
288 uint32_t *buf;
36996
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
289 unsigned int x, y;
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
290 unsigned int i = 0, c = 0;
34130
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
291 unsigned char tmp = 0, b = 1;
33118
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
292 int shaped = 0;
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
293
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
294 out->Width = in->Width;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
295 out->Height = in->Height;
33555
c5a19bbeac2b Cosmetic: Rename some GUI variables and functions.
ib
parents: 33534
diff changeset
296 out->Bpp = 1;
34130
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
297 out->ImageSize = ((out->Width + 7) / 8) * out->Height;
33118
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
298 out->Image = calloc(1, out->ImageSize);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
299
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
300 if (!out->Image) {
36996
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
301 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %u\n", out->ImageSize);
35493
411875efca3f Introduce boolean symbolic constants.
ib
parents: 35359
diff changeset
302 return False;
33125
9c178cf14a61 Get rid of else-block to improve readability.
ib
parents: 33122
diff changeset
303 }
9c178cf14a61 Get rid of else-block to improve readability.
ib
parents: 33122
diff changeset
304
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
305 buf = (uint32_t *)in->Image;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
306
34130
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
307 for (y = 0; y < in->Height; y++) {
34131
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
308 for (x = 0; x < in->Width; x++) {
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
309 if (!IS_TRANSPARENT(buf[i]))
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
310 tmp |= b;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
311 else {
34132
085b62ad0cf9 Add comment to bpRenderMask().
ib
parents: 34131
diff changeset
312 buf[i] = 0; // pixel should be black (if transparency isn't supported)
34131
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
313 shaped = 1;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
314 }
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
315
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
316 i++;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
317 b <<= 1;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
318
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
319 if (b == 0) {
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
320 out->Image[c++] = tmp;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
321 tmp = 0;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
322 b = 1;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
323 }
33128
64d19aaf624a Revert r33214.
ib
parents: 33127
diff changeset
324 }
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
325
34131
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
326 if (b != 1) {
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
327 out->Image[c++] = tmp;
34130
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
328 tmp = 0;
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
329 b = 1;
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
330 }
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
331 }
33112
654dfdf176ac Revise debug messages.
ib
parents: 33053
diff changeset
332
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
333 if (!shaped)
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
334 bpFree(out);
33114
5ad451a5ebec Remove useless warning message.
ib
parents: 33113
diff changeset
335
36996
a17b55521dd2 Don't use data type long.
ib
parents: 36032
diff changeset
336 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %u\n", out->ImageSize);
33133
41485430b18a Move debug message.
ib
parents: 33132
diff changeset
337
35493
411875efca3f Introduce boolean symbolic constants.
ib
parents: 35359
diff changeset
338 return True;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
339 }