Kannel: Open Source WAP and SMS gateway  svn-r5335
wbmp.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <errno.h>
61 
62 #include "gwlib/gwlib.h"
63 #include "wbmp.h"
64 
65 
66 
67 /* create empty WBMP */
68 
70 {
71  WBMP *new;
72 
73  new = gw_malloc(sizeof(WBMP));
74  memset(new, 0, sizeof(WBMP));
75  return new;
76 }
77 
78 /* delete a WBMP, freeing everything */
79 
80 void wbmp_delete(WBMP *pic)
81 {
82  gw_free(pic->ext_header_field);
83  gw_free(pic->main_image);
84  if (pic->animated_image) {
85  int i;
86  for(i=0; i < pic->animimg_count; i++)
87  gw_free(pic->animated_image[i]);
88  gw_free(pic->animated_image);
89  }
90  gw_free(pic);
91 }
92 
93 
94 WBMP *wbmp_create(int type, int width, int height, Octet *data, int flags)
95 {
96  WBMP *new;
97  int i, siz;
98  Octet val;
99 
100  new = wbmp_create_empty();
101 
102  new->type_field = type;
103  if (type == 0) {
104  new->fix_header_field = 0x00;
105  } else {
106  error(0, "WBMP type %d not supported", type);
107  return NULL;
108  }
109  new->width = width;
110  new->height = height;
111  siz = (width+7)/8 * height;
112 
113  new->main_image = gw_malloc(siz);
114  for(i=0; i < siz; i++) {
115  if (flags & REVERSE) val = reverse_octet(data[i]);
116  else val = data[i];
117  if (flags & NEGATIVE) val = ~val;
118  new->main_image[i] = val;
119  }
120  return new;
121 }
122 
123 
124 
125 /* create Octet stream from given WBMP */
126 
127 int wbmp_create_stream(WBMP *pic, Octet **stream)
128 {
129  Octet tmp_w[30], tmp_h[30];
130  int wl, hl, pic_size;
131 
132  wl = write_variable_value(pic->width, tmp_w);
133  hl = write_variable_value(pic->height, tmp_h);
134 
135  pic_size = ((pic->width+7)/8) * pic->height;
136 
137  if (pic->type_field != 0) {
138  error(0, "Unknown WBMP type %d, cannot convert", pic->type_field);
139  return -1;
140  }
141  *stream = gw_malloc(2+wl+hl+pic_size);
142  sprintf(*stream, "%c%c", 0x00, 0x00);
143  memcpy(*stream+2, tmp_w, wl);
144  memcpy(*stream+2+wl, tmp_h, hl);
145  memcpy(*stream+2+wl+hl, pic->main_image, pic_size);
146 
147  debug("util", 0, "picture %d x %d, stream length %d",
148  pic->width, pic->height, 2+wl+hl+pic_size);
149 
150  return (2+wl+hl+pic_size);
151 }
WBMP * wbmp_create(int type, int width, int height, Octet *data, int flags)
Definition: wbmp.c:94
void error(int err, const char *fmt,...)
Definition: log.c:648
void wbmp_delete(WBMP *pic)
Definition: wbmp.c:80
#define NEGATIVE
Definition: OTAbitmap.h:94
MultibyteInt height
Definition: wbmp.h:95
int wbmp_create_stream(WBMP *pic, Octet **stream)
Definition: wbmp.c:127
Octet * main_image
Definition: wbmp.h:96
ExtParam * ext_header_field
Definition: wbmp.h:92
#define REVERSE
Definition: OTAbitmap.h:95
int type
Definition: smsc_cimd2.c:215
MultibyteInt width
Definition: wbmp.h:94
Octet reverse_octet(Octet source)
Definition: utils.c:511
Definition: wbmp.h:85
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
int write_variable_value(MultibyteInt value, Octet *dest)
Definition: utils.c:490
MultibyteInt type_field
Definition: wbmp.h:86
WBMP * wbmp_create_empty(void)
Definition: wbmp.c:69
int animimg_count
Definition: wbmp.h:98
unsigned char Octet
Definition: utils.h:75
Octet ** animated_image
Definition: wbmp.h:97
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.