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

Last change on this file since 2913 was 2092, checked in by enxrah, 13 years ago

Nick didn't write this. I did :-)

File size: 4.3 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
21ImgData::ImgData(){
22  timeval tv;
23  int retval;
24
25  blocksize = 512;
26  nblocks = 3;
27  data = new char[nblocks * blocksize];
28  xorbyte = 0; 
29  retval = gettimeofday(&tv, NULL);
30  SetUpdateTime(tv.tv_sec);
31
32}
33
34void ImgData::SetUpdateTime(tm timeval){
35  updatetime = timeval;
36}
37
38void ImgData::SetUpdateTime(time_t timeval){
39  gmtime_r(&timeval, &updatetime);
40}
41
42void ImgData::WriteFile(std::ofstream &out){
43  CreateHeader();
44  out.write(data,blocksize * nblocks);
45}
46
47
48void ImgData::SetByte(int add, int val){
49  data[add] = (char)val^xorbyte;
50}
51
52int ImgData::DecodeByte(int add)
53{
54  return data[add]^xorbyte;
55}
56
57void ImgData::SetByte(int add, int val, int n){
58  for(int i=0;i<n;i++){
59    data[add+i] = (char)val^xorbyte;
60  }
61}
62
63void ImgData::SetShort(int add, long val){
64  data[add] = (char)val^xorbyte;
65  data[add+1] = (char)(val >> 8)^xorbyte;
66}
67
68long ImgData::DecodeShort(int add){
69  long v1, v2;
70  v1 = ((unsigned char)(data[add+1])^xorbyte)<<8;
71  v2 = (long)((unsigned char)(data[add])^xorbyte);
72  return v1 + v2;
73}
74
75void ImgData::SetLong(int add, long val){
76  data[add] = (char)val^xorbyte;
77  data[add+1] = (char)(val >> 8)^xorbyte;
78  data[add+2] = (char)(val >> 16)^xorbyte;
79  data[add+3] = (char)(val >> 24)^xorbyte;
80}
81
82void ImgData::SetInt24(int add, long val){
83  data[add] = (char)val^xorbyte;
84  data[add+1] = (char)(val >> 8)^xorbyte;
85  data[add+2] = (char)(val >> 16)^xorbyte;
86}
87
88long ImgData::DecodeLong(int add)
89{
90  long v1, v2, v3, v4;
91  v4 = ((unsigned char)(data[add+3])^xorbyte)<<24;
92  v3 = ((unsigned char)(data[add+2])^xorbyte)<<16;
93  v2 = ((unsigned char)(data[add+1])^xorbyte)<<8;
94  v1 = (long)((unsigned char)(data[add])^xorbyte);
95  return v1 + v2 + v3 + v4;
96}
97
98long ImgData::DecodeInt24(int add)
99{
100  long v1, v2, v3;
101  v3 = ((unsigned char)(data[add+2])^xorbyte)<<16;
102  v2 = ((unsigned char)(data[add+1])^xorbyte)<<8;
103  v1 = (long)((unsigned char)(data[add])^xorbyte);
104  return v1 + v2 + v3;
105}
106
107void ImgData::SetString(int add, std::string str){
108  //  cout << "SetString: " << str << endl;
109  //  cout << "  is " << str.length() << "bytes long\n";
110  for(int i=0;i<str.length();i++){
111    data[add+i]=(char)str.at(i)^xorbyte;
112  }
113}
114
115void ImgData::SetString(int add, char* str){
116  for(int i=0;i<strlen(str);i++){
117    data[add+i]=(char)str[i]^xorbyte;
118  }
119}
120
121void ImgData::DecodeString(int add, int length, std::string &decoded){
122  for(int i=0;i<length;i++) {
123    char c[2] = { data[add+i]^xorbyte, 0 };
124    //    cout << "appending " << c << endl;
125    decoded.append( c );
126  }
127}
128
129void ImgData::SetBlockList(int add, int first, int last){
130  int i;
131  for(i=0; i<(last-first); i++){
132    SetShort(add+2*i, first+i);
133  }
134  for(; i<240; i++){
135    SetShort(add+2*i, 65535);
136  }
137}
138
139int ImgData::DecodeBlockList(int add, int &first, int &last){
140  int i, j, k;
141  first = j = DecodeShort(add);
142  cout << j;
143  for(i=1; i<240; i++){
144    k = DecodeShort(add+2*i);
145    //   cout << ", " << k;
146    if(k!=j+1)
147      break;
148    j=k;
149  }
150  //cout << endl;
151  return last = j;
152}
153
154//FIXME: need to deal with negative values.
155double ImgData::DecodeCoord(int add)
156{
157    long v1, v2, v3;
158  v3 = ((unsigned char)(data[add+2])^xorbyte)<<16;
159  v2 = ((unsigned char)(data[add+1])^xorbyte)<<8;
160  v1 = (long)((unsigned char)(data[add])^xorbyte);
161  return MapToCoord(v1 + v2 + v3);
162}
163
164void ImgData::SetCoord(int add, double coord)
165{
166  long val = CoordToMap(coord);
167  data[add] = (char)val^xorbyte;
168  data[add+1] = (char)(val >> 8)^xorbyte;
169  data[add+2] = (char)(val >> 16)^xorbyte;
170}
171
172int ImgData::CoordToMap(double coord)
173{
174  return (int)(coord/360.0*(1<<24));
175}
176
177double ImgData::MapToCoord(int map)
178{
179  return (double)map*360.0/(1<<24);
180}
Note: See TracBrowser for help on using the repository browser.