format_jpeg.c 2.7 KB

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