#include "stdafx.h"
#include <graphics.h>
#include <windows.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
#define GRP(x, y) GetRValue(getpixel(x, y))
#define GGP(x, y) GetGValue(getpixel(x, y))
#define GBP(x, y) GetBValue(getpixel(x, y))
#define
#define PI 3.1415926535
#define WIDTH 504
#define HEIGHT 600
#define MODE 0
#define S 2
#define R 2
#define GRAYSV 30
int main(void)
{
void Sepia();
void Comic();
void GBlur();
void Screen();
void SoftLight();
void Multiply();
void ColorDodge();
void MaxFilter();
void MinFilter();
void WoodCarving();
void Pencil();
void Sharpen();
void Soften();
void BgR();
void Diffuse();
void CarveLR();
void ColortoGRay();
void Invert();
void CopyLayer();
void LR();
initgraph(WIDTH, HEIGHT);
setbkcolor(WHITE);
cleardevice();
loadimage(NULL, "F:beauty.jpg");
CopyLayer();
Sepia();
getch();
closegraph();
return 0;
}
void Sepia()
{
int br, bg, bb;
int mr, mg, mb;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
br = GRP(x, y);
bg = GGP(x, y);
bb = GBP(x, y);
mr = (101 * br + 197 * bg + 48 * bb) >> 8;
mg = (89 * br + 176 * bg + 43 * bb) >> 8;
mb = (70 * br + 137 * bg + 34 * bb) >> 8;
putpixel(x, y, RGB(mr > 255? 255 : mr, mg > 255? 255 : mg, mb > 255? 255 : mb ));
}
}
void Comic()
{
void ColortoGRay();
int br, bg, bb;
int mr, mg, mb;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
br = GRP(x, y);
bg = GGP(x, y);
bb = GBP(x, y);
mr = (abs(bg - bb + bg + br) * br) >> 8;
mg = (abs(bb - bg + bb + br) * br) >> 8;
mb = (abs(bb - bg + bb + br) * bg) >> 8;
putpixel(x, y, RGB(mr > 255? 255 : mr, mg > 255? 255 : mg, mb > 255? 255 : mb ));
}
ColortoGRay();
}
void GBlur()
{
int br, bg, bb;
int tem[3][3] = {1, 2, 1, 2, 4, 2, 1, 2, 1};
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
br = bg = bb = 0;
for(int i = -1; i <= 1; i++)
for(int j = -1; j <= 1; j++){
br += GRP(x + i, y + i) * tem[i + 1][j + 1];
bg += GGP(x + i, y + i) * tem[i + 1][j + 1];
bb += GBP(x + i, y + i) * tem[i + 1][j + 1];
}
putpixel(x, y, RGB(br >>4, bg >> 4, bb >> 4));
}
}
void LR()
{
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ )
putpixel(x, y, getpixel(WIDTH + WIDTH - x, y));
}
void SoftLight()
{
int br, bg, bb;
int mr, mg, mb;
int cdr, cdg, cdb;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
br = GRP(x, y);
bg = GGP(x, y);
bb = GBP(x, y);
mr = GRP(x + WIDTH, y);
mg = GGP(x + WIDTH, y);
mb = GBP(x + WIDTH, y);
cdr = (int)(mr > 127.5? br + (255 - br) * (mr - 127.5) / 127.5 * (0.5 - fabs(br - 127.5) / 255) :
br - br * (127.5 - mr) / 127.5 * (0.5 - fabs(br - 127.5) / 255));
cdg = (int)(mg > 127.5? bg + (255 - bg) * (mg - 127.5) / 127.5 * (0.5 - fabs(bg - 127.5) / 255) :
bg - bg * (127.5 - mg) / 127.5 * (0.5 - fabs(bg - 127.5) / 255));
cdb = (int)(mb > 127.5? bb + (255 - bb) * (mb - 127.5) / 127.5 * (0.5 - fabs(bb - 127.5) / 255) :
bb - bb * (127.5 - mb) / 127.5 * (0.5 - fabs(bb - 127.5) / 255));
putpixel(x, y, RGB(cdr, cdg, cdb));
}
}
void OverLay()
{
int br, bg, bb;
int mr, mg, mb;
int cdr, cdg, cdb;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
br = GRP(x, y);
bg = GGP(x, y);
bb = GBP(x, y);
mr = GRP(x + WIDTH, y);
mg = GGP(x + WIDTH, y);
mb = GBP(x + WIDTH, y);
cdr = (int)(br + (mr - 127.5) * (1 - fabs(br - 127.5) / 127.5));
cdg = (int)(bg + (mg - 127.5) * (1 - fabs(bg - 127.5) / 127.5));
cdb = (int)(bb + (mb - 127.5) * (1 - fabs(bb - 127.5) / 127.5));
putpixel(x, y, RGB(cdr, cdg, cdb));
}
}
void Screen()
{
int br, bg, bb;
int mr, mg, mb;
int cdr, cdg, cdb;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
br = GRP(x, y);
bg = GGP(x, y);
bb = GBP(x, y);
mr = GRP(x + WIDTH, y);
mg = GGP(x + WIDTH, y);
mb = GBP(x + WIDTH, y);
cdr = br + mr - br * mr / 255;
cdg = bg + mg - bg * mg / 255;
cdb = bb + mb - bb * mb / 255;
putpixel(x, y, RGB(cdr, cdg, cdb));
}
}
void Multiply()
{
int br, bg, bb;
int mr, mg, mb;
int cdr, cdg, cdb;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
br = GRP(x, y);
bg = GGP(x, y);
bb = GBP(x, y);
mr = GRP(x + WIDTH, y);
mg = GGP(x + WIDTH, y);
mb = GBP(x + WIDTH, y);
cdr = br * mr / 255;
cdg = bg * mg / 255;
cdb = bb * mb / 255;
putpixel(x, y, RGB(cdr, cdg, cdb));
}
}
void ColorDodge()
{
int br, bg, bb;
int mr, mg, mb;
int cdr, cdg, cdb;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
br = GRP(x, y);
bg = GGP(x, y);
bb = GBP(x, y);
mr = GRP(x + WIDTH, y);
mg = GGP(x + WIDTH, y);
mb = GBP(x + WIDTH, y);
cdr = (br + (br * mr) / (256 - mr)) > 255? 255 : (br + (br * mr) / (256 - mr));
cdg = (bg + (bg * mg) / (256 - mg)) > 255? 255 : (bg + (bg * mg) / (256 - mg));
cdb = (bb + (bb * mb) / (256 - mb)) > 255? 255 : (bg + (bg * mg) / (256 - mg));
putpixel(x, y, RGB(cdr, cdg, cdb));
}
}
void MaxFilter()
{
int r, g, b;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
r = GRP(x, y);
g = GGP(x, y);
b = GBP(x, y);
for(int i = -1 * R; i <= R; i++)
for(int j = -1 * R; j <= R; j++){
r = (r < GRP(x + i + WIDTH, y + i)? GRP(x + i + WIDTH, y + i) : r);
g = (g < GGP(x + i + WIDTH, y + i)? GGP(x + i + WIDTH, y + i) : g);
b = (b < GBP(x + i + WIDTH, y + i)? GBP(x + i + WIDTH, y + i) : b);
}
putpixel(x, y, RGB(r, g, b));
}
}
void MinFilter()
{
int r, g, b;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
r = GRP(x, y);
g = GGP(x, y);
b = GBP(x, y);
for(int i = -1 * R; i <= R; i++)
for(int j = -1 * R; j <= R; j++){
r = (r > GRP(x + i + WIDTH, y + i)? GRP(x + i + WIDTH, y + i) : r);
g = (g > GGP(x + i + WIDTH, y + i)? GGP(x + i + WIDTH, y + i) : g);
b = (b > GBP(x + i + WIDTH, y + i)? GBP(x + i + WIDTH, y + i) : b);
}
putpixel(x, y, RGB(r, g, b));
}
}
void WoodCarving()
{
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
if((abs(GRP(x + WIDTH, y) - GRP(x + 1 + WIDTH, y)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x - 1 + WIDTH, y)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x + WIDTH, y + 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x + WIDTH, y - 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x + 1 + WIDTH, y - 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x - 1 + WIDTH, y + 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x - 1 + WIDTH, y - 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x + 1 + WIDTH, y + 1)) > GRAYSV))
putpixel(x, y, WHITE);
else
putpixel(x, y, BLACK);
}
}
void Pencil()
{
void ColortoGRay();
ColortoGRay();
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
if((abs(GRP(x + WIDTH, y) - GRP(x + 1 + WIDTH, y)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x - 1 + WIDTH, y)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x + WIDTH, y + 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x + WIDTH, y - 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x + 1 + WIDTH, y - 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x - 1 + WIDTH, y + 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x - 1 + WIDTH, y - 1)) > GRAYSV) ||
(abs(GRP(x + WIDTH, y) - GRP(x + 1 + WIDTH, y + 1)) > GRAYSV))
putpixel(x, y, BLACK);
else
putpixel(x, y, WHITE);
}
}
void Sharpen()
{
int r, g, b;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
r = g = b = 0;
for(int i = -1 * R; i <= R; i++)
for(int j = -1 * R; j <= R; j++){
if(i == 0 && j == 0) continue;
r += GRP(x + i + WIDTH, y + j);
g += GGP(x + i + WIDTH, y + j);
b += GBP(x + i + WIDTH, y + j);
}
r = GRP(x + WIDTH, y) * (1 + S) - r * S / (4 * R * R + 4 * R);
g = GGP(x + WIDTH, y) * (1 + S) - g * S / (4 * R * R + 4 * R);
b = GBP(x + WIDTH, y) * (1 + S) - b * S / (4 * R * R + 4 * R);
r = r > 255? 255 : r;
g = g > 255? 255 : g;
b = b > 255? 255 : b;
r = r < 0? 0 : r;
g = g < 0? 0 : g;
b = b < 0? 0 : b;
putpixel(x, y, RGB(r, g, b));
}
}
void Soften()
{
int r, g, b;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
r = g = b = 0;
for(int i = -1 * R; i <= R; i++)
for(int j = -1 * R; j <= R; j++){
r += GRP(x + i + WIDTH, y + j);
g += GGP(x + i + WIDTH, y + j);
b += GBP(x + i + WIDTH, y + j);
}
r /= (4 * R * R + 4 * R + 1);
g /= (4 * R * R + 4 * R + 1);
b /= (4 * R * R + 4 * R + 1);
putpixel(x, y, RGB(r, g, b));
}
}
void Diffuse()
{
int x0, y0;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
x0 = abs(rand()) % (2 * R + 1) - R;
y0 = abs(rand()) % (2 * R + 1) - R;
putpixel(x, y, getpixel(x + x0 + WIDTH, y + y0));
}
}
void CarveLR()
{
int r, g, b;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
r = GRP(x, y) - GRP(x + 1, y) + 127;
g = GGP(x, y) - GGP(x + 1, y) + 127;
b = GBP(x, y) - GBP(x + 1, y) + 127;
putpixel(x, y, RGB(r, g, b));
}
}
void CopyLayer()
{
Resize(NULL, WIDTH + WIDTH, HEIGHT);
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
putpixel(x + WIDTH, y, getpixel(x, y));
}
}
void ColortoGRay()
{
for(int x = 0; x < 2 * WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
int gRay = RGBtoGRAY(getpixel(x, y));
putpixel(x, y, RGB(gRay, gRay, gRay));
}
}
void Invert()
{
int IR, IG, IB;
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ ){
IR = 255 - GRP(x, y);
IG = 255 - GGP(x, y);
IB = 255 - GBP(x, y);
putpixel(x, y, RGB(IR, IG, IB));
}
}
void BgR()
{
for(int x = 0; x < WIDTH; x++)
for(int y = 0; y < HEIGHT; y++ )
putpixel(x, y, BGR(getpixel(x, y)));
}