format_jpeg.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * JPEG File format support.
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <sys/types.h>
  14. #include <asterisk/channel.h>
  15. #include <asterisk/file.h>
  16. #include <asterisk/logger.h>
  17. #include <asterisk/sched.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/image.h>
  20. #include <asterisk/lock.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include <stdlib.h>
  24. #include <sys/time.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include "asterisk/endian.h"
  30. static char *desc = "JPEG (Joint Picture Experts Group) Image Format";
  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)) {
  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 = 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, 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. "jpg",
  85. "JPEG (Joint Picture Experts Group)",
  86. "jpg|jpeg",
  87. AST_FORMAT_JPEG,
  88. jpeg_read_image,
  89. jpeg_identify,
  90. jpeg_write_image,
  91. };
  92. int load_module()
  93. {
  94. return ast_image_register(&jpeg_format);
  95. }
  96. int unload_module()
  97. {
  98. ast_image_unregister(&jpeg_format);
  99. return 0;
  100. }
  101. int usecount()
  102. {
  103. /* We never really have any users */
  104. return 0;
  105. }
  106. char *description()
  107. {
  108. return desc;
  109. }
  110. char *key()
  111. {
  112. return ASTERISK_GPL_KEY;
  113. }