format_jpeg.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <sys/types.h>
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. #include <stdlib.h>
  29. #include <sys/time.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. #include <string.h>
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/channel.h"
  37. #include "asterisk/file.h"
  38. #include "asterisk/logger.h"
  39. #include "asterisk/sched.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/image.h"
  42. #include "asterisk/lock.h"
  43. #include "asterisk/endian.h"
  44. static char *desc = "JPEG (Joint Picture Experts Group) Image Format";
  45. static struct ast_frame *jpeg_read_image(int fd, int len)
  46. {
  47. struct ast_frame fr;
  48. int res;
  49. char buf[65536];
  50. if (len > sizeof(buf) || len < 0) {
  51. ast_log(LOG_WARNING, "JPEG image too large to read\n");
  52. return NULL;
  53. }
  54. res = read(fd, buf, len);
  55. if (res < len) {
  56. ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
  57. }
  58. memset(&fr, 0, sizeof(fr));
  59. fr.frametype = AST_FRAME_IMAGE;
  60. fr.subclass = AST_FORMAT_JPEG;
  61. fr.data = buf;
  62. fr.src = "JPEG Read";
  63. fr.datalen = len;
  64. return ast_frisolate(&fr);
  65. }
  66. static int jpeg_identify(int fd)
  67. {
  68. char buf[10];
  69. int res;
  70. res = read(fd, buf, sizeof(buf));
  71. if (res < sizeof(buf))
  72. return 0;
  73. if (memcmp(buf + 6, "JFIF", 4))
  74. return 0;
  75. return 1;
  76. }
  77. static int jpeg_write_image(int fd, struct ast_frame *fr)
  78. {
  79. int res=0;
  80. if (fr->frametype != AST_FRAME_IMAGE) {
  81. ast_log(LOG_WARNING, "Not an image\n");
  82. return -1;
  83. }
  84. if (fr->subclass != AST_FORMAT_JPEG) {
  85. ast_log(LOG_WARNING, "Not a jpeg image\n");
  86. return -1;
  87. }
  88. if (fr->datalen) {
  89. res = write(fd, fr->data, fr->datalen);
  90. if (res != fr->datalen) {
  91. ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
  92. return -1;
  93. }
  94. }
  95. return res;
  96. }
  97. static struct ast_imager jpeg_format = {
  98. "jpg",
  99. "JPEG (Joint Picture Experts Group)",
  100. "jpg|jpeg",
  101. AST_FORMAT_JPEG,
  102. jpeg_read_image,
  103. jpeg_identify,
  104. jpeg_write_image,
  105. };
  106. int load_module()
  107. {
  108. return ast_image_register(&jpeg_format);
  109. }
  110. int unload_module()
  111. {
  112. ast_image_unregister(&jpeg_format);
  113. return 0;
  114. }
  115. int usecount()
  116. {
  117. /* We never really have any users */
  118. return 0;
  119. }
  120. char *description()
  121. {
  122. return desc;
  123. }
  124. char *key()
  125. {
  126. return ASTERISK_GPL_KEY;
  127. }