Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
OTAbitmap.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 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 "OTAbitmap.h"
64 
65 
66 
67 /* create empty OTAbitmap */
68 
70 {
71  OTAbitmap *new;
72 
73  new = gw_malloc(sizeof(OTAbitmap));
74  memset(new, 0, sizeof(OTAbitmap));
75  return new;
76 }
77 
79 {
80  gw_free(pic->ext_fields);
81  gw_free(pic->main_image);
82  if (pic->animated_image) {
83  int i;
84  for(i=0; i < pic->animimg_count; i++)
85  gw_free(pic->animated_image[i]);
86  gw_free(pic->animated_image);
87  }
88  gw_free(pic);
89 }
90 
91 OTAbitmap *OTAbitmap_create(int width, int height, int depth,
92  Octet *data, int flags)
93 {
94  OTAbitmap *new;
95  int i, j, siz, osiz;
96  Octet val;
97 
98  new = OTAbitmap_create_empty();
99 
100  if (width > 255 || height > 255)
101  new->infofield = 0x10; /* set bit */
102  else
103  new->infofield = 0x00;
104 
105  new->width = width;
106  new->height = height;
107 
108  siz = (width * height + 7)/8;
109 
110  new->main_image = gw_malloc(siz);
111  osiz = (width+7)/8 * height;
112  for(i=j=0; i<osiz; i++, j+=8) {
113  val = data[i];
114  if (flags & REVERSE) val = reverse_octet(val);
115  if (flags & NEGATIVE) val = ~val;
116 
117  if (i > 0 && i % ((width+7)/8) == 0 && width % 8 > 0)
118  j -= 8 + width % 8;
119 
120  if (j % 8 == 0) {
121  new->main_image[j/8] = val;
122  }
123  else {
124  new->main_image[j/8] |= val >> (j % 8);
125  new->main_image[j/8 + 1] = val << (8 - j % 8);
126  }
127  }
128  /* no palette nor animated images, yet */
129 
130  return new;
131 }
132 
133 
134 
135 /* create Octet stream from given OTAbitmap */
136 
138 {
139  Octet tmp_header[10];
140  int hdr_len;
141  int pic_size;
142 
143  if (pic->infofield & 0x10) {
144  sprintf(tmp_header, "%c%c%c%c%c%c", pic->infofield, pic->width/256,
145  pic->width%256, pic->height/256, pic->height%256, pic->depth);
146  hdr_len = 6;
147  } else {
148  sprintf(tmp_header, "%c%c%c%c", pic->infofield,
149  pic->width, pic->height, pic->depth);
150  hdr_len = 4;
151  }
152 
153  pic_size = (pic->width * pic->height + 7)/8;
154 
155  *stream = gw_malloc(pic_size+pic_size);
156  memcpy(*stream, tmp_header, hdr_len);
157  memcpy(*stream + hdr_len, pic->main_image, pic_size);
158 
159  debug("util", 0, "picture %d x %d, stream length %d",
160  pic->width, pic->height, hdr_len + pic_size);
161 
162  return (hdr_len + pic_size);
163 }
Octet * ext_fields
Definition: OTAbitmap.h:73
int animimg_count
Definition: OTAbitmap.h:80
int height
Definition: OTAbitmap.h:76
#define NEGATIVE
Definition: OTAbitmap.h:94
int depth
Definition: OTAbitmap.h:77
Octet * main_image
Definition: OTAbitmap.h:78
Octet infofield
Definition: OTAbitmap.h:72
#define REVERSE
Definition: OTAbitmap.h:95
int width
Definition: OTAbitmap.h:75
int OTAbitmap_create_stream(OTAbitmap *pic, Octet **stream)
Definition: OTAbitmap.c:137
Octet reverse_octet(Octet source)
Definition: utils.c:511
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
OTAbitmap * OTAbitmap_create_empty(void)
Definition: OTAbitmap.c:69
void OTAbitmap_delete(OTAbitmap *pic)
Definition: OTAbitmap.c:78
OTAbitmap * OTAbitmap_create(int width, int height, int depth, Octet *data, int flags)
Definition: OTAbitmap.c:91
unsigned char Octet
Definition: utils.h:75
Octet ** animated_image
Definition: OTAbitmap.h:79
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.