Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 37065:b28b632efeef
Cosmetic: Revise a few comments.
| author | ib |
|---|---|
| date | Thu, 24 Apr 2014 11:31:31 +0000 |
| parents | a17b55521dd2 |
| children | 15efbc138bd3 |
| rev | line source |
|---|---|
| 33046 | 1 /* |
| 2 * This file is part of MPlayer. | |
| 3 * | |
| 4 * MPlayer is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; either version 2 of the License, or | |
| 7 * (at your option) any later version. | |
| 8 * | |
| 9 * MPlayer is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License along | |
| 15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
| 16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 17 */ | |
| 18 | |
| 33984 | 19 /** |
| 20 * @file | |
| 21 * @brief Image loader and bitmap mask rendering | |
| 22 */ | |
| 23 | |
| 33046 | 24 #include <stdio.h> |
| 25 #include <stdlib.h> | |
| 26 #include <string.h> | |
| 33130 | 27 #include <unistd.h> |
| 33046 | 28 |
| 29 #include "bitmap.h" | |
| 35525 | 30 #include "gui/app/gui.h" |
| 33046 | 31 |
| 32 #include "help_mp.h" | |
| 36032 | 33 #include "mp_msg.h" |
| 33046 | 34 #include "libavcodec/avcodec.h" |
| 33690 | 35 #include "libavutil/common.h" |
| 33046 | 36 #include "libavutil/intreadwrite.h" |
| 37 #include "libvo/fastmemcpy.h" | |
| 38 | |
| 33984 | 39 /** |
| 35662 | 40 * @brief Check whether a (PNG) file exists. |
| 41 * | |
| 42 * @param fname filename (with path, but may lack extension) | |
| 43 * | |
| 44 * @return path including extension (ok) or NULL (not accessible) | |
| 45 */ | |
| 46 static const char *fExist(const char *fname) | |
| 47 { | |
| 48 static const char ext[][4] = { "png", "PNG" }; | |
| 49 static char buf[512]; | |
| 50 unsigned int i; | |
| 51 | |
| 52 if (access(fname, R_OK) == 0) | |
| 53 return fname; | |
| 54 | |
| 55 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { | |
| 56 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
| 57 | |
| 58 if (access(buf, R_OK) == 0) | |
| 59 return buf; | |
| 60 } | |
| 61 | |
| 62 return NULL; | |
| 63 } | |
| 64 | |
| 65 /** | |
| 33984 | 66 * @brief Read and decode a PNG file into bitmap data. |
| 67 * | |
| 68 * @param fname filename (with path) | |
| 37065 | 69 * @param img memory location to store the image data |
| 33984 | 70 * |
| 71 * @return 0 (ok), 1 (decoding error), 2 (open error), 3 (file too big), | |
| 35359 | 72 * 4 (out of memory), 5 (read error), 6 (avcodec alloc error) |
| 33984 | 73 */ |
| 33983 | 74 static int pngRead(const char *fname, guiImage *img) |
| 33046 | 75 { |
|
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
76 FILE *file; |
| 35359 | 77 size_t len, l; |
| 33046 | 78 void *data; |
| 33135 | 79 int decode_ok, bpl; |
| 33046 | 80 AVCodecContext *avctx; |
| 81 AVFrame *frame; | |
| 82 AVPacket pkt; | |
| 83 | |
|
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
84 file = fopen(fname, "rb"); |
| 33046 | 85 |
| 33137 | 86 if (!file) |
| 87 return 2; | |
| 33046 | 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 | 91 |
| 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 | 94 return 3; |
| 33046 | 95 } |
| 96 | |
| 97 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); | |
| 98 | |
| 33135 | 99 if (!data) { |
| 100 fclose(file); | |
| 33137 | 101 return 4; |
| 33135 | 102 } |
| 103 | |
|
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
104 fseek(file, 0, SEEK_SET); |
| 35359 | 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 | 107 |
| 35359 | 108 if (l != 1) { |
| 109 av_free(data); | |
| 110 return 5; | |
| 111 } | |
| 112 | |
| 34468 | 113 avctx = avcodec_alloc_context3(NULL); |
| 33046 | 114 frame = avcodec_alloc_frame(); |
| 33139 | 115 |
| 116 if (!(avctx && frame)) { | |
| 117 av_free(frame); | |
| 118 av_free(avctx); | |
| 119 av_free(data); | |
| 35359 | 120 return 6; |
| 33139 | 121 } |
| 122 | |
| 33046 | 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 | 125 |
| 33046 | 126 av_init_packet(&pkt); |
| 127 pkt.data = data; | |
| 128 pkt.size = len; | |
| 34684 | 129 /* HACK: Make PNGs decode normally instead of as CorePNG delta frames. */ |
| 33046 | 130 pkt.flags = AV_PKT_FLAG_KEY; |
| 33139 | 131 |
| 33046 | 132 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); |
| 33134 | 133 |
| 33983 | 134 memset(img, 0, sizeof(*img)); |
| 33046 | 135 |
| 136 switch (avctx->pix_fmt) { | |
| 137 case PIX_FMT_GRAY8: | |
| 33983 | 138 img->Bpp = 8; |
| 33046 | 139 break; |
| 140 | |
| 141 case PIX_FMT_GRAY16BE: | |
| 33983 | 142 img->Bpp = 16; |
| 33046 | 143 break; |
| 144 | |
| 145 case PIX_FMT_RGB24: | |
| 33983 | 146 img->Bpp = 24; |
| 33046 | 147 break; |
| 148 | |
| 34515 | 149 case PIX_FMT_RGBA: |
| 33983 | 150 img->Bpp = 32; |
| 33046 | 151 break; |
| 152 | |
| 153 default: | |
| 33983 | 154 img->Bpp = 0; |
| 33046 | 155 break; |
| 156 } | |
| 157 | |
| 33983 | 158 if (decode_ok && img->Bpp) { |
| 159 img->Width = avctx->width; | |
| 160 img->Height = avctx->height; | |
| 161 bpl = img->Width * (img->Bpp / 8); | |
| 162 img->ImageSize = bpl * img->Height; | |
| 33136 | 163 |
| 33985 | 164 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname); |
| 36996 | 165 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %ux%u, color depth: %u\n", img->Width, img->Height, img->Bpp); |
| 166 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %u\n", img->ImageSize); | |
| 33136 | 167 |
| 33983 | 168 img->Image = malloc(img->ImageSize); |
| 33135 | 169 |
| 33983 | 170 if (img->Image) |
| 171 memcpy_pic(img->Image, frame->data[0], bpl, img->Height, bpl, frame->linesize[0]); | |
| 33137 | 172 else |
| 35493 | 173 decode_ok = False; |
| 33046 | 174 } |
| 175 | |
| 176 avcodec_close(avctx); | |
| 33138 | 177 av_free(frame); |
| 178 av_free(avctx); | |
| 179 av_free(data); | |
| 33046 | 180 |
| 33983 | 181 return !(decode_ok && img->Bpp); |
| 33046 | 182 } |
| 183 | |
| 33984 | 184 /** |
| 34515 | 185 * @brief Convert a 24-bit RGB or 32-bit RGBA image into a 32-bit ARGB image. |
| 33984 | 186 * |
| 187 * @param img image to be converted | |
| 188 * | |
| 35493 | 189 * @return #True (ok) or #False (error) |
| 33984 | 190 * |
| 34515 | 191 * @note This is an in-place conversion, |
| 192 * new memory will be allocated for @a img if necessary. | |
| 33984 | 193 */ |
| 34515 | 194 static int convert_ARGB(guiImage *img) |
| 33046 | 195 { |
| 36996 | 196 unsigned char *orgImage; |
| 197 unsigned int i, c; | |
| 33046 | 198 |
| 33983 | 199 if (img->Bpp == 24) { |
| 200 orgImage = img->Image; | |
| 33132 | 201 |
| 33983 | 202 img->Bpp = 32; |
| 203 img->ImageSize = img->Width * img->Height * 4; | |
| 204 img->Image = calloc(1, img->ImageSize); | |
| 33046 | 205 |
| 33983 | 206 if (!img->Image) { |
| 33132 | 207 free(orgImage); |
| 36996 | 208 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %u\n", img->ImageSize); |
| 35493 | 209 return False; |
| 33046 | 210 } |
| 211 | |
| 36996 | 212 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %u\n", img->ImageSize); |
| 33112 | 213 |
| 34517 | 214 for (i = 0, c = 0; i < img->ImageSize; i += 4, c += 3) |
| 215 *(uint32_t *)&img->Image[i] = ALPHA_OPAQUE | AV_RB24(&orgImage[c]); | |
| 33046 | 216 |
| 33132 | 217 free(orgImage); |
| 34515 | 218 } else if (img->Bpp == 32) { |
| 219 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp ARGB conversion\n"); | |
| 220 | |
| 221 for (i = 0; i < img->ImageSize; i += 4) | |
| 222 *(uint32_t *)&img->Image[i] = (img->Image[i + 3] << 24) | AV_RB24(&img->Image[i]); | |
| 223 } else | |
| 35493 | 224 return False; |
| 33046 | 225 |
| 35493 | 226 return True; |
| 33046 | 227 } |
| 228 | |
| 33984 | 229 /** |
| 230 * @brief Read a PNG file. | |
| 231 * | |
| 232 * @param fname filename (with path, but may lack extension) | |
| 37065 | 233 * @param img memory location to store the image data |
| 33984 | 234 * |
| 235 * @return 0 (ok), -1 (color depth too low), -2 (not accessible), | |
| 34515 | 236 * -5 (#pngRead() error) or -8 (#convert_ARGB() error) |
| 33984 | 237 */ |
| 33983 | 238 int bpRead(const char *fname, guiImage *img) |
| 33046 | 239 { |
| 33137 | 240 int r; |
| 241 | |
| 33046 | 242 fname = fExist(fname); |
| 243 | |
|
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
244 if (!fname) |
| 33046 | 245 return -2; |
| 246 | |
| 33983 | 247 r = pngRead(fname, img); |
| 33137 | 248 |
| 249 if (r != 0) { | |
| 33985 | 250 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error #%d: %s\n", r, fname); |
| 33046 | 251 return -5; |
| 252 } | |
| 253 | |
| 33983 | 254 if (img->Bpp < 24) { |
| 33985 | 255 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", img->Bpp); |
| 33046 | 256 return -1; |
| 257 } | |
| 258 | |
| 34515 | 259 if (!convert_ARGB(img)) |
| 33046 | 260 return -8; |
| 261 | |
| 262 return 0; | |
| 263 } | |
| 264 | |
| 33984 | 265 /** |
| 266 * @brief Free all memory allocated to an image and set all its pointers to NULL. | |
| 267 * | |
| 268 * @param img image to be freed | |
| 269 */ | |
| 33983 | 270 void bpFree(guiImage *img) |
| 33046 | 271 { |
| 33983 | 272 free(img->Image); |
| 273 memset(img, 0, sizeof(*img)); | |
| 33046 | 274 } |
| 275 | |
| 33984 | 276 /** |
| 277 * @brief Render a bitmap mask for an image. | |
| 278 * | |
| 279 * @param in image to render a bitmap mask from | |
| 280 * @param out bitmap mask | |
| 281 * | |
| 35493 | 282 * @return #True (ok) or #False (error) |
| 34132 | 283 * |
| 284 * @note As a side effect, transparent pixels of @a in will be rendered black. | |
| 33984 | 285 */ |
| 33710 | 286 int bpRenderMask(const guiImage *in, guiImage *out) |
| 33046 | 287 { |
| 33118 | 288 uint32_t *buf; |
| 36996 | 289 unsigned int x, y; |
| 290 unsigned int i = 0, c = 0; | |
| 34130 | 291 unsigned char tmp = 0, b = 1; |
| 33118 | 292 int shaped = 0; |
| 293 | |
| 33046 | 294 out->Width = in->Width; |
| 295 out->Height = in->Height; | |
| 33555 | 296 out->Bpp = 1; |
| 34130 | 297 out->ImageSize = ((out->Width + 7) / 8) * out->Height; |
| 33118 | 298 out->Image = calloc(1, out->ImageSize); |
| 33046 | 299 |
|
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
300 if (!out->Image) { |
| 36996 | 301 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %u\n", out->ImageSize); |
| 35493 | 302 return False; |
| 33125 | 303 } |
| 304 | |
| 33126 | 305 buf = (uint32_t *)in->Image; |
| 33046 | 306 |
| 34130 | 307 for (y = 0; y < in->Height; y++) { |
| 34131 | 308 for (x = 0; x < in->Width; x++) { |
| 309 if (!IS_TRANSPARENT(buf[i])) | |
| 310 tmp |= b; | |
| 311 else { | |
| 34132 | 312 buf[i] = 0; // pixel should be black (if transparency isn't supported) |
| 34131 | 313 shaped = 1; |
| 314 } | |
| 315 | |
| 316 i++; | |
| 317 b <<= 1; | |
| 318 | |
| 319 if (b == 0) { | |
| 320 out->Image[c++] = tmp; | |
| 321 tmp = 0; | |
| 322 b = 1; | |
| 323 } | |
| 33128 | 324 } |
| 33046 | 325 |
| 34131 | 326 if (b != 1) { |
| 33126 | 327 out->Image[c++] = tmp; |
| 34130 | 328 tmp = 0; |
| 329 b = 1; | |
| 33126 | 330 } |
| 331 } | |
| 33112 | 332 |
| 33126 | 333 if (!shaped) |
| 334 bpFree(out); | |
| 33114 | 335 |
| 36996 | 336 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %u\n", out->ImageSize); |
| 33133 | 337 |
| 35493 | 338 return True; |
| 33046 | 339 } |
