~ubuntu-branches/ubuntu/saucy/openexr/saucy

« back to all changes in this revision

Viewing changes to IlmImfTest/testExistingStreams.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adeodato Simó
  • Date: 2008-03-24 23:00:21 UTC
  • mfrom: (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080324230021-gnofz9mnvcj1xlv3
Tags: 1.6.1-3
Disable (hopefully temporarily) the test suite on arm and ia64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
 
43
43
#include <stdio.h>
44
44
#include <assert.h>
 
45
#include "Iex.h"
 
46
#include <errno.h>
45
47
 
46
48
using namespace Imf;
47
49
using namespace Imath;
85
87
}
86
88
 
87
89
 
 
90
//
 
91
// class MMIFStream -- a memory-mapped implementation of
 
92
// class IStream based on class std::ifstream
 
93
//
 
94
 
 
95
class MMIFStream: public IStream
 
96
{
 
97
  public:
 
98
 
 
99
    //-------------------------------------------------------
 
100
    // A constructor that opens the file with the given name.
 
101
    // It reads the whole file into an internal buffer and
 
102
    // then immediately closes the file.
 
103
    //-------------------------------------------------------
 
104
 
 
105
    MMIFStream (const char fileName[]);
 
106
 
 
107
    virtual ~MMIFStream ();
 
108
 
 
109
    virtual bool        isMemoryMapped () const {return true;}
 
110
 
 
111
    virtual bool        read (char c[/*n*/], int n);
 
112
    virtual char*       readMemoryMapped (int n);
 
113
    virtual Int64       tellg () {return _pos;}
 
114
    virtual void        seekg (Int64 pos) {_pos = pos;}
 
115
    virtual void        clear () {}
 
116
 
 
117
  private:
 
118
 
 
119
    char*               _buffer;
 
120
    Int64               _length;
 
121
    Int64               _pos;
 
122
};
 
123
 
 
124
 
 
125
 
 
126
MMIFStream::MMIFStream (const char fileName[]):
 
127
    IStream (fileName),
 
128
    _buffer (0),
 
129
    _length (0),
 
130
    _pos (0)
 
131
{
 
132
    std::ifstream ifs (fileName, ios_base::binary);
 
133
 
 
134
    //
 
135
    // Get length of file
 
136
    //
 
137
 
 
138
    ifs.seekg (0, ios::end);
 
139
    _length = ifs.tellg();
 
140
    ifs.seekg (0, ios::beg);
 
141
    
 
142
    //
 
143
    // Allocate memory
 
144
    //
 
145
 
 
146
    _buffer = new char [_length];
 
147
    
 
148
    //
 
149
    // Read the entire file
 
150
    //
 
151
 
 
152
    ifs.read (_buffer, _length);
 
153
    ifs.close();
 
154
}
 
155
 
 
156
 
 
157
MMIFStream::~MMIFStream ()
 
158
{
 
159
    delete [] _buffer;
 
160
}
 
161
 
 
162
 
 
163
bool
 
164
MMIFStream::read (char c[/*n*/], int n)
 
165
{
 
166
    if ((_pos < 0 || _pos >= _length) && n != 0)
 
167
        throw Iex::InputExc ("Unexpected end of file.");
 
168
        
 
169
    Int64 n2 = n;
 
170
    bool retVal = true;
 
171
 
 
172
    if (_length - _pos <= n2)
 
173
    {
 
174
        n2 = _length - _pos;
 
175
        retVal = false;
 
176
    }
 
177
 
 
178
    memcpy (c, &(_buffer[_pos]), n2);
 
179
    _pos += n2;
 
180
    return retVal;
 
181
}
 
182
 
 
183
 
 
184
char*
 
185
MMIFStream::readMemoryMapped (int n)
 
186
{
 
187
    if (_pos < 0 || _pos >= _length)
 
188
        throw Iex::InputExc ("Unexpected end of file.");
 
189
       
 
190
    if (_pos + n > _length)
 
191
        throw Iex::InputExc ("Reading past end of file.");    
 
192
 
 
193
    char* retVal = &(_buffer[_pos]);
 
194
    _pos += n;
 
195
    return retVal;
 
196
}
 
197
 
 
198
 
