format_jpeg.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. static struct ast_frame *jpeg_read_image(int fd, int len)
  35. {
  36. struct ast_frame fr;
  37. int res;
  38. char buf[65536];
  39. if (len > sizeof(buf) || len < 0) {
  40. ast_log(LOG_WARNING, "JPEG image too large to read\n");
  41. return NULL;
  42. }
  43. res = read(fd, buf, len);
  44. if (res < len) {
  45. ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
  46. }
  47. memset(&fr, 0, sizeof(fr));
  48. fr.frametype = AST_FRAME_IMAGE;
  49. ast_format_set(&fr.subclass.format, AST_FORMAT_JPEG, 0);
  50. fr.data.ptr = buf;
  51. fr.src = "JPEG Read";
  52. fr.datalen = len;
  53. return ast_frisolate(&fr);
  54. }
  55. static int jpeg_identify(int fd)
  56. {
  57. char buf[10];
  58. int res;
  59. res = read(fd, buf, sizeof(buf));
  60. if (res < sizeof(buf))
  61. return 0;
  62. if (memcmp(buf + 6, "JFIF", 4))
  63. return 0;
  64. return 1;
  65. }
  66. static int jpeg_write_image(int fd, struct ast_frame *fr)
  67. {
  68. int res=0;
  69. if (fr->frametype != AST_FRAME_IMAGE) {
  70. ast_log(LOG_WARNING, "Not an image\n");
  71. return -1;
  72. }
  73. if (fr->subclass.format.id != AST_FORMAT_JPEG) {
  74. ast_log(LOG_WARNING, "Not a jpeg image\n");
  75. return -1;
  76. }
  77. if (fr->datalen) {
  78. res = write(fd, fr->data.ptr, fr->datalen);
  79. if (res != fr->datalen) {
  80. ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
  81. return -1;
  82. }
  83. }
  84. return res;
  85. }
  86. static struct ast_imager jpeg_format = {
  87. .name = "jpg",
  88. .desc = "JPEG (Joint Picture Experts Group)",
  89. .exts = "jpg|jpeg",
  90. .read_image = jpeg_read_image,
  91. .identify = jpeg_identify,
  92. .write_image = jpeg_write_image,
  93. };
  94. static int load_module(void)
  95. {
  96. ast_format_set(&jpeg_format.format, AST_FORMAT_JPEG, 0);
  97. if (ast_image_register(&jpeg_format))
  98. return AST_MODULE_LOAD_FAILURE;
  99. return AST_MODULE_LOAD_SUCCESS;
  100. }
  101. static int unload_module(void)
  102. {
  103. ast_image_unregister(&jpeg_format);
  104. return 0;
  105. }
  106. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "jpeg (joint picture experts group) image format",
  107. .load = load_module,
  108. .unload = unload_module,
  109. .load_pri = AST_MODPRI_APP_DEPEND
  110. );