MrSID Decode SDK for Raster Reference Manual  9.5.5.5244
DerivedStream.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 your own stream class. This example
// stream takes a standard FILE* and wraps the LTIOStreamInf stream interface
// around it.
#include "main.h"
#include "support.h"
#include "lt_base.h"
#include "lt_ioStreamInf.h"
LT_USE_NAMESPACE(LizardTech);
//---------------------------------------------------------------------------
// simple stdio/FILE* stream
//---------------------------------------------------------------------------
class MyStream : public LTIOStreamInf
{
public:
MyStream() :
m_file(nullptr)
{
}
~MyStream()
{
}
LT_STATUS initialize(FILE* fp)
{
m_file = fp;
return m_file != nullptr ? LT_STS_Success : LT_STS_Failure;
}
bool isEOF()
{
return feof(m_file) != 0;
}
bool isOpen()
{
return (m_file != nullptr);
}
{
return m_file != nullptr ? LT_STS_Success : LT_STS_Failure;
}
{
}
lt_uint32 read(lt_uint8 *pDest, lt_uint32 numBytes)
{
return (lt_uint32)fread(pDest, sizeof(lt_uint8), numBytes, m_file);
}
lt_uint32 write(const lt_uint8 *pSrc, lt_uint32 numBytes)
{
return (lt_uint32)fwrite(pSrc, sizeof(lt_uint8), numBytes, m_file);
}
{
int stdOrigin;
switch (origin)
{
stdOrigin = SEEK_SET;
break;
stdOrigin = SEEK_CUR;
break;
stdOrigin = SEEK_END;
break;
default:
}
// stdio doesn't support 64-bit files
if (offset > LT_LONG_MAX || offset < LT_LONG_MIN)
if(::fseek(m_file, (long)offset, stdOrigin) == 0)
else
}
{
return ftell(m_file);
}
{
// not supported by this class
return nullptr;
}
{
if(ferror(m_file))
return LT_STS_Failure; //return errno;
else
}
const char* getID() const
{
return "my_stdio_stream:";
}
private:
FILE* m_file;
};
//---------------------------------------------------------------------------
LT_STATUS DerivedStream()
{
// read in a file using the stdio stream, then write it
// to disk the same way
#if defined(LT_OS_WIN32) || defined(LT_OS_WIN64)
const lt_uint32 len = 44;
#else
const lt_uint32 len = 40;
#endif
lt_uint8* buf = new lt_uint8[len];
{
FILE* inFP = fopen(INPUT_PATH("meg.hdr"), "rb");
MyStream inStream;
TEST_SUCCESS(inStream.initialize(inFP));
TEST_SUCCESS(inStream.open());
TEST_BOOL(inStream.read(buf, len) == len);
inStream.close();
fclose(inFP);
}
{
FILE* outFP = fopen(OUTPUT_PATH("foo"), "wb");
MyStream outStream;
TEST_SUCCESS(outStream.initialize(outFP));
TEST_SUCCESS(outStream.open());
TEST_BOOL(outStream.write(buf, len) == len);
outStream.close();
fclose(outFP);
}
delete [] buf;
// verify we got the right output
TEST_BOOL(Compare(OUTPUT_PATH("foo"), INPUT_PATH("meg.hdr")));
Remove(OUTPUT_PATH("foo"));
}
Include file for all LizardTech sources.
LTIOSeekDir
Stream seek directions.
Definition: lt_lib_io.h:26
@ LTIO_SEEK_DIR_BEG
Offset from the beginning of the file.
Definition: lt_lib_io.h:31
@ LTIO_SEEK_DIR_CUR
Offset from the current read/write position.
Definition: lt_lib_io.h:34
@ 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_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 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
Abstract definition of a stream.
virtual LT_STATUS getLastError() const =0
Get status code of last error event.
virtual lt_uint32 read(lt_uint8 *pDest, lt_uint32 numBytes)=0
Retrieve the specified number of bytes from the data source and place them in pDest.
virtual LTIOStreamInf * duplicate()=0
Clone the stream.
virtual const char * getID() const =0
Get a URI describing the stream object.
virtual lt_uint32 write(const lt_uint8 *pSrc, lt_uint32 numBytes)=0
Store the specified number of bytes in the data source.
virtual bool isEOF()=0
Test for end-of-stream.
virtual bool isOpen()=0
Test for stream openness.
virtual LT_STATUS seek(lt_int64 offset, LTIOSeekDir origin)=0
Moves the data access position to origin + offset.
virtual lt_int64 tell()=0
Returns the current data access position as an offset from the start of the data.
virtual LT_STATUS open()=0
Opens the stream.
virtual LT_STATUS close()=0
Closes the stream.

Extensis