//-----------------------------------------------------------------------------
// loadImage()
//-----------------------------------------------------------------------------
bool LLFloaterImagePreview::loadImage(const std::string& src_filename)
{
	std::string exten = gDirUtilp->getExtension(src_filename);
	
	U32 codec = IMG_CODEC_INVALID;
	std::string temp_str;
	if( exten == "bmp")
	{
		codec = IMG_CODEC_BMP;
	}
	else if( exten == "tga")
	{
		codec = IMG_CODEC_TGA;
	}
	else if( exten == "jpg" || exten == "jpeg")
	{
		codec = IMG_CODEC_JPEG;
	}
	else if( exten == "png" )
	{
		codec = IMG_CODEC_PNG;
	}
#ifdef LL_DARWIN
	else if( exten == "psd" )
	{
		codec = IMG_CODEC_PSD;
	}
	else if( exten == "tif" || exten == "tiff" )
	{
		codec = IMG_CODEC_TIFF;
	}
#endif

	LLPointer<LLImageRaw> raw_image = new LLImageRaw;
#ifdef LL_DARWIN
	if (! decodeImageQuartz(src_filename, raw_image))
		return false;
#else
	switch (codec)
	{
	case IMG_CODEC_BMP:
		{
			LLPointer<LLImageBMP> bmp_image = new LLImageBMP;

			if (!bmp_image->load(src_filename))
			{
				return false;
			}
			
			if (!bmp_image->decode(raw_image, 0.0f))
			{
				return false;
			}
		}
		break;
	case IMG_CODEC_TGA:
		{
			LLPointer<LLImageTGA> tga_image = new LLImageTGA;

			if (!tga_image->load(src_filename))
			{
				return false;
			}
			
			if (!tga_image->decode(raw_image))
			{
				return false;
			}

			if(	(tga_image->getComponents() != 3) &&
				(tga_image->getComponents() != 4) )
			{
				tga_image->setLastError( "Image files with less than 3 or more than 4 components are not supported." );
				return false;
			}
		}
		break;
	case IMG_CODEC_JPEG:
		{
			LLPointer<LLImageJPEG> jpeg_image = new LLImageJPEG;

			if (!jpeg_image->load(src_filename))
			{
				return false;
			}
			
			if (!jpeg_image->decode(raw_image, 0.0f))
			{
				return false;
			}
		}
		break;
	case IMG_CODEC_PNG:
		{
			LLPointer<LLImagePNG> png_image = new LLImagePNG;

			if (!png_image->load(src_filename))
			{
				return false;
			}
			
			if (!png_image->decode(raw_image, 0.0f))
			{
				return false;
			}
		}
		break;
	default:
		return false;
	}

#endif

	raw_image->biasedScaleToPowerOfTwo(1024);
	mRawImagep = raw_image;
	return true;
}
BOOL LLViewerImageList::createUploadFile(const std::string& filename,
										 const std::string& out_filename,
										 const U8 codec)
{
	// First, load the image.
	LLPointer<LLImageRaw> raw_image = new LLImageRaw;
#ifdef LL_DARWIN
	if (!decodeImageQuartz(filename, raw_image))
		return FALSE;
#else
	switch (codec)
	{
		case IMG_CODEC_BMP:
		{
			LLPointer<LLImageBMP> bmp_image = new LLImageBMP;
			
			if (!bmp_image->load(filename))
			{
				return FALSE;
			}
			
			if (!bmp_image->decode(raw_image, 0.0f))
			{
				return FALSE;
			}
		}
			break;
		case IMG_CODEC_TGA:
		{
			LLPointer<LLImageTGA> tga_image = new LLImageTGA;
			
			if (!tga_image->load(filename))
			{
				return FALSE;
			}
			
			if (!tga_image->decode(raw_image))
			{
				return FALSE;
			}
			
			if(	(tga_image->getComponents() != 3) &&
			   (tga_image->getComponents() != 4) )
			{
				tga_image->setLastError( "Image files with less than 3 or more than 4 components are not supported." );
				return FALSE;
			}
		}
			break;
		case IMG_CODEC_JPEG:
		{
			LLPointer<LLImageJPEG> jpeg_image = new LLImageJPEG;
			
			if (!jpeg_image->load(filename))
			{
				return FALSE;
			}
			
			if (!jpeg_image->decode(raw_image, 0.0f))
			{
				return FALSE;
			}
		}
			break;
		case IMG_CODEC_PNG:
		{
			LLPointer<LLImagePNG> png_image = new LLImagePNG;
			
			if (!png_image->load(filename))
			{
				return FALSE;
			}
			
			if (!png_image->decode(raw_image, 0.0f))
			{
				return FALSE;
			}
		}
			break;
		default:
			return FALSE;
	}
#endif
	LLPointer<LLImageJ2C> compressedImage = convertToUploadFile(raw_image);
	
	if( !compressedImage->save(out_filename) )
	{
		llinfos << "Couldn't create output file " << out_filename << llendl;
		return FALSE;
	}
	
	// test to see if the encode and save worked.
	LLPointer<LLImageJ2C> integrity_test = new LLImageJ2C;
	if( !integrity_test->loadAndValidate( out_filename ) )
	{
		llinfos << "Image: " << out_filename << " is corrupt." << llendl;
		return FALSE;
	}
	
	return TRUE;
}