// グレイスケール画像を組織的ディザ法によって2値化した画像を作成するプログラム // 2値化した画像は,元画像と同じ大きさとなる。 // // プログラム名:dither_org1.cpp #include "eps-header.hh" // 元画像(src)を組織的ディザ法によって2値化した処理済画像(dst)を作成する関数 // 処理済画像は,元画像の縦横とも同じ大きさとなる void dither_org1( class image& src, // 元画像クラス(入力) class image& dst, // 処理済画像クラス(出力) int dither_mat[4][4], // ディザ行列(4×4)(入力) char * dither_name) // ディザパターン名(入力) { if (src.p == NULL) { printf("元画像がありません。"); return; } printf("\n組織的ディザ法によって2値化します。\n"); printf("2値化した画像は,元画像と同じ大きさとなります。\n"); int dither_size = 4; // ディザパターンの大きさ printf("%s ディザ行列:%d × %d\n", dither_name, dither_size, dither_size); for (int iy_f = 0; iy_f < dither_size; iy_f ++) { printf(" "); for (int ix_f = 0; ix_f < dither_size; ix_f ++) { printf("+----------"); } printf("+\n"); printf(" "); for (int ix_f = 0; ix_f < dither_size; ix_f ++) { printf("|%9d ", dither_mat[iy_f][ix_f]); } printf("|\n"); } printf(" "); for (int ix_f = 0; ix_f < dither_size; ix_f ++) { printf("+----------"); } printf("+\n"); int nr_lines = src.get_nr_lines(); // 元画像のライン数 (縦の画素数) int nr_pixels = src.get_nr_pixels(); // 元画像のピクセル数 (横の画素数) dst.gmake(nr_lines, nr_pixels); // 処理済画像の配列を確保 // 画素値はゼロにクリアされる。 // 組織的ディザ画像の作成 for (int iy = 0; iy < nr_lines; iy += dither_size) { for (int ix = 0; ix < nr_pixels; ix += dither_size) { int ix_dst = dither_size * ix; int iy_dst = dither_size * iy; int a = 0; // ディザ化する領域の輝度値を平均値とする。 for (int iy_d = 0; iy_d < dither_size; iy_d ++) { for (int ix_d = 0; ix_d < dither_size; ix_d ++) { a += src.p[iy + iy_d][ix + ix_d]; } } a = (int)((double)a / (dither_size * dither_size)); a = (a * dither_size * dither_size + 1) / MAX_GRAY_LEVEL; for (int iy_d = 0; iy_d < dither_size; iy_d ++) { for (int ix_d = 0; ix_d < dither_size; ix_d ++) { if (a > dither_mat[iy_d][ix_d]) { dst.p[iy + iy_d][ix + ix_d] = MAX_GRAY_LEVEL; } } } } } } // プログラムの開始位置 int main() { class image src; // 元画像クラスを宣言 class image dst; // 処理済画像クラスを宣言 int dither_bayer[4][4] = { // Bayer型のディザ行列 { 0, 8, 2, 10}, {12, 4, 14, 6}, { 3, 11, 1, 9}, {15, 7, 13, 5} }; char * name_bayer = "Bayer型"; int dither_uzu[4][4] = { // 渦巻き型のディザ行列 { 6, 7, 8, 9}, { 5, 0, 1, 10}, { 4, 3, 2, 11}, {15, 14, 13, 12} }; char * name_uzu = "渦巻き型"; int dither_ami[4][4] = { // 網点型のディザ行列 {11, 4, 6, 9}, {12, 0, 2, 14}, { 7, 8, 10, 5}, { 3, 15, 13, 1} }; char * name_ami = "網点型"; src.gload(); // 元画像をBMPファイルから読み込む // BMPファイルを指定したい場合は, // src.gload("パス付きBMPファイル名"); // とする。 // 元画像(src)を組織的ディザ法によって2値化した処理済画像(dst)を作成する。 // 処理済画像は,元画像の縦横とも同じ大きさとなる dither_org1( src, // 元画像クラス(入力) dst, // 処理済画像クラス(出力) dither_bayer, // ディザ行列(4×4)(入力) name_bayer); // ディザパターン名(入力) dst.gsave(); // 処理済画像をBMPファイルに保存する // BMPファイルを指定したい場合は, // dst.gsave("パス付きBMPファイル名"); // とする。 }