MrSID Decode SDK for Raster Reference Manual 9.5.5.5244
DerivedImageReader.cpp
//
// This code is property of Celartem, Inc dba Extensis.
// Copyright (c) 1995-2023. All rights reserved.
// Use permitted only under license from Extensis.
//
/* PUBLIC */
// This demonstrates how to derive a simple reader. This class reads raw
// files (BIP), with the colorspace, dimension, etc, passed in via the
// constructor. To make the example simple, we do not supplort duplicate(),
// metadata, background/nodata pixels, we only support 8-bit samples, etc.
#include "main.h"
#include "support.h"
#include "lt_fileSpec.h"
#include "lti_pixel.h"
#include "lti_scene.h"
#include "lti_utils.h"
LT_USE_NAMESPACE(LizardTech);
#ifdef WIN32_DLL_WORKAROUND
template <class T>
class LTIDLLFileStream : public T
{
public:
LTIDLLFileStream() {}
virtual ~LTIDLLFileStream() {}
};
#endif
//---------------------------------------------------------------------------
// reader for simple BIP raw files
//---------------------------------------------------------------------------
class MyReader : public LTIImageReader
{
public:
LT_STATUS initialize(const LTFileSpec& fileSpec,
const LTIPixel& pixelProps,
lt_uint32 width,
lt_uint32 height)
{
if (!LT_SUCCESS(sts))
return sts;
// only support LTI_DATATYPE_UINT8 or LTI_DATATYPE_UINT16
if (pixelProps.getDataType() != LTI_DATATYPE_UINT8 &&
#ifdef WIN32_DLL_WORKAROUND
m_stream = new LTIDLLFileStream<LTIOFileStream>;
#else
m_stream = new LTIOFileStream;
#endif
m_stream->initialize(fileSpec, "rb");
if (!m_stream)
sts = m_stream->open();
if (!LT_SUCCESS(sts))
return sts;
sts = setPixelProps(pixelProps);
if (!LT_SUCCESS(sts))
return sts;
sts = setDimensions(width, height);
if (!LT_SUCCESS(sts))
return sts;
sts = setDefaultGeoCoord(*this);
if (!LT_SUCCESS(sts))
return sts;
m_rowBytes = pixelProps.getNumBytes() * getWidth();
}
lt_int64 getPhysicalFileSize(void) const
{
const lt_int64 pos = m_stream->tell();
m_stream->seek(0,LTIO_SEEK_DIR_END);
const lt_int64 siz = m_stream->tell();
m_stream->seek(pos,LTIO_SEEK_DIR_BEG);
return siz;
}
protected:
LT_STATUS decodeBegin(const LTIPixel &pixelProps,
const LTIScene &fullScene)
{
const lt_uint32 numCols = fullScene.getNumCols();
const lt_uint32 bytesPerPixel = getPixelProps().getNumBytes();
const lt_uint32 sceneRowBytes = numCols * bytesPerPixel;
m_rowBuffer = new lt_uint8[sceneRowBytes];
}
const LTIScene &stripScene)
{
const lt_uint32 numCols = stripScene.getNumCols();
const lt_uint32 numRows = stripScene.getNumRows();
const lt_uint32 totalNumCols = stripBuffer.getTotalNumCols();
const LTIPixel &pixel = stripBuffer.getPixelProps();
const lt_uint32 bytesPerPixel = pixel.getNumBytes();
const lt_uint32 bytesPerSample = pixel.getSample(0).getNumBytes();
const lt_uint32 sceneRowBytes = numCols * bytesPerPixel;
const lt_uint16 numBands = stripBuffer.getNumBands();
lt_int64 curPos = stripScene.getUpperLeftRow() * m_rowBytes +
stripScene.getUpperLeftCol() * bytesPerPixel;
for (lt_uint32 row = 0; row < numRows; row++)
{
// load a BIP row from disk
sts = m_stream->seek(curPos, LTIO_SEEK_DIR_BEG);
if (!LT_SUCCESS(sts))
return sts;
if (m_stream->read(m_rowBuffer, sceneRowBytes) != sceneRowBytes)
return m_stream->getLastError();
// load the BIP row into the BSQ SceneBuffer
for(lt_uint16 band = 0; band < numBands; band++)
{
const lt_uint8 *src = m_rowBuffer + band * bytesPerSample;
lt_uint8 *dst = static_cast<lt_uint8 *>(stripBuffer.getBandData(band));
// skip down to the row of interest
dst += row * totalNumCols * bytesPerSample;
for(lt_uint32 col = 0; col < numCols; col++, src += bytesPerPixel, dst += bytesPerSample)
memcpy(dst, src, bytesPerSample);
}
curPos += m_rowBytes;
}
stripBuffer.byteSwap();
}
{
delete [] m_rowBuffer;
m_rowBuffer = nullptr;
}
const char *getSourceName(void) const
{
return m_stream != nullptr ? m_stream->getID() : nullptr;
}
private:
#ifdef WIN32_DLL_WORKAROUND
LTIDLLFileStream<LTIOFileStream> *m_stream;
#else
LTIOFileStream *m_stream;
#endif
lt_uint32 m_rowBytes;
lt_uint8 *m_rowBuffer;
};
MyReader::MyReader() :
m_stream(nullptr),
m_rowBuffer(nullptr),
m_rowBytes(0)
{
}
MyReader::~MyReader()
{
m_stream->close();
delete m_stream;
delete[] m_rowBuffer;
}
MyReader*
MyReader::create()
{
return new MyReader;
}
//---------------------------------------------------------------------------
LT_STATUS DerivedImageReader()
{
// read in a raw rgb image, and write it out as grayscale
// make the reader
const LTFileSpec fileSpec(INPUT_PATH("meg.bip"));
MyReader *reader = MyReader::create();
TEST_BOOL(reader != nullptr);
TEST_SUCCESS(reader->initialize(fileSpec, pixel, 640, 480));
// make the BBB writer
TEST_SUCCESS(writer.initialize(reader));
// set up the output file
TEST_SUCCESS(writer.setOutputFileSpec(OUTPUT_PATH("meg.bip")));
const LTIScene scene(0, 0, 640, 480, 1.0);
// write the scene to the file
TEST_SUCCESS(writer.write(scene));
// verify we got the right output
TEST_BOOL(Compare(OUTPUT_PATH("meg.bip"), INPUT_PATH("meg.bip")));
Remove(OUTPUT_PATH("meg.bip"));
Remove(OUTPUT_PATH("meg.hdr"));
reader->release();
reader = nullptr;
}
#define LTI_REFERENCE_COUNTED_BOILERPLATE(classname)
@ LTI_DATATYPE_UINT16
Definition: lti_types.h:105
@ LTI_DATATYPE_UINT8
Definition: lti_types.h:103
@ LTI_COLORSPACE_RGB
Definition: lti_types.h:46
@ LTI_ENDIAN_LITTLE
Definition: lti_types.h:163
@ LTIO_SEEK_DIR_BEG
Offset from the beginning of the file.
Definition: lt_lib_io.h:31
@ LTIO_SEEK_DIR_END
Offset from the end of the file (offsets need to be negative to be valid)
Definition: lt_lib_io.h:37
lt_uint32 LT_STATUS
An integral status code.
Definition: lt_status.h:30
#define LT_STS_Uninit
status code indicating unintialized variable
Definition: lt_status.h:60
#define LT_SUCCESS(err)
checks return value for success status
Definition: lt_status.h:143
#define LT_STS_Failure
status code indicating failure
Definition: lt_status.h:51
#define LT_STS_Success
status code indicating success
Definition: lt_status.h:43
unsigned short lt_uint16
unsigned 16-bit integer
Definition: lt_types.h:40
unsigned char lt_uint8
unsigned 8-bit integer
Definition: lt_types.h:36
unsigned int lt_uint32
unsigned 32-bit integer
Definition: lt_types.h:44
long long int lt_int64
signed 64-bit integer
Definition: lt_types.h:57
Represents a file or directory path.
Definition: lt_fileSpec.h:35
File stream.
virtual LT_STATUS initialize(const LTFileSpec &fs, const char *mode)
Initializes the stream from a file spec.
writes an image stage to a BBB file
virtual LT_STATUS setOutputFileSpec(const LTFileSpec &fileSpec)
set output file name
LTIDataType getDataType() const
get data type
abstract class for implementing an image reader
virtual const char * getSourceName(void) const =0
LT_STATUS init(const LTIImage *image=nullptr)
virtual LT_STATUS decodeBegin(const LTIPixel &pixelProps, const LTIScene &fullScene)=0
start strip-based read
virtual LT_STATUS decodeStrip(LTISceneBuffer &stripBuffer, const LTIScene &stripScene)=0
read a strip from the image
virtual LT_STATUS decodeEnd(void)=0
finish strip-based read
LT_STATUS setDimensions(lt_uint32 width, lt_uint32 height)
set width and height of the image
LT_STATUS setPixelProps(const LTIPixel &pixelProps)
set pixel properties of the image
LT_STATUS setDefaultGeoCoord(const LTIImage &image)
set the geographic coordinates of the image
virtual LT_STATUS write(const LTIScene &scene)
write (encode) a scene to the output format
basic properties of a pixel
Definition: lti_pixel.h:32
LTIDataType getDataType() const
get the sample datatype
LTISample & getSample(lt_uint16) const
lt_uint32 getNumBytes() const
get the size of a pixel
LT_STATUS initialize(LTIImageStage *imageStage)
initialization function
lt_uint32 getNumBytes() const
get the size of a sample
representation of a scene
Definition: lti_scene.h:60
lt_int32 getNumRows() const
returns the height of the scene
lt_int32 getNumCols() const
returns the width of the scene
lt_int32 getUpperLeftRow() const
returns the y-position of the upper-left point
lt_int32 getUpperLeftCol() const
returns the x-position of the upper-left point
class to hold data passed between image stages
lt_int32 getTotalNumCols() const
get width of (entire) buffer
void * getBandData(lt_uint16 band) const
get pointer to data (for 1 band)
lt_uint16 getNumBands() const
get number of bands
const LTIPixel & getPixelProps() const
get pixel type of buffer
void byteSwap()
export to memory (BSQ)
static bool needsSwapping(LTIDataType datatype, LTIEndian byteOrder)

Extensis