MrSID Decode SDK for Raster Reference Manual 9.5.5.5244
DerivedImageFilter.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 image filter from LTIImageFilter.
// For this example, we make a filter which simply converts RGB images to
// grayscale. (For clarity of presentation, this filter does not update
// the metadata to reflect the new colorspace, it only works on 8 bit
// samples, and it does it modify the background and nodata pixels to be
// grayscale data.)
#include "main.h"
#include "support.h"
#include "lt_fileSpec.h"
#include "lti_pixel.h"
#include "lti_scene.h"
LT_USE_NAMESPACE(LizardTech);
//---------------------------------------------------------------------------
// RGB to grayscale filter
//---------------------------------------------------------------------------
class MyFilter : public LTIOverridePixelProps<LTIImageFilter>
{
private:
public:
LT_STATUS initialize(LTIImageStage* sourceImage)
{
LT_STATUS sts = LTIImageFilter::init(sourceImage);
if (!LT_SUCCESS(sts))
return sts;
const LTIImageStage& prev = *getPreviousStage();
sts = setPixelProps(props);
if (!LT_SUCCESS(sts))
return sts;
}
// LTIImageStage override
lt_uint32 getModifications(const LTIScene &scene) const
{
if (getPreviousStage())
mods = getPreviousStage()->getModifications(scene);
return mods;
}
protected:
LT_STATUS decodeBegin(const LTIPixel &pixelProps,
const LTIScene &fullScene)
{
// set up state needed for this decode
return getPreviousStage()->readBegin(getPreviousStage()->getPixelProps(),
fullScene);
}
LT_STATUS decodeStrip(LTISceneBuffer &stripBuffer,
const LTIScene &stripScene)
{
const lt_int32 width = stripScene.getNumCols();
const lt_int32 height = stripScene.getNumRows();
// make a buffer to hold the rgb data
LTISceneBuffer srcData(getPreviousStage()->getPixelProps(),
width, height, nullptr);
// read the RGB data from the previous stage
sts = getPreviousStage()->readStrip(srcData, stripScene);
if (!LT_SUCCESS(sts))
return sts;
// copy the data from the RGB buffer into our grayscale buffer
lt_uint8 *red = static_cast<lt_uint8 *>(srcData.getBandData(0));
lt_uint8 *green = static_cast<lt_uint8 *>(srcData.getBandData(1));
lt_uint8 *blue = static_cast<lt_uint8 *>(srcData.getBandData(2));
lt_uint8 *gray = static_cast<lt_uint8 *>(stripBuffer.getBandData(0));
for (lt_int32 r = 0; r < height; r++)
{
for (lt_int32 c = 0; c < width; c++)
{
gray[c] = static_cast<lt_uint8>(0.3f * red[c] +
0.6f * green[c] +
0.1f * blue[c]);
}
red += srcData.getTotalNumCols();
green += srcData.getTotalNumCols();
blue += srcData.getTotalNumCols();
gray += stripBuffer.getTotalNumCols();
}
}
LT_STATUS decodeEnd(void)
{
// clean up state
return getPreviousStage()->readEnd();
}
};
MyFilter::MyFilter(void) {}
MyFilter::~MyFilter() {}
MyFilter *MyFilter::create(void)
{
return new MyFilter;
}
//---------------------------------------------------------------------------
LT_STATUS DerivedImageFilter()
{
// read in a raw rgb image, and write it out as grayscale
// make the reader
const LTFileSpec fileSpec(INPUT_PATH("meg.bip"));
TEST_BOOL(reader != nullptr);
TEST_SUCCESS(reader->initialize(fileSpec));
// connect up the filter
MyFilter *filter = MyFilter::create();
TEST_BOOL(filter != nullptr);
TEST_SUCCESS(filter->initialize(reader));
TEST_BOOL(reader->getColorSpace() == LTI_COLORSPACE_RGB);
TEST_BOOL(filter->getColorSpace() == LTI_COLORSPACE_GRAYSCALE);
// make the BBB writer
TEST_SUCCESS(writer.initialize(filter));
// set up the output file
TEST_SUCCESS(writer.setOutputFileSpec(OUTPUT_PATH("meg_gray.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_gray.bip"), INPUT_PATH("meg_gray.bip")));
Remove(OUTPUT_PATH("meg_gray.bip"));
Remove(OUTPUT_PATH("meg_gray.hdr"));
filter->release();
filter = nullptr;
reader->release();
reader = nullptr;
}
#define LTI_REFERENCE_COUNTED_BOILERPLATE(classname)
@ LTI_DATATYPE_UINT8
Definition: lti_types.h:103
@ LTI_MODIFICATION_CHANGEDCOLORSPACE
Definition: lti_types.h:204
@ LTI_MODIFICATION_UNKNOWN
Definition: lti_types.h:196
@ LTI_COLORSPACE_GRAYSCALE
Definition: lti_types.h:40
@ LTI_COLORSPACE_RGB
Definition: lti_types.h:46
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 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
signed int lt_int32
signed 32-bit integer
Definition: lt_types.h:42
Represents a file or directory path.
Definition: lt_fileSpec.h:35
read an image from a BBB file
static LTIBBBImageReader * create(void)
LT_STATUS initialize(const LTFileSpec &fileSpec, bool useWorldFile=true)
intializer
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
LTIColorSpace getColorSpace() const
get colorspace
LT_STATUS init(LTIImageStage *previousStage)
abstract class for decoding from an image
LT_STATUS setPixelProps(const LTIPixel &pixelProps)
set pixel properties of the image
const LTIPixel & getPixelProps() const
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
LT_STATUS initialize(LTIImageStage *imageStage)
initialization function
virtual void release(void) const
decrement the reference counter and delete the object when the counter is zero
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
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)

Extensis