clean target: +missing hello.o
[gnokii.git] / common / gsm-bitmaps.c
1 /*
2
3   G N O K I I
4
5   A Linux/Unix toolset and driver for Nokia mobile phones.
6
7   Released under the terms of the GNU GPL, see file COPYING for more details.
8         
9   Functions for manipulating bitmaps
10   
11 */
12
13 #include "config.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <sys/stat.h>
20
21 #include "gsm-common.h"
22 #include "gsm-bitmaps.h"
23 #include "gsm-sms.h"
24 #include "gsm-coding.h"
25 #include "gsm-networks.h"
26
27 void GSM_SetPointBitmap(GSM_Bitmap *bmp, int x, int y)
28 {
29   int pixel;
30   if (bmp->type == GSM_StartupLogo || bmp->type == GSM_6210StartupLogo || bmp->type == GSM_7110StartupLogo)
31     bmp->bitmap[((y/8)*bmp->width)+x] |= 1 << (y%8);
32   if (bmp->type == GSM_OperatorLogo || bmp->type == GSM_7110OperatorLogo || bmp->type == GSM_CallerLogo) {
33     pixel=bmp->width*y + x;
34     bmp->bitmap[pixel/8] |= 1 << (7-(pixel%8));
35   }
36   if (bmp->type == GSM_PictureImage) bmp->bitmap[9*y + (x/8)] |= 1 << (7-(x%8));
37 }
38   
39 void GSM_ClearPointBitmap(GSM_Bitmap *bmp, int x, int y)
40 {
41   int pixel;
42   if (bmp->type == GSM_StartupLogo || bmp->type == GSM_6210StartupLogo || bmp->type == GSM_7110StartupLogo)
43     bmp->bitmap[((y/8)*bmp->width)+x] &= 255 - (1 << (y%8));
44   if (bmp->type == GSM_OperatorLogo || bmp->type == GSM_7110OperatorLogo || bmp->type == GSM_CallerLogo) {
45     pixel=bmp->width*y + x;
46     bmp->bitmap[pixel/8] &= ~(1 << (7-(pixel%8)));
47   }
48   if (bmp->type == GSM_PictureImage) bmp->bitmap[9*y + (x/8)] &= 255 - (1 << (7-(x%8)));
49 }
50
51 bool GSM_IsPointBitmap(GSM_Bitmap *bmp, int x, int y)
52 {
53   int i=0;
54   int pixel;
55
56   if (bmp->type == GSM_StartupLogo || bmp->type == GSM_6210StartupLogo || bmp->type == GSM_7110StartupLogo)
57     i=(bmp->bitmap[((y/8)*bmp->width) + x] & 1<<((y%8)));
58   if (bmp->type == GSM_OperatorLogo || bmp->type == GSM_7110OperatorLogo || bmp->type == GSM_CallerLogo) {
59     pixel=bmp->width*y + x;
60     i=(bmp->bitmap[pixel/8] & 1<<(7-(pixel%8)));
61   }
62   if (bmp->type == GSM_PictureImage) i=(bmp->bitmap[9*y + (x/8)] & 1<<(7-(x%8)));
63
64   if (i) return true; else return false;
65 }
66   
67 void GSM_ClearBitmap(GSM_Bitmap *bmp)
68 {
69   int i;
70   for (i=0;i<bmp->size;i++) bmp->bitmap[i]=0;
71 }
72
73 GSM_Error GSM_ReadBitmap(GSM_SMSMessage *message, GSM_Bitmap *bitmap)
74 {
75   int offset = 1;
76   unsigned char buffer[20];
77
78   switch (message->UDHType) {
79   case GSM_OpLogo:
80     EncodeUDHHeader(buffer, GSM_OperatorLogo);
81     if (message->Length!=133) return GE_UNKNOWN;
82     
83     bitmap->type = GSM_OperatorLogo;
84
85     DecodeNetworkCode(message->MessageText, bitmap->netcode);
86
87     offset = 4;
88     break;
89
90   case GSM_CallerIDLogo:
91     EncodeUDHHeader(buffer, GSM_CallerLogo);
92     if (message->Length!=130) return GE_UNKNOWN;
93     
94     bitmap->type=GSM_CallerLogo;
95
96     break;
97   default: /* error */
98     return GE_UNKNOWN;
99     break;
100   }
101   bitmap->width = message->MessageText[offset];
102   bitmap->height = message->MessageText[offset + 1];
103   
104   if (bitmap->width!=72 || bitmap->height!=14) return GE_INVALIDIMAGESIZE;
105   
106   bitmap->size = (bitmap->width * bitmap->height) / 8;
107   memcpy(bitmap->bitmap, message->MessageText + offset + 3, bitmap->size);
108
109 #ifdef DEBUG
110   fprintf(stdout, _("Bitmap from SMS: width %i, height %i\n"),bitmap->width,bitmap->height);
111 #endif
112
113   return GE_NONE;
114 }
115
116 void GSM_ResizeBitmap(GSM_Bitmap *bitmap, GSM_Bitmap_Types target)
117 {
118   GSM_Bitmap backup;
119   int x,y,width,height;
120   
121   backup=*bitmap;
122       
123   if (target==GSM_StartupLogo) {
124     bitmap->width=84;
125     bitmap->height=48;
126   }
127   if (target==GSM_7110StartupLogo) {
128     bitmap->width=96;
129     bitmap->height=65;
130   }
131   if (target==GSM_6210StartupLogo) {
132     bitmap->width=96;
133     bitmap->height=60;
134   }
135   if (target==GSM_OperatorLogo || target==GSM_CallerLogo) {
136     bitmap->width=72;
137     bitmap->height=14;
138   }
139   if (target==GSM_PictureImage ) {
140     bitmap->width=72;
141     bitmap->height=28;
142   }
143   if (target==GSM_7110OperatorLogo) {
144     bitmap->width=78;
145     bitmap->height=21;
146   }
147   bitmap->type=target;
148   bitmap->size=(bitmap->width*bitmap->height + 7)/8;
149   
150   width=backup.width;
151   if (bitmap->width<width) {
152     width=bitmap->width;
153 #ifdef DEBUG
154     fprintf(stdout,_("We lost some part of image - it's cut (width from %i to %i) !\n"),backup.width,width);
155 #endif /* DEBUG */
156   }
157   
158   height=backup.height;
159   if (bitmap->height<height) {
160     height=bitmap->height; 
161 #ifdef DEBUG
162     fprintf(stdout,_("We lost some part of image - it's cut (height from %i to %i) !\n"),backup.height,height);
163 #endif /* DEBUG */
164   }
165   
166   GSM_ClearBitmap(bitmap);
167   
168   for (y=0;y<height;y++) {
169     for (x=0;x<width;x++)
170       if (GSM_IsPointBitmap(&backup,x,y)) GSM_SetPointBitmap(bitmap,x,y);
171   }
172   
173 //GSM_PrintBitmap(&backup);
174 //GSM_PrintBitmap(bitmap);
175 }
176
177 void GSM_PrintBitmap(GSM_Bitmap *bitmap)
178 {
179   int x,y;
180
181   for (y=0;y<bitmap->height;y++) {
182     for (x=0;x<bitmap->width;x++) {
183       if (GSM_IsPointBitmap(bitmap,x,y)) {
184         fprintf(stdout, _("#"));
185       } else {
186         fprintf(stdout, _(" "));
187       }
188     }
189     fprintf(stdout, _("\n"));
190   }
191 }
192
193 int GSM_SaveBitmapToSMS(GSM_MultiSMSMessage *SMS, GSM_Bitmap *bitmap,
194                         bool ScreenSaver, bool UnicodeText)
195 {
196   char MessageBuffer[GSM_MAX_SMS_8_BIT_LENGTH*4];
197   int MessageLength=0;
198   GSM_UDH UDHType=GSM_NoUDH;
199   
200   switch (bitmap->type) {
201     case GSM_OperatorLogo:
202       UDHType=GSM_OpLogo;
203
204       EncodeNetworkCode(MessageBuffer, bitmap->netcode);
205       MessageLength=3;
206
207       /* Set the logo size */
208       MessageBuffer[MessageLength++] = 0x00;
209       MessageBuffer[MessageLength++] = bitmap->width;
210       MessageBuffer[MessageLength++] = bitmap->height;
211       MessageBuffer[MessageLength++] = 0x01;
212
213       memcpy(MessageBuffer+MessageLength,bitmap->bitmap,bitmap->size);
214       MessageLength=MessageLength+bitmap->size;
215
216       break;
217     case GSM_CallerLogo:
218       UDHType=GSM_CallerIDLogo;
219
220       /* Set the logo size */
221       MessageBuffer[MessageLength++] = 0x00;
222       MessageBuffer[MessageLength++] = bitmap->width;
223       MessageBuffer[MessageLength++] = bitmap->height;
224       MessageBuffer[MessageLength++] = 0x01;
225
226       memcpy(MessageBuffer+MessageLength,bitmap->bitmap,bitmap->size);
227       MessageLength=MessageLength+bitmap->size;
228
229       break;
230     case GSM_PictureImage:
231       UDHType=GSM_ProfileUDH;
232
233       MessageBuffer[MessageLength++]=0x30;     //SM version. Here 3.0
234
235       if (!ScreenSaver)
236         MessageBuffer[MessageLength++]=SM30_OTA; //ID for OTA bitmap
237       else
238         MessageBuffer[MessageLength++]=SM30_SCREENSAVER; //ID for screen saver
239
240       /* Length for picture part */
241       MessageBuffer[MessageLength++]=0x01;     //length hi
242       MessageBuffer[MessageLength++]=0x00;     //length lo
243
244       /* Set the logo size */
245       MessageBuffer[MessageLength++] = 0x00;
246       MessageBuffer[MessageLength++] = bitmap->width;
247       MessageBuffer[MessageLength++] = bitmap->height;
248       MessageBuffer[MessageLength++] = 0x01;
249
250       memcpy(MessageBuffer+MessageLength,bitmap->bitmap,bitmap->size);
251       MessageLength=MessageLength+bitmap->size;
252
253       if (strlen(bitmap->text)!=0) {
254         if (UnicodeText) {
255           MessageBuffer[MessageLength++]=SM30_UNICODETEXT; //ID for Unicode text
256
257           /* Length for text part */
258           MessageBuffer[MessageLength++]=0x00;             //length of text1
259           MessageBuffer[MessageLength++]=strlen(bitmap->text)*2;//length of text2
260
261           EncodeUnicode (MessageBuffer+MessageLength,bitmap->text,strlen(bitmap->text));
262           MessageLength=MessageLength+2*strlen(bitmap->text);
263         } else {
264           MessageBuffer[MessageLength++]=SM30_ISOTEXT;    //ID for ISO-8859-1 text
265
266           /* Length for text part */
267           MessageBuffer[MessageLength++]=0x00;            //length of text1
268           MessageBuffer[MessageLength++]=strlen(bitmap->text); //length of text2
269
270           memcpy(MessageBuffer+MessageLength,bitmap->text,strlen(bitmap->text));
271           MessageLength=MessageLength+strlen(bitmap->text);
272        }
273       }
274       break;
275     
276     default: /* error */
277       break;
278   }
279         
280   GSM_MakeMultiPartSMS2(SMS,MessageBuffer,MessageLength, UDHType, GSM_Coding_Default);
281
282   return 0;
283 }