annotate gui/util/bitmap.c @ 33985:d99f341d8442

Replace all mp_dbg() calls by mp_msg(). These messages are useful to track down error causes and should be available even in a non-debug version of the GUI. Additionally, remove #ifdef MP_DEBUG which isn't longer needed in skin.c now.
author ib
date Tue, 06 Sep 2011 15:28:53 +0000
parents 60449f4234f7
children b0c63b098b6b
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"
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
30
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
31 #include "help_mp.h"
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
32 #include "libavcodec/avcodec.h"
33690
76342ee379eb Add missing #include "libavutil/common.h".
ib
parents: 33555
diff changeset
33 #include "libavutil/common.h"
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
34 #include "libavutil/intreadwrite.h"
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
35 #include "libvo/fastmemcpy.h"
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
36 #include "mp_msg.h"
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
37
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
38 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
39 * @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
40 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
41 * @param fname filename (with path)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
42 * @param img pointer suitable to store the image data
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
43 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
44 * @return 0 (ok), 1 (decoding error), 2 (open error), 3 (file too big),
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
45 * 4 (out of memory), 5 (avcodec alloc error)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
46 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
47 static int pngRead(const char *fname, guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
48 {
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
49 FILE *file;
33134
ffe524b8b8f3 Make len match ftell() return value type.
ib
parents: 33133
diff changeset
50 long len;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
51 void *data;
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
52 int decode_ok, bpl;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
53 AVCodecContext *avctx;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
54 AVFrame *frame;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
55 AVPacket pkt;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
56
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
57 file = fopen(fname, "rb");
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
58
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
59 if (!file)
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
60 return 2;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
61
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
62 fseek(file, 0, SEEK_END);
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
63 len = ftell(file);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
64
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
65 if (len > 50 * 1024 * 1024) {
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
66 fclose(file);
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
67 return 3;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
68 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
69
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
70 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE);
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
71
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
72 if (!data) {
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
73 fclose(file);
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
74 return 4;
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
75 }
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
76
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
77 fseek(file, 0, SEEK_SET);
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
78 fread(data, len, 1, file);
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
79 fclose(file);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
80
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
81 avctx = avcodec_alloc_context();
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
82 frame = avcodec_alloc_frame();
33139
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
83
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
84 if (!(avctx && frame)) {
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
85 av_free(frame);
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
86 av_free(avctx);
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
87 av_free(data);
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
88 return 5;
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
89 }
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
90
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
91 avcodec_register_all();
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
92 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG));
33139
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
93
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
94 av_init_packet(&pkt);
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
95 pkt.data = data;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
96 pkt.size = len;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
97 // HACK: make PNGs decode normally instead of as CorePNG delta frames
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
98 pkt.flags = AV_PKT_FLAG_KEY;
33139
64f29d5dfea2 Prevent segmentation faults.
ib
parents: 33138
diff changeset
99
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
100 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt);
33134
ffe524b8b8f3 Make len match ftell() return value type.
ib
parents: 33133
diff changeset
101
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
102 memset(img, 0, sizeof(*img));
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
103
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
104 switch (avctx->pix_fmt) {
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
105 case PIX_FMT_GRAY8:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
106 img->Bpp = 8;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
107 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
108
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
109 case PIX_FMT_GRAY16BE:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
110 img->Bpp = 16;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
111 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
112
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
113 case PIX_FMT_RGB24:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
114 img->Bpp = 24;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
115 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
116
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
117 case PIX_FMT_BGRA:
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
118 case PIX_FMT_ARGB:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
119 img->Bpp = 32;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
120 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
121
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
122 default:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
123 img->Bpp = 0;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
124 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
125 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
126
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
127 if (decode_ok && img->Bpp) {
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
128 img->Width = avctx->width;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
129 img->Height = avctx->height;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
130 bpl = img->Width * (img->Bpp / 8);
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
131 img->ImageSize = bpl * img->Height;
33136
07be88814256 Move debug messages.
ib
parents: 33135
diff changeset
132
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
133 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname);
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
134 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %lux%lu, color depth: %u\n", img->Width, img->Height, img->Bpp);
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
135 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", img->ImageSize);
33136
07be88814256 Move debug messages.
ib
parents: 33135
diff changeset
136
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
137 img->Image = malloc(img->ImageSize);
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
138
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
139 if (img->Image)
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
140 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
141 else
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
142 decode_ok = 0;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
143 }
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 avcodec_close(avctx);
33138
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
146 av_free(frame);
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
147 av_free(avctx);
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
148 av_free(data);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
149
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
150 return !(decode_ok && img->Bpp);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
151 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
152
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
153 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
154 * @brief Convert a 24-bit color depth image into an 32-bit one.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
155 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
156 * @param img image to be converted
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
157 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
158 * @return 1 (ok) or 0 (error)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
159 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
160 * @note This is an in-place conversion, new memory will be allocated for @a img.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
161 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
162 static int Convert24to32(guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
163 {
33132
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
164 char *orgImage;
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
165 unsigned long i, c;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
166
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
167 if (img->Bpp == 24) {
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
168 orgImage = img->Image;
33132
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
169
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
170 img->Bpp = 32;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
171 img->ImageSize = img->Width * img->Height * 4;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
172 img->Image = calloc(1, img->ImageSize);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
173
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
174 if (!img->Image) {
33132
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
175 free(orgImage);
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
176 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", img->ImageSize);
33121
7623be6c1f0e Rename conv24to32() Convert24to32().
ib
parents: 33120
diff changeset
177 return 0;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
178 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
179
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
180 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %lu\n", img->ImageSize);
33112
654dfdf176ac Revise debug messages.
ib
parents: 33053
diff changeset
181
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
182 for (c = 0, i = 0; c < img->ImageSize; c += 4, i += 3)
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
183 *(uint32_t *)&img->Image[c] = ALPHA_OPAQUE | AV_RB24(&orgImage[i]);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
184
33132
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
185 free(orgImage);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
186 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
187
33121
7623be6c1f0e Rename conv24to32() Convert24to32().
ib
parents: 33120
diff changeset
188 return 1;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
189 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
190
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
191 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
192 * @brief Check whether a (PNG) file exists.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
193 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
194 * @param fname filename (with path, but may lack extension)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
195 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
196 * @return path including extension (ok) or NULL (not accessible)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
197 */
33710
fd498969e72d Clean up some function prototypes.
reimar
parents: 33690
diff changeset
198 static const char *fExist(const char *fname)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
199 {
33141
fd90a10d81a1 Define ext array more skillfully.
ib
parents: 33139
diff changeset
200 static const char ext[][4] = { "png", "PNG" };
33710
fd498969e72d Clean up some function prototypes.
reimar
parents: 33690
diff changeset
201 static char buf[512];
33130
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
202 unsigned int i;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
203
33130
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
204 if (access(fname, R_OK) == 0)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
205 return fname;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
206
33130
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
207 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) {
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
208 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
209
33130
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
210 if (access(buf, R_OK) == 0)
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
211 return buf;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
212 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
213
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
214 return NULL;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
215 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
216
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
217 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
218 * @brief Read a PNG file.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
219 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
220 * @param fname filename (with path, but may lack extension)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
221 * @param img pointer suitable to store the image data
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
222 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
223 * @return 0 (ok), -1 (color depth too low), -2 (not accessible),
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
224 * -5 (#pngRead() error) or -8 (#Convert24to32() error)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
225 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
226 int bpRead(const char *fname, guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
227 {
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
228 int r;
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
229
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
230 fname = fExist(fname);
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
231
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
232 if (!fname)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
233 return -2;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
234
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
235 r = pngRead(fname, img);
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
236
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
237 if (r != 0) {
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
238 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
239 return -5;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
240 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
241
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
242 if (img->Bpp < 24) {
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
243 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
244 return -1;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
245 }
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 if (!Convert24to32(img))
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
248 return -8;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
249
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
250 return 0;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
251 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
252
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
253 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
254 * @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
255 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
256 * @param img image to be freed
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
257 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
258 void bpFree(guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
259 {
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
260 free(img->Image);
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
261 memset(img, 0, sizeof(*img));
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
262 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
263
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
264 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
265 * @brief Render a bitmap mask for an image.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
266 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
267 * @param in image to render a bitmap mask from
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
268 * @param out bitmap mask
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
269 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
270 * @return 1 (ok) or 0 (error)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
271 */
33710
fd498969e72d Clean up some function prototypes.
reimar
parents: 33690
diff changeset
272 int bpRenderMask(const guiImage *in, guiImage *out)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
273 {
33118
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
274 uint32_t *buf;
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
275 unsigned long i;
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
276 int b = 0, c = 0;
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
277 unsigned char tmp = 0;
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
278 int shaped = 0;
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
279
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
280 out->Width = in->Width;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
281 out->Height = in->Height;
33555
c5a19bbeac2b Cosmetic: Rename some GUI variables and functions.
ib
parents: 33534
diff changeset
282 out->Bpp = 1;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
283 out->ImageSize = (out->Width * out->Height + 7) / 8;
33118
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
284 out->Image = calloc(1, out->ImageSize);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
285
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
286 if (!out->Image) {
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
287 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize);
33114
5ad451a5ebec Remove useless warning message.
ib
parents: 33113
diff changeset
288 return 0;
33125
9c178cf14a61 Get rid of else-block to improve readability.
ib
parents: 33122
diff changeset
289 }
9c178cf14a61 Get rid of else-block to improve readability.
ib
parents: 33122
diff changeset
290
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
291 buf = (uint32_t *)in->Image;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
292
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
293 for (i = 0; i < out->Width * out->Height; i++) {
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
294 tmp >>= 1;
33119
8835e684a41d Move common pixel shifting statement
ib
parents: 33118
diff changeset
295
33534
22c3241467b3 Remove function Normalize().
ib
parents: 33141
diff changeset
296 if (!IS_TRANSPARENT(buf[i]))
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
297 tmp |= 0x80;
33128
64d19aaf624a Revert r33214.
ib
parents: 33127
diff changeset
298 else {
64d19aaf624a Revert r33214.
ib
parents: 33127
diff changeset
299 buf[i] = 0;
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
300 shaped = 1;
33128
64d19aaf624a Revert r33214.
ib
parents: 33127
diff changeset
301 }
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
302
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
303 if (++b == 8) {
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
304 out->Image[c++] = tmp;
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
305 tmp = b = 0;
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
306 }
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
307 }
33112
654dfdf176ac Revise debug messages.
ib
parents: 33053
diff changeset
308
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
309 if (b)
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
310 out->Image[c] = tmp;
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
311
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
312 if (!shaped)
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
313 bpFree(out);
33114
5ad451a5ebec Remove useless warning message.
ib
parents: 33113
diff changeset
314
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
315 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize);
33133
41485430b18a Move debug message.
ib
parents: 33132
diff changeset
316
33114
5ad451a5ebec Remove useless warning message.
ib
parents: 33113
diff changeset
317 return 1;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
318 }