Česky
Kamil Dudka

GED 2006 (C++)

File detail

Name:Downloadpixeling.cc [Download]
Location: ged2006 > src
Size:2.0 KB
Last modification:2007-08-29 02:16

Source code

/*
 * File: pixeling.cc - Plugin Pixeling
 * Project: GED - bitmap editor (ICP)
 * Author: Lukas Hefka, xhefka00
 * Team: xdudka00, xfilak01, xhefka00, xhradi08
 * Created: 2006-04-04
 */
 
#include <string>
#include <vector>
#include <iostream>
#include "gedplugin.h"
 
using std::string;
using std::vector;
 
/**
 * Return minumum from a and b.
 */
int min (int a, int b) {
	return (a<b) ? a:b;
}
 
/**
 * Plugin core function
 * \param fb Reference to frambe buffer to work with.
 * \return Return error-code, 0 if success.
 */
int core (FrameBuffer &fb, vector<string>) {
	// Selected area range
	const int xMin= fb.select().at.x;
	const int yMin= fb.select().at.y;
	const int xMax= xMin + fb.select().size.width;
	const int yMax= yMin + fb.select().size.height;
 
	// Size of block
	const BoxSize block (8, 8);
 
	for (int y= yMin; y< yMax; y+= block.height)
		for (int x= xMin; x< xMax; x+= block.width)
		{
			// Block range
			const int ixMax= min (x+block.width, xMax);
			const int iyMax= min (y+block.height, yMax);
 
			// Count average
			unsigned sumRed= 0;
			unsigned sumGreen= 0;
			unsigned sumBlue= 0;
			for (int iy= y; iy< iyMax; iy++)
				for (int ix= x; ix< ixMax; ix++)
				{
					sumRed+= fb [iy][ix].red;
					sumGreen+= fb [iy][ix].green;
					sumBlue+= fb [iy][ix].blue;
				}
 
			// Average pixel
			const int nPixels= (ixMax-x) * (iyMax-y);
			const Pixel avgPixel = {
				sumRed / nPixels,
				sumGreen / nPixels,
				sumBlue / nPixels,
				UCHAR_MAX		// Alpha
			};
 
			// Fill block
			for (int iy= y; iy< iyMax; iy++)
				for (int ix= x; ix< ixMax; ix++)
					fb [iy][ix] = avgPixel;
		}
 
	return 0;	// success
}
 
// plugin main() - read the manual if available
int main (int argc, char *argv[]) {
	using namespace GedPlugin;
 
	// Create plugin object
	Plugin myPlugin (argc, argv);
 
	// Set object properties
	PluginDescription desc;
	desc.type = TYPE_FILTER;	// Plugin category: filter
	desc.name = "Pixeling";		// Plugin name
	desc.pCore = core;		// Plugin core function (!!)
	myPlugin.set (desc);
 
	// Execute plugin (if needed)
	return myPlugin.exec();	
}