Wed Oct 28 11:51:03 2009

Asterisk developer's documentation


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 #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");

Generated on Wed Oct 28 11:51:04 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6