00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "asterisk.h"
00028
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 117802 $")
00030
00031 #include "asterisk/mod_format.h"
00032 #include "asterisk/module.h"
00033 #include "asterisk/image.h"
00034 #include "asterisk/endian.h"
00035
00036 static struct ast_frame *jpeg_read_image(int fd, int len)
00037 {
00038 struct ast_frame fr;
00039 int res;
00040 char buf[65536];
00041 if (len > sizeof(buf) || len < 0) {
00042 ast_log(LOG_WARNING, "JPEG image too large to read\n");
00043 return NULL;
00044 }
00045 res = read(fd, buf, len);
00046 if (res < len) {
00047 ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
00048 }
00049 memset(&fr, 0, sizeof(fr));
00050 fr.frametype = AST_FRAME_IMAGE;
00051 fr.subclass = AST_FORMAT_JPEG;
00052 fr.data.ptr = buf;
00053 fr.src = "JPEG Read";
00054 fr.datalen = len;
00055 return ast_frisolate(&fr);
00056 }
00057
00058 static int jpeg_identify(int fd)
00059 {
00060 char buf[10];
00061 int res;
00062 res = read(fd, buf, sizeof(buf));
00063 if (res < sizeof(buf))
00064 return 0;
00065 if (memcmp(buf + 6, "JFIF", 4))
00066 return 0;
00067 return 1;
00068 }
00069
00070 static int jpeg_write_image(int fd, struct ast_frame *fr)
00071 {
00072 int res=0;
00073 if (fr->frametype != AST_FRAME_IMAGE) {
00074 ast_log(LOG_WARNING, "Not an image\n");
00075 return -1;
00076 }
00077 if (fr->subclass != AST_FORMAT_JPEG) {
00078 ast_log(LOG_WARNING, "Not a jpeg image\n");
00079 return -1;
00080 }
00081 if (fr->datalen) {
00082 res = write(fd, fr->data.ptr, fr->datalen);
00083 if (res != fr->datalen) {
00084 ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
00085 return -1;
00086 }
00087 }
00088 return res;
00089 }
00090
00091 static struct ast_imager jpeg_format = {
00092 .name = "jpg",
00093 .desc = "JPEG (Joint Picture Experts Group)",
00094 .exts = "jpg|jpeg",
00095 .format = AST_FORMAT_JPEG,
00096 .read_image = jpeg_read_image,
00097 .identify = jpeg_identify,
00098 .write_image = jpeg_write_image,
00099 };
00100
00101 static int load_module(void)
00102 {
00103 if (ast_image_register(&jpeg_format))
00104 return AST_MODULE_LOAD_FAILURE;
00105 return AST_MODULE_LOAD_SUCCESS;
00106 }
00107
00108 static int unload_module(void)
00109 {
00110 ast_image_unregister(&jpeg_format);
00111
00112 return 0;
00113 }
00114
00115 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "JPEG (Joint Picture Experts Group) Image Format");