source: subversion/applications/lib/libimg/ImgData.cpp @ 34650

Last change on this file since 34650 was 11688, checked in by nienhueser, 11 years ago

compile.

File size: 4.4 KB
Line 
1/*
2    Copyright (C) 2007 Robert Hart, rob@bathterror.free-online.co.uk
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU Lesser General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
17
18 */
19#include "Img.h"
20
21#include <cstring>
22
23ImgData::ImgData(){
24  timeval tv;
25  int retval;
26
27  blocksize = 512;
28  nblocks = 3;
29  data = new char[nblocks * blocksize];
30  xorbyte = 0; 
31  retval = gettimeofday(&tv, NULL);
32  SetUpdateTime(tv.tv_sec);
33
34}
35
36void ImgData::SetUpdateTime(tm timeval){
37  updatetime = timeval;
38}
39
40void ImgData::SetUpdateTime(time_t timeval){
41  gmtime_r(&timeval, &updatetime);
42}
43
44void ImgData::WriteFile(std::ofstream &out){
45  CreateHeader();
46  out.write(data,blocksize * nblocks);
47}
48
49
50void ImgData::SetByte(int add, int val){
51  data[add] = (char)val^xorbyte;
52}
53
54int ImgData::DecodeByte(int add)
55{
56  return data[add]^xorbyte;
57}
58
59void ImgData::SetByte(int add, int val, int n){
60  for(int i=0;i<n;i++){
61    data[add+i] = (char)val^xorbyte;
62  }
63}
64
65void ImgData::SetShort(int add, long val){
66  data[add] = (char)val^xorbyte;
67  data[add+1] = (char)(val >> 8)^xorbyte;
68}
69
70long ImgData::DecodeShort(int add){
71  long v1, v2;
72  v1 = ((unsigned char)(data[add+1])^xorbyte)<<8;
73  v2 = (long)((unsigned char)(data[add])^xorbyte);
74  return v1 + v2;
75}
76
77void ImgData::SetLong(int add, long val){
78  data[add] = (char)val^xorbyte;
79  data[add+1] = (char)(val >> 8)^xorbyte;
80  data[add+2] = (char)(val >> 16)^xorbyte;
81  data[add+3] = (char)(val >> 24)^xorbyte;
82}
83
84void ImgData::SetInt24(int add, long val){
85  data[add] = (char)val^xorbyte;
86  data[add+1] = (char)(val >> 8)^xorbyte;
87  data[add+2] = (char)(val >> 16)^xorbyte;
88}
89
90long ImgData::DecodeLong(int add)
91{
92  long v1, v2, v3, v4;
93  v4 = ((unsigned char)(data[add+3])^xorbyte)<<24;
94  v3 = ((unsigned char)(data[add+2])^xorbyte)<<16;
95  v2 = ((unsigned char)(data[add+1])^xorbyte)<<8;
96  v1 = (long)((unsigned char)(data[add])^xorbyte);
97  return v1 + v2 + v3 + v4;
98}
99
100long ImgData::DecodeInt24(int add)
101{
102  long v1, v2, v3;
103  v3 = ((unsigned char)(data[add+2])^xorbyte)<<16;
104  v2 = ((unsigned char)(data[add+1])^xorbyte)<<8;
105  v1 = (long)((unsigned char)(data[add])^xorbyte);
106  return v1 + v2 + v3;
107}
108
109void ImgData::SetString(int add, std::string str){
110  //  cout << "SetString: " << str << endl;
111  //  cout << "  is " << str.length() << "bytes long\n";
112  for(int i=0;i<str.length();i++){
113    data[add+i]=(char)str.at(i)^xorbyte;
114  }
115}
116
117void ImgData::SetString(int add, char* str){
118  for(int i=0;i<strlen(str);i++){
119    data[add+i]=(char)str[i]^xorbyte;
120  }
121}
122
123void ImgData::DecodeString(int add, int length, std::string &decoded){
124  for(int i=0;i<length;i++) {
125    char c[2] = { data[add+i]^xorbyte, 0 };
126    //    cout << "appending " << c << endl;
127    decoded.append( c );
128  }
129}
130
131void ImgData::SetBlockList(int add, int first, int last){
132  int i;
133  for(i=0; i<(last-first); i++){
134    SetShort(add+2*i, first+i);
135  }
136  for(; i<240; i++){
137    SetShort(add+2*i, 65535);
138  }
139}
140
141int ImgData::DecodeBlockList(int add, int &first, int &last){
142  int i, j, k;
143  first = j = DecodeShort(add);
144  cout << j;
145  for(i=1; i<240; i++){
146    k = DecodeShort(add+2*i);
147    //   cout << ", " << k;
148    if(k!=j+1)
149      break;
150    j=k;
151  }
152  //cout << endl;
153  return last = j;
154}
155
156//FIXME: need to deal with negative values.
157double ImgData::DecodeCoord(int add)
158{
159    long v1, v2, v3;
160  v3 = ((unsigned char)(data[add+2])^xorbyte)<<16;
161  v2 = ((unsigned char)(data[add+1])^xorbyte)<<8;
162  v1 = (long)((unsigned char)(data[add])^xorbyte);
163  return MapToCoord(v1 + v2 + v3);
164}
165
166void ImgData::SetCoord(int add, double coord)
167{
168  long val = CoordToMap(coord);
169  data[add] = (char)val^xorbyte;
170  data[add+1] = (char)(val >> 8)^xorbyte;
171  data[add+2] = (char)(val >> 16)^xorbyte;
172}
173
174int ImgData::CoordToMap(double coord)
175{
176  return (int)(coord/360.0*(1<<24));
177}
178
179double ImgData::MapToCoord(int map)
180{
181  return (double)map*360.0/(1<<24);
182}
Note: See TracBrowser for help on using the repository browser.