~ubuntu-branches/ubuntu/karmic/asterisk/karmic

« back to all changes in this revision

Viewing changes to formats/format_jpeg.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2002-04-27 21:19:32 UTC
  • Revision ID: james.westby@ubuntu.com-20020427211932-kqaertc4bg7ss5mc
Tags: upstream-0.1.11
ImportĀ upstreamĀ versionĀ 0.1.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Asterisk -- A telephony toolkit for Linux.
 
3
 *
 
4
 * JPEG File format support.
 
5
 * 
 
6
 * Copyright (C) 1999, Mark Spencer
 
7
 *
 
8
 * Mark Spencer <markster@linux-support.net>
 
9
 *
 
10
 * This program is free software, distributed under the terms of
 
11
 * the GNU General Public License
 
12
 */
 
13
 
 
14
#include <asterisk/channel.h>
 
15
#include <asterisk/file.h>
 
16
#include <asterisk/logger.h>
 
17
#include <asterisk/sched.h>
 
18
#include <asterisk/module.h>
 
19
#include <asterisk/image.h>
 
20
#include <arpa/inet.h>
 
21
#include <stdlib.h>
 
22
#include <sys/time.h>
 
23
#include <stdio.h>
 
24
#include <unistd.h>
 
25
#include <errno.h>
 
26
#include <string.h>
 
27
#include <pthread.h>
 
28
#include <endian.h>
 
29
 
 
30
 
 
31
 
 
32
static char *desc = "JPEG (Joint Picture Experts Group) Image Format";
 
33
 
 
34
 
 
35
static struct ast_frame *jpeg_read_image(int fd, int len)
 
36
{
 
37
        struct ast_frame fr;
 
38
        int res;
 
39
        char buf[65536];
 
40
        if (len > sizeof(buf)) {
 
41
                ast_log(LOG_WARNING, "JPEG image too large to read\n");
 
42
                return NULL;
 
43
        }
 
44
        res = read(fd, buf, len);
 
45
        if (res < len) {
 
46
                ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
 
47
        }
 
48
        memset(&fr, 0, sizeof(fr));
 
49
        fr.frametype = AST_FRAME_IMAGE;
 
50
        fr.subclass = AST_FORMAT_JPEG;
 
51
        fr.data = buf;
 
52
        fr.src = "JPEG Read";
 
53
        fr.datalen = len;
 
54
        return ast_frisolate(&fr);
 
55
}
 
56
 
 
57
static int jpeg_identify(int fd)
 
58
{
 
59
        char buf[10];
 
60
        int res;
 
61
        res = read(fd, buf, sizeof(buf));
 
62
        if (res < sizeof(buf))
 
63
                return 0;
 
64
        if (memcmp(buf + 6, "JFIF", 4))
 
65
                return 0;
 
66
        return 1;
 
67
}
 
68
 
 
69
static int jpeg_write_image(int fd, struct ast_frame *fr)
 
70
{
 
71
        int res=0;
 
72
        if (fr->frametype != AST_FRAME_IMAGE) {
 
73
                ast_log(LOG_WARNING, "Not an image\n");
 
74
                return -1;
 
75
        }
 
76
        if (fr->subclass != AST_FORMAT_JPEG) {
 
77
                ast_log(LOG_WARNING, "Not a jpeg image\n");
 
78
                return -1;
 
79
        }
 
80
        if (fr->datalen) {
 
81
                res = write(fd, fr->data, fr->datalen);
 
82
                if (res != fr->datalen) {
 
83
                        ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen);
 
84
                        return -1;
 
85
                }
 
86
        }
 
87
        return res;
 
88
}
 
89
 
 
90
static struct ast_imager jpeg_format = {
 
91
        "jpg",
 
92
        "JPEG (Joint Picture Experts Group)",
 
93
        "jpg|jpeg",
 
94
        AST_FORMAT_JPEG,
 
95
        jpeg_read_image,
 
96
        jpeg_identify,
 
97
        jpeg_write_image,
 
98
};
 
99
 
 
100
int load_module()
 
101
{
 
102
        return ast_image_register(&jpeg_format);
 
103
}
 
104
 
 
105
int unload_module()
 
106
{
 
107
        ast_image_unregister(&jpeg_format);
 
108
        return 0;
 
109
}       
 
110
 
 
111
int usecount()
 
112
{
 
113
        /* We never really have any users */
 
114
        return 0;
 
115
}
 
116
 
 
117
char *description()
 
118
{
 
119
        return desc;
 
120
}
 
121
 
 
122
 
 
123
char *key()
 
124
{
 
125
        return ASTERISK_GPL_KEY;
 
126
}