You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.0KB

  1. /*
  2. * Targa (.tga) image encoder
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include "avcodec.h"
  23. static int targa_encode_frame(AVCodecContext *avctx,
  24. unsigned char *outbuf,
  25. int buf_size, void *data){
  26. AVFrame *p = data;
  27. int i, n, linesize;
  28. uint8_t *ptr, *out;
  29. if(avctx->width > 0xffff || avctx->height > 0xffff) {
  30. av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
  31. return -1;
  32. }
  33. if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 45) {
  34. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  35. return -1;
  36. }
  37. p->pict_type= FF_I_TYPE;
  38. p->key_frame= 1;
  39. /* zero out the header and only set applicable fields */
  40. memset(outbuf, 0, 11);
  41. AV_WL16(outbuf+12, avctx->width);
  42. AV_WL16(outbuf+14, avctx->height);
  43. outbuf[17] = 0x20; /* origin is top-left. no alpha */
  44. /* TODO: support alpha channel and other bit-depths. and RLE? */
  45. switch(avctx->pix_fmt) {
  46. case PIX_FMT_GRAY8:
  47. outbuf[2] = 3; /* uncompressed grayscale image */
  48. outbuf[16] = 8; /* bpp */
  49. n = avctx->width;
  50. break;
  51. case PIX_FMT_BGR24:
  52. outbuf[2] = 2; /* uncompressed true-color image */
  53. outbuf[16] = 24; /* bpp */
  54. n = 3 * avctx->width;
  55. break;
  56. default:
  57. return -1;
  58. }
  59. out = outbuf + 18; /* skip past the header we just output */
  60. ptr = p->data[0];
  61. linesize = p->linesize[0];
  62. for(i=0; i < avctx->height; i++) {
  63. memcpy(out, ptr, n);
  64. out += n;
  65. ptr += linesize;
  66. }
  67. /* The standard recommends including this section, even if we don't use
  68. * any of the features it affords. TODO: take advantage of the pixel
  69. * aspect ratio and encoder ID fields available? */
  70. memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
  71. return out + 26 - outbuf;
  72. }
  73. static int targa_encode_init(AVCodecContext *avctx)
  74. {
  75. return 0;
  76. }
  77. AVCodec targa_encoder = {
  78. .name = "targa",
  79. .type = CODEC_TYPE_VIDEO,
  80. .id = CODEC_ID_TARGA,
  81. .priv_data_size = 0,
  82. .init = targa_encode_init,
  83. .encode = targa_encode_frame,
  84. .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_GRAY8, -1},
  85. };