juliapluginreader.cpp
//-----------------------------------------------------------------------------
//$Id: juliapluginreader.cpp 189 2007-04-19 10:53:38Z robin $
//
// Copyright (c) 2006 Zanja Technologies, Inc
//
// TRADE SECRETS: PROPRIETARY AND CONFIDENTIAL
// Unpublished material - all rights reserved under the
// Copyright Laws of the United States.
//
// Distributed under license by ESRI, Inc.
// For additional information, contact:
// Environmental Systems Research Institute, Inc.
// 380 New York Street
// Redlands, California, USA 92373
//
/*! \file juliapluginreader.cpp
\brief <TODO: insert file description here>
\author Robin Princeley
\date $Date: 2007-04-19 14:53:38 +0400 (Thu, 19 Apr 2007) $
\version $Revision: 189 $
*/
//-----------------------------------------------------------------------------
#include "julia.h"
// Julia function
uint32_t iterateJulia( std::complex< float > &z,
std::complex< float > &c,
const uint32_t &iterations = 128 )
{
uint32_t counter = 0;
while( ( ( ( z.real() * z.real() ) + ( z.imag() * z.imag() ) ) < 4.0 ) && ( counter < iterations ) ) {
z = ( z * z ) + c;
counter++;
}
return counter;
}
JuliaPluginReader::JuliaPluginReader() : mFilename(), mFractalROI(), mCenter()
{
mIterations = 100;
mIndex = 0;
mFractalROI.ulx = -1.50;
mFractalROI.uly = -1.50;
mFractalROI.lrx = 1.50;
mFractalROI.lry = 1.50;
mCenter = std::complex< float >( 0.360284, 0.100376 );
}
JuliaPluginReader::~JuliaPluginReader()
{
close();
}
bool JuliaPluginReader::open( const std::string & filename, uint32_t index, uint32_t slice,
const FC_Parameter *parameters, uint32_t parameterCount )
{
uint32_t count = 1;
uint32_t result = JULIAERROR_None;
assert( slice == 0 );
close();
mFilename = filename;
if ( mFilename.empty() ) {
return failure( JULIAERROR_ReadFailed );
}
result = readDescription( mFilename, mInformation, mFractalROI, mCenter, mIterations, count );
if ( result != JULIAERROR_None ) {
return failure( result );
}
if ( index >= count ) {
return failure( JULIAERROR_ReadOverviewOutOfRange );
}
mIndex = index;
if ( mIndex ) {
mInformation.width /= ( mIndex + 1 );
mInformation.height /= ( mIndex + 1 );
}
mInformation.bands = 1;
mInformation.bits = 8;
mInformation.byteorder = FC_ByteOrderNative;
mInformation.colorSpace = FC_ColorSpacePalette;
mInformation.dataType = FC_DataTypeByte;
mInformation.layout = FC_LayoutBIP;
mInformation.tileWidth = 0;
mInformation.tileHeight = 0;
return success();
}
bool JuliaPluginReader::close()
{
return success();
}
bool JuliaPluginReader::prepare( const FC_ImageROI *roi,
uint32_t rows, uint32_t cols,
const FC_Parameter *parameters, uint32_t parameterCount )
{
mImageROI.ulx = roi->ulx;
mImageROI.uly = roi->uly;
mImageROI.lrx = roi->lrx;
mImageROI.lry = roi->lry;
return success();
}
bool JuliaPluginReader::readScanlines( byte_t **buffer, uint32_t startRow,
uint32_t rows, size_t size, uint32_t plane )
{
double_t dx, dy;
uint32_t currentX, currentY, width;
uint32_t pixelColor;
float px, py;
width = mImageROI.lrx - mImageROI.ulx;
dx = getDX();
dy = getDY();
for( uint32_t y = 0; y < rows; y++ ) {
currentY = startRow + y;
py = mFractalROI.uly + static_cast< float >( currentY ) * dy;
for( uint32_t x = 0; x < width; x++ ) {
currentX = mImageROI.ulx + x;
px = mFractalROI.ulx + static_cast< float >( currentX ) * dx;
pixelColor = iterateJulia( std::complex< float >( px, py ),
mCenter,
mIterations );
pixelColor = min( pixelColor, static_cast< uint32_t >( 255 ) );
buffer[y][x] = pixelColor;
}
}
return success();
}
bool JuliaPluginReader::getOverviewCount( const std::string &filename, uint32_t &count )
{
FC_ImageInformation info;
SimpleROI roi;
std::complex< float > point;
uint32_t iterations;
if ( JuliaPluginReader::readDescription( filename, info, roi, point, iterations, count ) != JULIAERROR_None ) {
return false;
}
return true;
}
bool JuliaPluginReader::failure( uint32_t errorCode )
{
mError = errorCode;
return ( mError == JULIAERROR_None );
}
bool JuliaPluginReader::success()
{
mError = JULIAERROR_None;
return ( mError == JULIAERROR_None );
}
uint32_t JuliaPluginReader::readDescription( const std::string &filename,
FC_ImageInformation &info,
SimpleROI &roi,
std::complex< float > &c,
uint32_t &iterations,
uint32_t &count ) throw()
{
char str[256];
uint32_t width, height;
FILE *file = NULL;
double cr = 0.360284, ci = 0.100376;
memset( &info, 0, sizeof( FC_ImageInformation ) );
file = fopen( filename.c_str(), "r" );
if ( !file ) {
return JULIAERROR_ReadFailed;
}
while( 1 ) {
fgets( str, 256, file );
if( feof( file ) ) {
break;
} else {
if ( strnicmp( str, "ncols", 4 ) == 0 ) {
sscanf( str, "%*s%lu", &info.width );
} else if ( strnicmp( str, "nrows", 4 ) == 0 ) {
sscanf( str,"%*s%lu", &info.height );
} else if ( strnicmp( str, "ulx", 6) == 0 ) {
sscanf( str,"%*s%lf", &roi.ulx );
} else if ( strnicmp( str, "uly", 6) == 0 ) {
sscanf( str,"%*s%lf", &roi.uly );
} else if ( strnicmp( str, "lrx", 4) == 0 ) {
sscanf( str,"%*s%lf", &roi.lrx );
} else if ( strnicmp( str, "lry", 4) == 0 ) {
sscanf(str, "%*s%lf", &roi.lry );
} else if ( strnicmp( str, "iterations", 10 ) == 0 ) {
sscanf( str,"%*s%lu", &iterations );
} else if ( strnicmp( str, "creal", 5 ) == 0 ) {
sscanf( str,"%*s%lf", &cr );
} else if ( strnicmp( str, "cimg", 4 ) == 0 ) {
sscanf( str,"%*s%lf", &ci );
}
}
}
fclose( file );
if( !info.width ) {
return JULIAERROR_NoWidth;
}
if( !info.height ) {
return JULIAERROR_NoHeight;
}
c = std::complex< float >( cr, ci );
count = 0;
/* width = info->width;
height = info->height;
while ( ( width < 256 ) && ( height < 256 ) ) {
count++;
width /= 2;
height /= 2;
}
*/ if ( !count ) {
count = 1;
}
return JULIAERROR_None;
}