format_jpeg.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief JPEG File format support.
00022  * 
00023  * \arg File name extension: jpeg, jpg
00024  * \ingroup formats
00025  */
00026 
00027 /*** MODULEINFO
00028    <support_level>extended</support_level>
00029  ***/
00030  
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00034 
00035 #include "asterisk/mod_format.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/image.h"
00038 #include "asterisk/endian.h"
00039 #include "asterisk/format_cache.h"
00040 
00041 static struct ast_frame *jpeg_read_image(int fd, int len)
00042 {
00043    struct ast_frame fr;
00044    int res;
00045    char buf[65536];
00046    if (len > sizeof(buf) || len < 0) {
00047       ast_log(LOG_WARNING, "JPEG image too large to read\n");
00048       return NULL;
00049    }
00050    res = read(fd, buf, len);
00051    if (res < len) {
00052       ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
00053    }
00054    memset(&fr, 0, sizeof(fr));
00055    fr.frametype = AST_FRAME_IMAGE;
00056    fr.subclass.format = ast_format_jpeg;
00057    fr.data.ptr = buf;
00058    fr.src = "JPEG Read";
00059    fr.datalen = len;
00060    return ast_frisolate(&fr);
00061 }
00062 
00063 static int jpeg_identify(int fd)
00064 {
00065    char buf[10];
00066    int res;
00067    res = read(fd, buf, sizeof(buf));
00068    if (res < sizeof(buf))
00069       return 0;
00070    if (memcmp(buf + 6, "JFIF", 4))
00071       return 0;
00072    return 1;
00073 }
00074 
00075 static int jpeg_write_image(int fd, struct ast_frame *fr)
00076 {
00077    int res=0;
00078    if (fr->datalen) {
00079       res = write(fd, fr->data.ptr, fr->datalen);
00080       if (res != fr->datalen) {
00081          ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
00082          return -1;
00083       }
00084    }
00085    return res;
00086 }
00087 
00088 static struct ast_imager jpeg_format = {
00089    .name = "jpg",
00090    .desc = "JPEG (Joint Picture Experts Group)",
00091    .exts = "jpg|jpeg",
00092    .read_image = jpeg_read_image,
00093    .identify = jpeg_identify,
00094    .write_image = jpeg_write_image,
00095 };
00096 
00097 static int load_module(void)
00098 {
00099    jpeg_format.format = ast_format_jpeg;
00100    if (ast_image_register(&jpeg_format))
00101       return AST_MODULE_LOAD_FAILURE;
00102    return AST_MODULE_LOAD_SUCCESS;
00103 }
00104 
00105 static int unload_module(void)
00106 {
00107    ast_image_unregister(&jpeg_format);
00108 
00109    return 0;
00110 }
00111 
00112 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "jpeg (joint picture experts group) image format",
00113    .support_level = AST_MODULE_SUPPORT_EXTENDED,
00114    .load = load_module,
00115    .unload = unload_module,
00116    .load_pri = AST_MODPRI_APP_DEPEND
00117 );

Generated on Thu Apr 16 06:27:34 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6