📄 blender.cpp
字号:
//////////////////////////////////////////////////////////////////////
// BLENDER by Tobias Franke (webmaster@cyberhead.de) 2001 //
//////////////////////////////////////////////////////////////////////
// //
// This little program blends together two images by taking a bmp //
// as a sort of heightmap and then mixes two other bmps together! //
// The output is a file called final.bmp... //
// //
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "bitmap.h"
#include "heightmap.h"
bitmap i1, i2, final; //The bitmap objects
heightmap blender; //The blender
//////////////////////////////////////////////////////////////////////
// Thanks to Nicholas Anderson (http://www.flyingislands.co.uk) //
float limit255(float a) //Check for invalid values
{
if(a < 0.0f) {
return 0.0f;
}
else if(a > 255.0f) {
return 255.0f;
}
else {
return a;
}
}
float texture_factor(int h1, float h2, float f) //Check for percentage of color
{
float t;
t = (f - (float)abs(h1 - (int)h2)) / f;
if(t < 0.0f) t = 0.0f;
else if(t > 1.0f) t = 1.0f;
return t;
}
// These two functions were taken and sligthly modified from //
// N.Andersons LOD Program //
//////////////////////////////////////////////////////////////////////
bool init()
{
if (!i1.load("i1.bmp") || !i2.load("i2.bmp") || !blender.load("blender.bmp")) //Load all images
{ printf ("couldn't load all files...\n"); return false; }
if ((i1.getheight() != i2.getheight()) || (i1.getwidth() != i2.getwidth())) //Check if i1 and i2 are of the same size
{ printf ("images aren't of the same size...\n"); return false; }
if ((i1.getheight() != blender.getheight()) || (i1.getwidth() != blender.getwidth())) //Check if blender is of the same size as i1 and i2
{ printf ("images aren't the same size as blender...\n"); return false; }
final.create (i1.getwidth(), i1.getheight()); //Create new bitmap
printf ("all images loaded, ready...\n");
return true;
}
bool blend()
{
float h1,f0, f1; //h1 is heigtmap value at position x/z, f0 and f1 are percentage of color resulting due to the heightmap value stored in h1
unsigned char r[2], g[2], b[2], new_r, new_g, new_b; //Store color at pixel x/z of both images (i1 and i2), store the new calculated values
for (int z=0; z<i1.getheight(); z++) //Go through all pixels
for (int x=0; x<i2.getwidth(); x++)
{
h1 = limit255(blender.hmap[x + (z*blender.getwidth())]);//Get proper heightmap value at x/z
f0 = texture_factor(0, h1, 256); //Get percentage of color of pixel x/z from bitmap1, if height is zero (black) then bitmap1 receives 100%
f1 = texture_factor(256, h1, 256); //Get percentage of color of pixel x/z from bitmap2, if height is 256 (white) then bitmap2 receives 100%
i1.getcolor(x, z, &r[0], &g[0], &b[0]); //Get color of pixel x/z from bitmap1
i2.getcolor(x, z, &r[1], &g[1], &b[1]); //Get color of pixel x/z from bitmap2
new_r = (unsigned)limit255((r[0] * f0) + (r[1] * f1)); //Calculate new r value for the pixel to be written
new_g = (unsigned)limit255((g[0] * f0) + (g[1] * f1)); //Calculate new g value for the pixel to be written
new_b = (unsigned)limit255((b[0] * f0) + (b[1] * f1)); //Calculate new b value for the pixel to be written
final.setcolor(x, z, new_r, new_g, new_b); //Set new pixel color in final
}
if (!final.save ("final.bmp")) //Save bitmap
{ printf ("couldn't save final image...\n"); return false; }
else
{ printf ("images blended, see final.bmp...\n"); return true; }
}
void main()
{
printf ("Blender v0.02 by Tobias Franke\n\n");
printf ("blending...\n");
if (init())
{ if (!blend()) printf ("terminating(error)...\n"); }
else
printf ("terminating(done)...\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -