/*
 Copyright (c) 2010, The Barbarian Group
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are permitted provided that
 the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and
	the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
	the following disclaimer in the documentation and/or other materials provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
*/

#include "cinder/ImageSourceOF.h"

namespace cinder {

///////////////////////////////////////////////////////////////////////////////
// Registrar
void ImageSourceOF::registerSelf()
{
	//ImageIoRegistrar::registerSourceGeneric( ImageSourceOF::createRef, 1 );
	ImageIoRegistrar::SourceCreationFunc sourceFunc = ImageSourceOF::createSourceRef;
	ImageIoRegistrar::registerSourceType( "png", sourceFunc, 1 );
	ImageIoRegistrar::registerSourceType( "gif", sourceFunc, 1 );
	ImageIoRegistrar::registerSourceType( "jpg", sourceFunc, 1 );
	ImageIoRegistrar::registerSourceType( "jpeg", sourceFunc, 1 );
}

///////////////////////////////////////////////////////////////////////////////
// ImageSourceOF
ImageSourceOFRef ImageSourceOF::createRef( DataSourceRef dataSourceRef )
{
	return ImageSourceOFRef( new ImageSourceOF( dataSourceRef ) );
}

ImageSourceOF::ImageSourceOF( DataSourceRef dataSourceRef )
	: ImageSource()
{
	mofImagePtr = shared_ptr<ofImage>(new ofImage);
	mofImagePtr->setUseTexture(false);
	std::cout << "loading ofImage" << std::endl;
	if(dataSourceRef->isFilePath()){
		std::cout << dataSourceRef->getFilePath() << std::endl;
		mofImagePtr->loadImage(dataSourceRef->getFilePath());
			//throw ImageSourceOFException();
	}else{

		std::cout << "not a file path trying to load filepathhint" << std::endl;
		std::cout << dataSourceRef->getFilePathHint() << std::endl;
		mofImagePtr->loadImage(dataSourceRef->getFilePathHint());
			//throw ImageSourceOFException();
	}
	processFormat();
}

ImageSourceOF::~ImageSourceOF()
{
	
}

bool ImageSourceOF::processFormat( )
{
	if(mofImagePtr->type==OF_IMAGE_COLOR){
		setChannelOrder( ImageIo::RGB ); setColorModel( ImageIo::CM_RGB ); setDataType( ImageIo::UINT8 );
	}
	else if(mofImagePtr->type==OF_IMAGE_COLOR_ALPHA){
		setChannelOrder( ImageIo::RGBA ); setColorModel( ImageIo::CM_RGB ); setDataType( ImageIo::UINT8 );
	}
	else if(mofImagePtr->type==OF_IMAGE_GRAYSCALE){
		setChannelOrder( ImageIo::Y ); setColorModel( ImageIo::CM_GRAY ); setDataType( ImageIo::UINT8 );
	}

	mWidth = mofImagePtr->width; mHeight = mofImagePtr->height;
	return true;
}

void ImageSourceOF::load( ImageTargetRef target )
{

	ImageSource::RowFunc func = setupRowFunc( target );

	const uint8_t *data = mofImagePtr->getPixels();
	for( int32_t row = 0; row < mHeight; ++row ) {
		((*this).*func)( target, row, data );
		data += int(mofImagePtr->getWidth()) * mofImagePtr->bpp/8;
	}
}

} // namespace cinder
