summaryrefslogtreecommitdiffstats
path: root/conversion/test/test_convertor.cc
blob: 930527843d1ce0bfbd130efb1bd6684fb18a6018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <iostream>
#include <stdio.h>
#include <string.h>
#include "conversion/convertor.hh"
#include "conversion/tiff.hh"
#include "raslib/rminit.hh"
#include "rasodmg/gmarray.hh"

#ifdef EARLY_TEMPLATE
#define __EXECUTABLE__
#include "raslib/template_inst.hh"
#endif

// define if you want to use r_Type variables to set the
// array type.
//#define TEST_CONV_USE_RTYPES


void ConvertToTIFFCore(r_Conv_TIFF *tiff, const char *save, const char *params)
{
  r_convDesc desc;
  FILE *tfile;
  long size;

  try
  {
    // desc will become invalid when the TIFF object is deleted.
    // The same applies to all data allocated by the TIFF object,
    // e.g. the buffer holding the resulting TIFF.
    desc = tiff->convertTo(params);

    size = (long)(desc.destInterv[0].high() - desc.destInterv[0].low() + 1);
    if ((tfile = fopen(save, "wb")) == NULL)
    {
      cerr << "Error opening file " << save << endl;
    }
    else
    {
      fwrite((void*)(desc.dest), (size_t)1, (size_t)size, tfile);
      fclose(tfile);
    }
    cout << "test_convertor: r_Conv_TIFF::convertTo successful. "
	 << "size = " << size << ", type = ";
    desc.destType->print_status(cout); cout << endl;
    // This is the job of the client!
    free(desc.dest);
    delete desc.destType;
  }
  catch (r_Error &err)
  {
    cout << "An error occurred: " << err.what() << endl;
  }
}


void ConvertToTIFF(char *data, r_Minterval &iv, r_Type *type, const char *save, const char *params)
{
  r_Conv_TIFF *tiff;

  cout << "test_convertor (r_Type): ";
  if (type)
  {
    cout << "Base Type is "; type->print_status(cout); cout << endl;
  }
  else
  {
    cout << "Base type not defined!" << endl;
  }

  tiff = new r_Conv_TIFF(data, iv, type);
  ConvertToTIFFCore(tiff, save, params);
  delete tiff;
}


void ConvertToTIFF(char *data, r_Minterval &iv, int type, const char *save, const char *params)
{
  r_Conv_TIFF *tiff;

  cout << "test_convertor (int):" << endl;

  tiff = new r_Conv_TIFF(data, iv, type);
  ConvertToTIFFCore(tiff, save, params);
  delete tiff;
}


void ConvertFromTIFF(char *name, const char *params, const char *save_as = NULL)
{
  FILE *fp;
  long size;
  r_convDesc desc;
  r_Conv_TIFF *tiff;
  char *data;
  r_Minterval iv;

  fp = fopen(name, "r");
  fseek(fp, 0, SEEK_END);
  size = ftell(fp);
  data = new char[size];
  fseek(fp, 0, SEEK_SET);
  fread((void*)data, (size_t)1, (size_t)size, fp);
  fclose(fp);

  // Set interval
  iv = r_Minterval(1); iv << r_Sinterval(r_Range(0), r_Range(size - 1));

  // Base type is coded in TIFF
  tiff = new r_Conv_TIFF(data, iv, (int)0);

  desc = tiff->convertFrom();

  cout << "test_convertor: r_Conv_TIFF::convertFrom successful."
       << " domain = " << desc.destInterv << ", type = ";
  desc.destType->print_status(cout); cout << endl;

  delete [] data;

  // save file again as TIFF to check convertFrom validity?
  if (save_as != NULL)
  {
    cout << "test_convertor: Saving data again as <" << save_as << ">..." << endl;
    ConvertToTIFF(desc.dest, desc.destInterv, desc.baseType, save_as, params);
  }

  // The order in which objects are deleted is important!
  free(desc.dest);
  delete desc.destType;

  // Delete this last. From then on desc is invalid.
  delete tiff;
}



// Flag bits for main()
#define CONVERTOR_WRITE_BACK	1