88
199
void
89
200
writeReadScanLines (const char fileName[],
90
201
                    int width,
96
207
    // letting the RgbaOutputFile object open the file,
97
208
    // make the RgbaOutputFile object use an existing
98
209
    // StdOFStream.  Read the image back, using an
99
 
    // existing  SgdIFStream, and compare the pixels
100
 
    // with the original data.
 
210
    // existing StdIFStream, and compare the pixels
 
211
    // with the original data.  Then read the image
 
212
    // back a second time using a memory-mapped
 
213
    // MMIFStream (see above).
101
214
    //
102
215
 
103
 
    cout << "scan-line based file" << endl;
 
216
    cout << "scan-line based file:" << endl;
104
217
 
105
218
    Header header (width, height);
106
219
 
107
220
    {
 
221
        cout << "writing";
108
222
        remove (fileName);
109
 
#ifndef HAVE_IOS_BASE
110
 
        std::ofstream os (fileName, ios::binary);
111
 
#else
112
223
        std::ofstream os (fileName, ios_base::binary);
113
 
#endif
114
224
        StdOFStream ofs (os, fileName);
115
225
        RgbaOutputFile out (ofs, header, WRITE_RGBA);
116
226
        out.setFrameBuffer (&p1[0][0], 1, width);
118
228
    }
119
229
 
120
230
    {
121
 
#ifndef HAVE_IOS_BASE
122
 
        std::ifstream is (fileName, ios::binary|ios::in);
123
 
#else
 
231
        cout << ", reading";
124
232
        std::ifstream is (fileName, ios_base::binary);
125
 
#endif
126
233
        StdIFStream ifs (is, fileName);
127
234
        RgbaInputFile in (ifs);
128
235
 
136
243
        in.setFrameBuffer (&p2[-dy][-dx], 1, w);
137
244
        in.readPixels (dw.min.y, dw.max.y);
138
245
 
139
 
        for (int y = 0; y < h; ++y)
140
 
        {
141
 
            for (int x = 0; x < w; ++x)
142
 
            {
143
 
                assert (p2[y][x].r == p1[y][x].r);
144
 
                assert (p2[y][x].g == p1[y][x].g);
145
 
                assert (p2[y][x].b == p1[y][x].b);
146
 
                assert (p2[y][x].a == p1[y][x].a);
147
 
            }
148
 
        }
149
 
    }
 
246
        cout << ", comparing";
 
247
        for (int y = 0; y < h; ++y)
 
248
        {
 
249
            for (int x = 0; x < w; ++x)
 
250
            {
 
251
                assert (p2[y][x].r == p1[y][x].r);
 
252
                assert (p2[y][x].g == p1[y][x].g);
 
253
                assert (p2[y][x].b == p1[y][x].b);
 
254
                assert (p2[y][x].a == p1[y][x].a);
 
255
            }
 
256
        }
 
257
    }
 
258
    
 
259
    {
 
260
        cout << ", reading (memory-mapped)";
 
261
        MMIFStream ifs (fileName);
 
262
        RgbaInputFile in (ifs);
 
263
 
 
264
        const Box2i &dw = in.dataWindow();
 
265
        int w = dw.max.x - dw.min.x + 1;
 
266
        int h = dw.max.y - dw.min.y + 1;
 
267
        int dx = dw.min.x;
 
268
        int dy = dw.min.y;
 
269
 
 
270
        Array2D<Rgba> p2 (h, w);
 
271
        in.setFrameBuffer (&p2[-dy][-dx], 1, w);
 
272
        in.readPixels (dw.min.y, dw.max.y);
 
273
 
 
274
        cout << ", comparing";
 
275
        for (int y = 0; y < h; ++y)
 
276
        {
 
277
            for (int x = 0; x < w; ++x)
 
278
            {
 
279
                assert (p2[y][x].r == p1[y][x].r);
 
280
                assert (p2[y][x].g == p1[y][x].g);
 
281
                assert (p2[y][x].b == p1[y][x].b);
 
282
                assert (p2[y][x].a == p1[y][x].a);
 
283
            }
 
284
        }
 
285
    }
 
286
    
 
287
    cout << endl;
150
288
 
151
289
    remove (fileName);
152
290
}
162
300
    // Save a tiled RGBA image, but instead of letting
163
301
    // the TiledRgbaOutputFile object open the file, make
164
302
    // it use an existing StdOFStream.  Read the image back,
165
 
    // using an existing  SgdIFStream, and compare the pixels
166
 
    // with the original data.
 
303
    // using an existing StdIFStream, and compare the pixels
 
304
    // with the original data.  Then read the image back a
 
305
    // second time using a memory-mapped MMIFStream (see above).
167
306
    //
168
307
 
169
 
    cout << "tiled file" << endl;
 
308
    cout << "tiled file:" << endl;
170
309
 
171
310
    Header header (width, height);
172
311
 
