// Save a raw image instance into a file
bool save_image(const std::string &dest_filename, LLPointer<LLImageRaw> raw_image, int blocks_size, int precincts_size, int levels, bool reversible, bool output_stats)
{
	LLPointer<LLImageFormatted> image = create_image(dest_filename);
	
	// Set the image codestream parameters on output in the case of a j2c image
	if (image->getCodec() == IMG_CODEC_J2C)
	{
		// That method doesn't exist (and likely, doesn't make sense) for any other image file format
		// hence the required cryptic cast.
		if ((blocks_size != -1) || (precincts_size != -1) || (levels != 0))
		{
			((LLImageJ2C*)(image.get()))->initEncode(*raw_image, blocks_size, precincts_size, levels);
		}
		((LLImageJ2C*)(image.get()))->setReversible(reversible);
	}
	
	if (!image->encode(raw_image, 0.0f))
	{
		return false;
	}
	
	if (output_stats)
	{
		output_image_stats(image, dest_filename);
	}

	return image->save(dest_filename);
}
BOOL LLViewerTextureList::createUploadFile(const std::string& filename,
										 const std::string& out_filename,
										 const U8 codec)
{	
	// Load the image
	LLPointer<LLImageFormatted> image = LLImageFormatted::createFromType(codec);
	if (image.isNull())
	{
		image->setLastError("Couldn't open the image to be uploaded.");
		return FALSE;
	}	
	if (!image->load(filename))
	{
		image->setLastError("Couldn't load the image to be uploaded.");
		return FALSE;
	}
	// Decompress or expand it in a raw image structure
	LLPointer<LLImageRaw> raw_image = new LLImageRaw;
	if (!image->decode(raw_image, 0.0f))
	{
		image->setLastError("Couldn't decode the image to be uploaded.");
		return FALSE;
	}
	// Check the image constraints
	if ((image->getComponents() != 3) && (image->getComponents() != 4))
	{
		image->setLastError("Image files with less than 3 or more than 4 components are not supported.");
		return FALSE;
	}
	// Convert to j2c (JPEG2000) and save the file locally
	LLPointer<LLImageJ2C> compressedImage = convertToUploadFile(raw_image);	
	if (compressedImage.isNull())
	{
		image->setLastError("Couldn't convert the image to jpeg2000.");
		llinfos << "Couldn't convert to j2c, file : " << filename << llendl;
		return FALSE;
	}
	if (!compressedImage->save(out_filename))
	{
		image->setLastError("Couldn't create the jpeg2000 image for upload.");
		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 ))
	{
		image->setLastError("The created jpeg2000 image is corrupt.");
		llinfos << "Image file : " << out_filename << " is corrupt" << llendl;
		return FALSE;
	}
	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;
	
	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;
	}
	
	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;
}