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.
21 * \brief JPEG File format support.
23 * \arg File name extension: jpeg, jpg
29 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
31 #include "asterisk/mod_format.h"
32 #include "asterisk/module.h"
33 #include "asterisk/image.h"
34 #include "asterisk/endian.h"
36 static struct ast_frame
*jpeg_read_image(int fd
, int len
)
41 if (len
> sizeof(buf
) || len
< 0) {
42 ast_log(LOG_WARNING
, "JPEG image too large to read\n");
45 res
= read(fd
, buf
, len
);
47 ast_log(LOG_WARNING
, "Only read %d of %d bytes: %s\n", res
, len
, strerror(errno
));
49 memset(&fr
, 0, sizeof(fr
));
50 fr
.frametype
= AST_FRAME_IMAGE
;
51 fr
.subclass
= AST_FORMAT_JPEG
;
55 return ast_frisolate(&fr
);
58 static int jpeg_identify(int fd
)
62 res
= read(fd
, buf
, sizeof(buf
));
63 if (res
< sizeof(buf
))
65 if (memcmp(buf
+ 6, "JFIF", 4))
70 static int jpeg_write_image(int fd
, struct ast_frame
*fr
)
73 if (fr
->frametype
!= AST_FRAME_IMAGE
) {
74 ast_log(LOG_WARNING
, "Not an image\n");
77 if (fr
->subclass
!= AST_FORMAT_JPEG
) {
78 ast_log(LOG_WARNING
, "Not a jpeg image\n");
82 res
= write(fd
, fr
->data
.ptr
, fr
->datalen
);
83 if (res
!= fr
->datalen
) {
84 ast_log(LOG_WARNING
, "Only wrote %d of %d bytes: %s\n", res
, fr
->datalen
, strerror(errno
));
91 static struct ast_imager jpeg_format
= {
93 .desc
= "JPEG (Joint Picture Experts Group)",
95 .format
= AST_FORMAT_JPEG
,
96 .read_image
= jpeg_read_image
,
97 .identify
= jpeg_identify
,
98 .write_image
= jpeg_write_image
,
101 static int load_module(void)
103 if (ast_image_register(&jpeg_format
))
104 return AST_MODULE_LOAD_FAILURE
;
105 return AST_MODULE_LOAD_SUCCESS
;
108 static int unload_module(void)
110 ast_image_unregister(&jpeg_format
);
115 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "JPEG (Joint Picture Experts Group) Image Format");