// Calling convention: test_convertor [-x # -y # -c <compression> -v -h]
// Where -x: width, -y: height, -c: compression, -v: write again as TIFF after reading
// -h: Help on usage
// width, height default to 200, 100
int main (int argc, char *argv[])
{
  r_GMarray dummyArray; // need this for linking on Linux, don't know why
  char *data, *lineBase, *line;
  int width = 200, height = 100;
  unsigned int flags = 0;
  int i, j;
  char params[256];
  const char *paramptr = NULL;
#ifdef TEST_CONV_USE_RTYPES
  r_Type *type;
#endif
  struct nametable {char *write, *verify;} tiffnames[] = {
    {"grey.tif", "grey2.tif"},
    {"bool.tif", "bool2.tif"},
    {"rgb.tif",  "rgb2.tif"}
  };

  i = 1;
  while (i < argc)
  {
    if (argv[i][0] == '-')
    {
      switch (argv[i][1])
      {
        case 'x': width = atoi(argv[++i]); break;
        case 'y': height = atoi(argv[++i]); break;
        case 'c':
	  sprintf(params, "comptype=%s", argv[++i]);
	  paramptr = params;
	  break;
        case 'v': flags |= CONVERTOR_WRITE_BACK; break;
        case 'h': 
	  cout << "Usage: " << argv[0] << "[-w # -h # -c <string> -v -h]" << endl;
	  cout << "\t-x #: width" << endl;
	  cout << "\t-y #: height" << endl;
	  cout << "\t-c <string>: compression (see r_Conv_TIFF::compNames)" << endl;
	  cout << "\t-v: write the TIFF files just read back as TIFF files for verifying" << endl;
	  cout << "\t-h: this help" << endl;
	  exit(0);
        default: cout << "Bad switch -" << argv[i][1] << endl; break;
      }
    }
    i++;
  }

  cout << "test_convertor: use images of size " << width << " * " << height
       << ", compression: ";

  // Allocate enough room for _all_ types of data.
  if ((data = new char[3*width*height]) == NULL)
  {
    cout << "Out of memory error!" << endl; exit(0);
  }

  r_Minterval iv(2);
  iv << r_Sinterval(r_Range(0), r_Range(width - 1))
     << r_Sinterval(r_Range(0), r_Range(height - 1));

  // Take into account that MDD arrays are transposed compared to
  // images!
  cout << "Greyscale:" << endl;
  lineBase = data;
  for (j=0; j<height; j++, lineBase++)
  {
    line = lineBase;
    for (i=0; i<width; i++, line += height)
    {
      *line = (char)((i+j) & 0xff);
    }
  }

#ifdef TEST_CONV_USE_RTYPES
  type = r_Type::get_base_type("char");
  ConvertToTIFF(data, iv, type, tiffnames[0].write, paramptr);
  delete type;
#else
  ConvertToTIFF(data, iv, (int)(r_Convertor::ctype_char), tiffnames[0].write, paramptr);
#endif

  cout << "Bitmap:" << endl;
  lineBase = data;
  for (j=0; j<height; j++, lineBase++)
  {
    line = lineBase;
    for (i=0; i<width; i++, line += height)
    {
      *line = (char)((i + j) & 1);
    }
  }

#ifdef TEST_CONV_USE_RTYPES
  type = r_Type::get_base_type("boolean");
  ConvertToTIFF(data, iv, type, tiffnames[1].write, paramptr);
  delete type;
#else
  ConvertToTIFF(data, iv, (int)(r_Convertor::ctype_bool), tiffnames[1].write, paramptr);
#endif

  cout << "RGB:" << endl;
  lineBase = data;
  for (j=0; j<height; j++, lineBase += 3)
  {
    line = lineBase;
    for (i=0; i<width; i++, line += 3*height)
    {
      line[0] = ((i+j) & 0xff); line [1] = ((0xff - (i+j)) & 0xff);
      line[2] = (((i+j) >> 1) & 0xff);
    }
  }

#ifdef TEST_CONV_USE_RTYPES
  type = r_Type::get_base_type("struct{ char red, char green, char blue }");
  ConvertToTIFF(data, iv, type, tiffnames[2].write, paramptr);
  delete type;
#else
  ConvertToTIFF(data, iv, (int)(r_Convertor::ctype_rgb), tiffnames[2].write, paramptr);
#endif

  delete [] data;

  // Try the other way around: convert from TIFF
  ConvertFromTIFF("rgb.tif", paramptr,
	((flags & CONVERTOR_WRITE_BACK) != 0) ? tiffnames[2].verify : NULL);

  ConvertFromTIFF("bool.tif", paramptr,
	((flags & CONVERTOR_WRITE_BACK) != 0) ? tiffnames[1].verify : NULL);

  ConvertFromTIFF("grey.tif", paramptr,
	((flags & CONVERTOR_WRITE_BACK) != 0) ? tiffnames[0].verify : NULL);

  return 0;
}