Add dummy-select from http://bugs.digium.com/13132
[asterisk-bristuff.git] / formats / format_jpeg.c
blobedef171d2602b848bc50ad576b6082e1a6b0541e
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief JPEG File format support.
23 * \arg File name extension: jpeg, jpg
24 * \ingroup formats
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <string.h>
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/image.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/endian.h"
50 static struct ast_frame *jpeg_read_image(int fd, int len)
52 struct ast_frame fr;
53 int res;
54 char buf[65536];
55 if (len > sizeof(buf) || len < 0) {
56 ast_log(LOG_WARNING, "JPEG image too large to read\n");
57 return NULL;
59 res = read(fd, buf, len);
60 if (res < len) {
61 ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
63 memset(&fr, 0, sizeof(fr));
64 fr.frametype = AST_FRAME_IMAGE;
65 fr.subclass = AST_FORMAT_JPEG;
66 fr.data = buf;
67 fr.src = "JPEG Read";
68 fr.datalen = len;
69 return ast_frisolate(&fr);
72 static int jpeg_identify(int fd)
74 char buf[10];
75 int res;
76 res = read(fd, buf, sizeof(buf));
77 if (res < sizeof(buf))
78 return 0;
79 if (memcmp(buf + 6, "JFIF", 4))
80 return 0;
81 return 1;
84 static int jpeg_write_image(int fd, struct ast_frame *fr)
86 int res=0;
87 if (fr->frametype != AST_FRAME_IMAGE) {
88 ast_log(LOG_WARNING, "Not an image\n");
89 return -1;
91 if (fr->subclass != AST_FORMAT_JPEG) {
92 ast_log(LOG_WARNING, "Not a jpeg image\n");
93 return -1;
95 if (fr->datalen) {
96 res = write(fd, fr->data, fr->datalen);
97 if (res != fr->datalen) {
98 ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
99 return -1;
102 return res;
105 static struct ast_imager jpeg_format = {
106 "jpg",
107 "JPEG (Joint Picture Experts Group)",
108 "jpg|jpeg",
109 AST_FORMAT_JPEG,
110 jpeg_read_image,
111 jpeg_identify,
112 jpeg_write_image,
115 static int load_module(void)
117 return ast_image_register(&jpeg_format);
120 static int unload_module(void)
122 ast_image_unregister(&jpeg_format);
124 return 0;
127 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "JPEG (Joint Picture Experts Group) Image Format");