FreeImageベース、画像回転、画像フィルタリングを使用
17047 ワード
1. freeImage , ( , (rgb), )
2.
3.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "FreeImage.h"
#include <math.h>
#include <time.h>
#pragma comment(lib, "FreeImage.lib")
#define PI 3.141592645
// 4
class byte4
{
public:
BYTE r; // red
BYTE g; // green
BYTE b; // blue
BYTE a; // alpha
};
static int maskHeight = 3; //
static int maskWidth = 3; //
//
float mask[3][3] =
{
{0.0751 , 0.1238 , 0.0751},
{0.1238 , 0.2042 , 0.1238},
{0.0751 , 0.1238 , 0.0751}};
//
void cpu_filter(byte4** inbuf, byte4** outbuf, int w, int h)
{
for(int row = 1; row < h - 1; row ++)
{
for(int col = 1; col < w - 1; col ++)
{
outbuf[row][col].r = 0;
outbuf[row][col].g = 0;
outbuf[row][col].b = 0;
outbuf[row][col].a = 0;
float t1 = 0, t2 = 0, t3 = 0, t4 = 0;
for(int i = 0; i < maskHeight; i ++)
for(int j = 0; j < maskWidth; j++)
{
t1 += inbuf[row + i - 1][col + j - 1].r * mask[i][j];
t2 += inbuf[row + i - 1][col + j - 1].g * mask[i][j];
t3 += inbuf[row + i - 1][col + j - 1].b * mask[i][j];
t4 += inbuf[row + i - 1][col + j - 1].a * mask[i][j];
}
outbuf[row][col].r = (int)t1;
outbuf[row][col].g = (int)t2;
outbuf[row][col].b = (int)t3;
outbuf[row][col].a = (int)t4;
}
}
}
//CPU
void cpu_rotate(byte4** inbuf, byte4** outbuf, int w, int h, float angle)
{
if(w % 2 == 0) w --;
if(h % 2 == 0) h --;
int i, j;
int xc = w/2;
int yc = h/2;
float cosTheta = cos(PI * angle/180);
float sinTheta = sin(PI * angle/180);
for(i = 0; i < h; i++)
{
for(j=0; j< w; j++)
{
int xpos = (int)( (i-xc)*cosTheta + (j-yc)*sinTheta + xc );
int ypos = (int)( -(i-xc)*sinTheta + (j-yc)*cosTheta + yc );
if(xpos>=0 && ypos>=0 && xpos<h && ypos < w )
outbuf[xpos][ypos] = inbuf[i][j];
}
}
}
void main(int argc, char* argv)
{
char * imageFile = "lenna.PNG";
//
FreeImage_Initialise(TRUE);
//
FIBITMAP * bitmap = FreeImage_Load(FIF_PNG, imageFile, PNG_DEFAULT);
if(bitmap) printf("Image Load successfully!
");
// ( )
int width = FreeImage_GetWidth(bitmap);
int height = FreeImage_GetHeight(bitmap);
//
/************************************************************************/
/* JPEG : 3 */
/* JPEG : 4 */
/************************************************************************/
int bytespp = FreeImage_GetLine(bitmap)/ width;
printf("Width:%d\t Height:%d\t :%d
", width, height, bytespp);
// int, float, byte4
printf("int: %d
float: %d
byte4: %d
", sizeof(int), sizeof(float), sizeof(byte4));
//
byte4 **matrix;
// 2
matrix = (byte4 **)calloc(height, sizeof(byte4*));
for(int i = 0; i < height; i++)
{
matrix[i] = (byte4*)calloc(width, sizeof(byte4));
}
//
byte4 ** matrix_dst;
// 2
matrix_dst = (byte4 **)calloc(height, sizeof(byte4*));
for(int i = 0; i < height; i++)
{
matrix_dst[i] = (byte4*)calloc(width, sizeof(byte4));
}
if(matrix && matrix_dst)
printf(" !
");
for(unsigned y = 0; y < height; y++)
{
BYTE *bitsLine = FreeImage_GetScanLine(bitmap, y);
for(unsigned x = 0; x < width; x++)
{
//
matrix[y][x].r = bitsLine[FI_RGBA_RED] ;
matrix[y][x].g = bitsLine[FI_RGBA_GREEN] ;
matrix[y][x].b = bitsLine[FI_RGBA_BLUE] ;
matrix[y][x].a = bitsLine[FI_RGBA_ALPHA] ;
bitsLine += bytespp;
}
}
// ,
int start = clock();
// cpu_rotate(matrix, matrix_dst, width, height, 90); cpu_filter(matrix, matrix_dst, width, height); printf("Time elapsed: %d ms
", (clock() - start)); // FreeImage
FIBITMAP * dst = bitmap;
for(unsigned y = 0; y < height; y++)
{
BYTE *bitsLine = FreeImage_GetScanLine(dst, y);
for(unsigned x = 0; x < width; x++)
{
//
bitsLine[FI_RGBA_RED] = matrix_dst[y][x].r ;
bitsLine[FI_RGBA_GREEN] = matrix_dst[y][x].g ;
bitsLine[FI_RGBA_BLUE] = matrix_dst[y][x].b ;
bitsLine[FI_RGBA_ALPHA] = matrix_dst[y][x].a ;
bitsLine += bytespp;
}
}
//
FreeImage_Save(FIF_PNG, dst, "dst.png", PNG_DEFAULT);
getchar();
}