173
312
    {
 
313
        cout << "writing";
174
314
        remove (fileName);
175
 
#ifndef HAVE_IOS_BASE
176
 
        std::ofstream os (fileName, ios::binary);
177
 
#else
178
315
        std::ofstream os (fileName, ios_base::binary);
179
 
#endif
180
316
        StdOFStream ofs (os, fileName);
181
317
        TiledRgbaOutputFile out (ofs, header, WRITE_RGBA, 20, 20, ONE_LEVEL);
182
318
        out.setFrameBuffer (&p1[0][0], 1, width);
183
 
 
184
 
        for (int dy = 0; dy < out.numYTiles(); ++dy)
185
 
            for (int dx = 0; dx < out.numXTiles(); ++dx)
186
 
                out.writeTile (dx, dy);
 
319
        out.writeTiles (0, out.numXTiles() - 1, 0, out.numYTiles() - 1);
187
320
    }
188
321
 
189
322
    {
190
 
#ifndef HAVE_IOS_BASE
191
 
        std::ifstream is (fileName, ios::binary|ios::in);
192
 
#else
 
323
        cout << ", reading";
193
324
        std::ifstream is (fileName, ios_base::binary);
194
 
#endif
195
325
        StdIFStream ifs (is, fileName);
196
326
        TiledRgbaInputFile in (ifs);
197
327
 
203
333
 
204
334
        Array2D<Rgba> p2 (h, w);
205
335
        in.setFrameBuffer (&p2[-dy][-dx], 1, w);
206
 
 
207
 
        for (int dy = 0; dy < in.numYTiles(); ++dy)
208
 
            for (int dx = 0; dx < in.numXTiles(); ++dx)
209
 
                in.readTile (dx, dy);
210
 
 
211
 
        for (int y = 0; y < h; ++y)
212
 
        {
213
 
            for (int x = 0; x < w; ++x)
214
 
            {
215
 
                assert (p2[y][x].r == p1[y][x].r);
216
 
                assert (p2[y][x].g == p1[y][x].g);
217
 
                assert (p2[y][x].b == p1[y][x].b);
218
 
                assert (p2[y][x].a == p1[y][x].a);
219
 
            }
220
 
        }
221
 
    }
 
336
        in.readTiles (0, in.numXTiles() - 1, 0, in.numYTiles() - 1);
 
337
 
 
338
        cout << ", comparing";
 
339
        for (int y = 0; y < h; ++y)
 
340
        {
 
341
            for (int x = 0; x < w; ++x)
 
342
            {
 
343
                assert (p2[y][x].r == p1[y][x].r);
 
344
                assert (p2[y][x].g == p1[y][x].g);
 
345
                assert (p2[y][x].b == p1[y][x].b);
 
346
                assert (p2[y][x].a == p1[y][x].a);
 
347
            }
 
348
        }
 
349
    }
 
350
    
 
351
    {
 
352
        cout << ", reading (memory-mapped)";
 
353
        MMIFStream ifs (fileName);
 
354
        TiledRgbaInputFile in (ifs);
 
355
 
 
356
        const Box2i &dw = in.dataWindow();
 
357
        int w = dw.max.x - dw.min.x + 1;
 
358
        int h = dw.max.y - dw.min.y + 1;
 
359
        int dx = dw.min.x;
 
360
        int dy = dw.min.y;
 
361
 
 
362
        Array2D<Rgba> p2 (h, w);
 
363
        in.setFrameBuffer (&p2[-dy][-dx], 1, w);
 
364
        in.readTiles (0, in.numXTiles() - 1, 0, in.numYTiles() - 1);
 
365
 
 
366
        cout << ", comparing";
 
367
        for (int y = 0; y < h; ++y)
 
368
        {
 
369
            for (int x = 0; x < w; ++x)
 
370
            {
 
371
                assert (p2[y][x].r == p1[y][x].r);
 
372
                assert (p2[y][x].g == p1[y][x].g);
 
373
                assert (p2[y][x].b == p1[y][x].b);
 
374
                assert (p2[y][x].a == p1[y][x].a);
 
375
            }
 
376
        }
 
377
    }
 
378
    
 
379
    cout << endl;
222
380
 
223
381
    remove (fileName);
224
382
}
243
401
        writeReadScanLines (IMF_TMP_DIR "imf_test_streams.exr", W, H, p1);
244
402
 
245
403
        fillPixels2 (p1, W, H);
246
 
        writeReadTiles (IMF_TMP_DIR "imf_test_streams.exr", W, H, p1);
 
404
        writeReadTiles (IMF_TMP_DIR "imf_test_streams2.exr", W, H, p1);
247
405
 
248
406
        cout << "ok\n" << endl;
249
407
    }
253
411
        assert (false);
254
412
    }
255
413
}
256