annotate gui/util/bitmap.c @ 34684:b03481253518

Cosmetic: Prefer C style for single line comments. Additionally, slightly revise some of these comments.
author ib
date Fri, 24 Feb 2012 17:16:54 +0000
parents 03a6ae3bee1e
children 37b9f0e82a31
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
34468
ef94d1d0e265 Replace deprecated avcodec functions by new ones.
ib
parents: 34132
diff changeset
81 avctx = avcodec_alloc_context3(NULL);
33046
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();
34468
ef94d1d0e265 Replace deprecated avcodec functions by new ones.
ib
parents: 34132
diff changeset
92 avcodec_open2(avctx, avcodec_find_decoder(CODEC_ID_PNG), NULL);
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;
34684
b03481253518 Cosmetic: Prefer C style for single line comments.
ib
parents: 34517
diff changeset
97 /* 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
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
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
117 case PIX_FMT_RGBA:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
118 img->Bpp = 32;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
119 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
120
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
121 default:
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
122 img->Bpp = 0;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
123 break;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
124 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
125
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
126 if (decode_ok && img->Bpp) {
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
127 img->Width = avctx->width;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
128 img->Height = avctx->height;
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
129 bpl = img->Width * (img->Bpp / 8);
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
130 img->ImageSize = bpl * img->Height;
33136
07be88814256 Move debug messages.
ib
parents: 33135
diff changeset
131
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
132 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
133 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
134 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", img->ImageSize);
33136
07be88814256 Move debug messages.
ib
parents: 33135
diff changeset
135
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
136 img->Image = malloc(img->ImageSize);
33135
36fdc061b4a8 Prevent segmentation faults.
ib
parents: 33134
diff changeset
137
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
138 if (img->Image)
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
139 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
140 else
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
141 decode_ok = 0;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
142 }
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 avcodec_close(avctx);
33138
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
145 av_free(frame);
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
146 av_free(avctx);
3335d117f6e6 Replace av_freep by av_free
ib
parents: 33137
diff changeset
147 av_free(data);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
148
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
149 return !(decode_ok && img->Bpp);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
150 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
151
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
152 /**
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
153 * @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
154 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
155 * @param img image to be converted
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
156 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
157 * @return 1 (ok) or 0 (error)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
158 *
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
159 * @note This is an in-place conversion,
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
160 * 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
161 */
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
162 static int convert_ARGB(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
34517
03a6ae3bee1e Cosmetic: Rename variables.
ib
parents: 34515
diff changeset
182 for (i = 0, c = 0; i < img->ImageSize; i += 4, c += 3)
03a6ae3bee1e Cosmetic: Rename variables.
ib
parents: 34515
diff changeset
183 *(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
184
33132
6ac1b560e209 Revise Convert24to32().
ib
parents: 33131
diff changeset
185 free(orgImage);
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
186 } else if (img->Bpp == 32) {
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
187 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp ARGB conversion\n");
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
188
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
189 for (i = 0; i < img->ImageSize; i += 4)
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
190 *(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
191 } else
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
192 return 0;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
193
33121
7623be6c1f0e Rename conv24to32() Convert24to32().
ib
parents: 33120
diff changeset
194 return 1;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
195 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
196
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
197 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
198 * @brief Check whether a (PNG) file exists.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
199 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
200 * @param fname filename (with path, but may lack extension)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
201 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
202 * @return path including extension (ok) or NULL (not accessible)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
203 */
33710
fd498969e72d Clean up some function prototypes.
reimar
parents: 33690
diff changeset
204 static const char *fExist(const char *fname)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
205 {
33141
fd90a10d81a1 Define ext array more skillfully.
ib
parents: 33139
diff changeset
206 static const char ext[][4] = { "png", "PNG" };
33710
fd498969e72d Clean up some function prototypes.
reimar
parents: 33690
diff changeset
207 static char buf[512];
33130
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
208 unsigned int 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(fname, R_OK) == 0)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
211 return fname;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
212
33130
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
213 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) {
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
214 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
215
33130
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
216 if (access(buf, R_OK) == 0)
4f60cd16ac04 Clean up and simplify fExist().
ib
parents: 33129
diff changeset
217 return buf;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
218 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
219
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
220 return NULL;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
221 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
222
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
223 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
224 * @brief Read a PNG file.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
225 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
226 * @param fname filename (with path, but may lack extension)
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
227 * @param img pointer suitable to store the image data
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
228 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
229 * @return 0 (ok), -1 (color depth too low), -2 (not accessible),
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
230 * -5 (#pngRead() error) or -8 (#convert_ARGB() error)
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
231 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
232 int bpRead(const char *fname, guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
233 {
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
234 int r;
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
235
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
236 fname = fExist(fname);
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
237
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
238 if (!fname)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
239 return -2;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
240
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
241 r = pngRead(fname, img);
33137
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
242
f884fb919ac5 Remove various pngRead() debug messages.
ib
parents: 33136
diff changeset
243 if (r != 0) {
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
244 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
245 return -5;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
246 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
247
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
248 if (img->Bpp < 24) {
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
249 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
250 return -1;
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
34515
a9d2dcb0f435 Fix skin PNG read errors.
ib
parents: 34468
diff changeset
253 if (!convert_ARGB(img))
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
254 return -8;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
255
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
256 return 0;
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
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
259 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
260 * @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
261 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
262 * @param img image to be freed
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
263 */
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
264 void bpFree(guiImage *img)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
265 {
33983
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
266 free(img->Image);
2218c589f9ab Cosmetic: Rename parameters bf img in bitmap.c.
ib
parents: 33710
diff changeset
267 memset(img, 0, sizeof(*img));
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
268 }
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
269
33984
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
270 /**
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
271 * @brief Render a bitmap mask for an image.
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
272 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
273 * @param in image to render a bitmap mask from
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
274 * @param out bitmap mask
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
275 *
60449f4234f7 Add doxygen comments to bitmap.c and bitmap.h.
ib
parents: 33983
diff changeset
276 * @return 1 (ok) or 0 (error)
34132
085b62ad0cf9 Add comment to bpRenderMask().
ib
parents: 34131
diff changeset
277 *
085b62ad0cf9 Add comment to bpRenderMask().
ib
parents: 34131
diff changeset
278 * @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
279 */
33710
fd498969e72d Clean up some function prototypes.
reimar
parents: 33690
diff changeset
280 int bpRenderMask(const guiImage *in, guiImage *out)
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
281 {
33118
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
282 uint32_t *buf;
34130
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
283 unsigned long x, y;
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
284 unsigned long i = 0, c = 0;
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
285 unsigned char tmp = 0, b = 1;
33118
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
286 int shaped = 0;
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
287
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
288 out->Width = in->Width;
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
289 out->Height = in->Height;
33555
c5a19bbeac2b Cosmetic: Rename some GUI variables and functions.
ib
parents: 33534
diff changeset
290 out->Bpp = 1;
34130
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
291 out->ImageSize = ((out->Width + 7) / 8) * out->Height;
33118
ae464a957cd0 Move variable definitions at top of function.
ib
parents: 33117
diff changeset
292 out->Image = calloc(1, out->ImageSize);
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
293
33122
1d81476c0d1e Cosmetic: Rephrase some conditions and rename some variables.
ib
parents: 33121
diff changeset
294 if (!out->Image) {
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
295 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
296 return 0;
33125
9c178cf14a61 Get rid of else-block to improve readability.
ib
parents: 33122
diff changeset
297 }
9c178cf14a61 Get rid of else-block to improve readability.
ib
parents: 33122
diff changeset
298
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
299 buf = (uint32_t *)in->Image;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
300
34130
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
301 for (y = 0; y < in->Height; y++) {
34131
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
302 for (x = 0; x < in->Width; x++) {
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
303 if (!IS_TRANSPARENT(buf[i]))
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
304 tmp |= b;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
305 else {
34132
085b62ad0cf9 Add comment to bpRenderMask().
ib
parents: 34131
diff changeset
306 buf[i] = 0; // pixel should be black (if transparency isn't supported)
34131
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
307 shaped = 1;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
308 }
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
309
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
310 i++;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
311 b <<= 1;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
312
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
313 if (b == 0) {
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
314 out->Image[c++] = tmp;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
315 tmp = 0;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
316 b = 1;
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
317 }
33128
64d19aaf624a Revert r33214.
ib
parents: 33127
diff changeset
318 }
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
319
34131
db5ddd7433c8 Cosmetic: Adjust indent.
ib
parents: 34130
diff changeset
320 if (b != 1) {
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
321 out->Image[c++] = tmp;
34130
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
322 tmp = 0;
b0c63b098b6b Fix bug in bpRenderMask().
ib
parents: 33985
diff changeset
323 b = 1;
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
324 }
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
325 }
33112
654dfdf176ac Revise debug messages.
ib
parents: 33053
diff changeset
326
33126
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
327 if (!shaped)
a5dc70e2cda3 Cosmetic: Adjust indent.
ib
parents: 33125
diff changeset
328 bpFree(out);
33114
5ad451a5ebec Remove useless warning message.
ib
parents: 33113
diff changeset
329
33985
d99f341d8442 Replace all mp_dbg() calls by mp_msg().
ib
parents: 33984
diff changeset
330 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
331
33114
5ad451a5ebec Remove useless warning message.
ib
parents: 33113
diff changeset
332 return 1;
33046
9684ad0e1291 Move files with auxiliary functions to own directory.
ib
parents:
diff changeset
333 }