#include <iostream> #include <vector> // ピクセルを表す構造体(RGB) struct Pixel { unsigned char r; unsigned char g; unsigned char b; }; // バイリニア補間関数 Pixel BilinearInterpolate( const Pixel& top_left, const Pixel& top_right, const Pixel& bottom_left, const Pixel& bottom_right, double x_ratio, double y_ratio) { Pixel result; result.r = (unsigned char)( top_left.r * (1 - x_ratio) * (1 - y_ratio) + top_right.r * x_ratio * (1 - y_ratio) + bottom_left.r * y_ratio * (1 - x_ratio) + bottom_right.r * x_ratio * y_ratio); result.g = (unsigned char)( top_left.g * (1 - x_ratio) * (1 - y_ratio) + top_right.g * x_ratio * (1 - y_ratio) + bottom_left.g * y_ratio * (1 - x_ratio) + bottom_right.g * x_ratio * y_ratio); result.b = (unsigned char)( top_left.b * (1 - x_ratio) * (1 - y_ratio) + top_right.b * x_ratio * (1 - y_ratio) + bottom_left.b * y_ratio * (1 - x_ratio) + bottom_right.b * x_ratio * y_ratio); return result; } // メイン関数 int main() { // 仮定:srcImageは512x512、dstImageは1024x1024の大きさ const int srcWidth = 512, srcHeight = 512; const int dstWidth = 1024, dstHeight = 1024; std::vector<std::vector<Pixel>> srcImage(srcHeight, std::vector<Pixel>(srcWidth)); std::vector<std::vector<Pixel>> dstImage(dstHeight, std::vector<Pixel>(dstWidth)); // ここで、srcImageに画像データを読み込む // リサイズ処理 for (int y = 0; y < dstHeight; ++y) { for (int x = 0; x < dstWidth; ++x) { double gx = ((double)x) / dstWidth * (srcWidth - 1); double gy = ((double)y) / dstHeight * (srcHeight - 1); int gxi = (int)gx; int gyi = (int)gy; Pixel top_left = srcImage[gyi][gxi]; Pixel top_right = srcImage[gyi][gxi + 1]; Pixel bottom_left = srcImage[gyi + 1][gxi]; Pixel bottom_right = srcImage[gyi + 1][gxi + 1]; double x_ratio = gx - gxi; double y_ratio = gy - gyi; dstImage[y][x] = BilinearInterpolate(top_left, top_right, bottom_left, bottom_right, x_ratio, y_ratio); } } // ここで、dstImageを画像ファイルに保存する return 0; }