format_jpeg.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief JPEG File format support.
  21. *
  22. * \arg File name extension: jpeg, jpg
  23. * \ingroup formats
  24. */
  25. /*** MODULEINFO
  26. <support_level>extended</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/mod_format.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/image.h"
  33. #include "asterisk/endian.h"
  34. #include "asterisk/format_cache.h"
  35. static struct ast_frame *jpeg_read_image(int fd, int len)
  36. {
  37. struct ast_frame fr;
  38. int res;
  39. char buf[65536];
  40. if (len > sizeof(buf) || len < 0) {
  41. ast_log(LOG_WARNING, "JPEG image too large to read\n");
  42. return NULL;
  43. }
  44. res = read(fd, buf, len);
  45. if (res < len) {
  46. ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
  47. }
  48. memset(&fr, 0, sizeof(fr));
  49. fr.frametype = AST_FRAME_IMAGE;
  50. fr.subclass.format = ast_format_jpeg;
  51. fr.data.ptr = buf;
  52. fr.src = "JPEG Read";
  53. fr.datalen = len;
  54. return ast_frisolate(&fr);
  55. }
  56. static int jpeg_identify(int fd)
  57. {
  58. char buf[10];
  59. int res;
  60. res = read(fd, buf, sizeof(buf));
  61. if (res < sizeof(buf))
  62. return 0;
  63. if (memcmp(buf + 6, "JFIF", 4))
  64. return 0;
  65. return 1;
  66. }
  67. static int jpeg_write_image(int fd, struct ast_frame *fr)
  68. {
  69. int res=0;
  70. if (fr->datalen) {
  71. res = write(fd, fr->data.ptr, fr->datalen);
  72. if (res != fr->datalen) {
  73. ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
  74. return -1;
  75. }
  76. }
  77. return res;
  78. }
  79. static struct ast_imager jpeg_format = {
  80. .name = "jpg",
  81. .desc = "JPEG (Joint Picture Experts Group)",
  82. .exts = "jpg|jpeg",
  83. .read_image = jpeg_read_image,
  84. .identify = jpeg_identify,
  85. .write_image = jpeg_write_image,
  86. };
  87. static int load_module(void)
  88. {
  89. jpeg_format.format = ast_format_jpeg;
  90. if (ast_image_register(&jpeg_format))
  91. return AST_MODULE_LOAD_FAILURE;
  92. return AST_MODULE_LOAD_SUCCESS;
  93. }
  94. static int unload_module(void)
  95. {
  96. ast_image_unregister(&jpeg_format);
  97. return 0;
  98. }
  99. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "jpeg (joint picture experts group) image format",
  100. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  101. .load = load_module,
  102. .unload = unload_module,
  103. .load_pri = AST_MODPRI_APP_DEPEND
  104. );