コード例 #1
0
/* saveTargaColorMatrix()
 * ----------------------
 * Save the specified ColorMatrix as the specified targa file 
 * @param targaFilename - name of the targa file to save as (including .tga)
 * @return - true on successful save, false on failure 
 */
bool ImageProcessing::saveTargaColorMatrix(const string& targaFilename, 
										   const ColorMatrix& source) 
{
	// Try to load a blank targa for the output
	TargaImage* dest = TargaImage::blankImage(source.cols(), source.rows());
	if( dest == nullptr ) {
		cout << "Error: unable to create blank image for output." << endl;
		return false;
	}

	// Setup constants for the output image
	const unsigned int rows   = dest->height();
	const unsigned int cols   = dest->width();
	unsigned char*	   pixels = dest->pixels();

	// Write the source ColorMatrix to the dest TargaImage
	for(unsigned int row = 0, i = 0; row < rows; ++row)
	for(unsigned int col = 0; col < cols; ++col, ++i)
	{
		const Color color(source(row,col));
		pixels[i*4 + 0] = color.r();
		pixels[i*4 + 1] = color.g();
		pixels[i*4 + 2] = color.b();
		pixels[i*4 + 3] = color.a();
	}

	// Save the new dest TargaImage
	bool success = true;
	if( dest->write(targaFilename.c_str()) != 1 ) {
		cout << "Error: unable to save " << targaFilename 
			 << ", " << tga_error_string(tga_get_last_error()) 
			 << endl;
		success = false;
	} 

	// Cleanup the new TargaImage
	delete dest;
	
	return success;
}
コード例 #2
0
/* loadTargaColorMatrix()
 * ----------------------
 * Load the specified targa file into a ColorMatrix object
 * @param targaFilename - name of the targa file to open (including .tga)
 * @return - pixel data from the targa file in a ColorMatrix object
 */
ColorMatrix ImageProcessing::loadTargaColorMatrix(const string& targaFilename)
{
	// Try to load the targa image
	TargaImage* image = TargaImage::readImage(targaFilename.c_str());
	if( image == nullptr ) { 
		stringstream ss;
		ss << "Error: unable to open " << targaFilename << endl;
		throw FailedToLoad(ss.str());
	} 

	// Setup constants from the input image
	const unsigned int   rows   = image->height();
	const unsigned int   cols   = image->width();
	const unsigned char* pixels = image->pixels();
	if( pixels == nullptr ) { 
		stringstream ss;
		ss << "Error: invalid pixel data in " << targaFilename << endl;
		throw BadPixelData(ss.str());	
	}

	// Generate the color matrix based on the image pixels
	ColorMatrix colors(rows, cols);
	for(unsigned int row = 0, i = 0; row < rows; ++row)
	for(unsigned int col = 0; col < cols; ++col, ++i)
	{
		colors(row,col) = Color(pixels[i*4 + 0],	// red
								pixels[i*4 + 1],	// green
								pixels[i*4 + 2],	// blue
								pixels[i*4 + 3]);	// alpha
	}

	// Cleanup the TargaImage
	delete image;

	return colors;
}