Include "rpm-qa" file.
[nethome.git] / src / pbmtopicturemsg.pl
1 #! /usr/bin/perl
2 #
3 # $Id$
4
5 use strict;
6 use warnings;
7
8 undef $/;
9 my $data=<>;
10 $data=~s/^\s*#.*\n//m;
11 my($format,$width,$height)=($data=~/^(P[14])\n(\d+) (\d+)\n/);
12 $data=$';
13 die "Invalid format" if !$format || !$width || !$height;
14 die "Picture too big; width=$width < 0xFF && height=$height < 0xFF" if $width>0xFF || $height>0xFF;
15 if ($format eq "P1") {
16         $data=~tr/01//cd;
17         }
18 else {
19         $data=unpack("b*",$data);
20         my $width8=int(($width+7)/8)*8;
21         for my $y (0..$height-1) {
22                 # cut end-of-line gap
23                 substr($data,$width*$y+$width,$width8-$width)="";
24                 }
25         }
26 die "Bad data length ".length($data) if length($data)!=$width*$height;
27
28 my @pictureudh=(
29                 0x05,   # IEI
30                 0x04,   # IEDL
31                 0x15,0x8A,      # dest port (ring tones)
32                 0x15,0x8A);     # src port (unused)
33
34 my $multihdr_uniq=int(rand(0x100));
35 sub multihdr
36 {
37 my($num,$total)=@_;
38
39         return (
40                 0x00,   # IEI
41                 0x03,   # IEDL
42                 $multihdr_uniq, # unique serial ID
43                 $total, # total messages
44                 $num+1);        # message number (# from 1)
45 }
46
47 sub udh
48 {
49 my(@bytes)=@_;
50
51         return pack "C*",scalar(@bytes),@bytes;
52 }
53
54
55 my $picturebyteslen=int(($width*$height+7)/8)+4;        # +4 is for some part of the header
56 my $picturedata=unpack("b*",pack("C*",
57                                 0x30,   # version string '0'
58                                 0x02,   # item=OTA bitmap
59                                 ($picturebyteslen>>8)&0xFF,     # picture size in bytes incl. header; HIGH
60                                 ($picturebyteslen>>0)&0xFF,     # picture size in bytes incl. header; LOW
61                                 0x00,   # animation pictures - 0=static picture
62                                 $width,$height, # picture size in pixels
63                                 0x01,   # picture depth - B/W
64                                 ))
65                 .$data;
66
67 my @out=udh(@pictureudh).pack("b*",$picturedata);
68 if (length($out[0])>140) {
69         my $datalen=int((length($picturedata)+7)/8);
70         my $hdrlen=length(udh(@pictureudh,multihdr(8,8)));
71         my $dataspace=(140-$hdrlen);
72         my $total=int(($datalen+$dataspace-1)/$dataspace);
73         die "Too big: total=$total <=0xFF !" if $total>0xFF;
74         @out=map({
75                 udh(@pictureudh,multihdr($_,$total)).pack("b*",substr($picturedata,$dataspace*8*$_,$dataspace*8));
76                 } (0..$total-1));
77         }
78 print map({ uc(unpack("H*",$_))."\n"; } @out);