/* GDKSCale.c * Copyright(C) 2007 Akiyuki Kishiki */ #include #include #include enum{ SCALE = 3, MAX_DIPTH = 255 }; int gdImageD8ColorAllocate( gdImagePtr img, int d8_color ) { int red, grn, blu; red = ( ( d8_color >> 2 ) % 2 ) * MAX_DIPTH; grn = ( ( d8_color >> 1 ) % 2 ) * MAX_DIPTH; blu = ( ( d8_color >> 0 ) % 2 ) * MAX_DIPTH; return gdImageColorAllocate( img, red, grn, blu ); } int gdRScaleGetXel2( direct, center, front1, front2, back ) { if( front1 == center && front2 == center && back == center ) return direct; else if( front1 == center || front2 == center || back == direct ) return center; else return direct; } void gdKScaleGetXel( gdImagePtr dst, gdImagePtr src, int x, int y, int bg_color ) { int center, over, under, left, right, o_l, o_r, u_l, u_r; int high1, high2, high3, mid1, mid2, mid3, low1, low2, low3; center = gdImageGetPixel( src, x, y ); over = ( y > 0 ) ? gdImageGetPixel( src, x, y - 1 ) : bg_color; under = ( y < gdImageSY( src ) - 1 ) ? gdImageGetPixel( src, x, y + 1 ) : bg_color; left = ( x > 0 ) ? gdImageGetPixel( src, x - 1, y ) : bg_color; right = ( x < gdImageSX( src ) - 1 ) ? gdImageGetPixel( src, x + 1, y ) : bg_color; o_l = ( y > 0 || x > 0 ) ? gdImageGetPixel( src, x - 1, y - 1 ) : bg_color; o_r = ( y > 0 || x < gdImageSX( src ) - 1 ) ? gdImageGetPixel( src, x + 1, y - 1 ) : bg_color; u_l = ( y < gdImageSY( src ) - 1 || x > 0 ) ? gdImageGetPixel( src, x - 1, y + 1 ) : bg_color; u_r = ( y < gdImageSY( src ) - 1 || x < gdImageSX( src ) - 1 ) ? gdImageGetPixel( src, x + 1, y + 1 ) : bg_color; if( over == under || left == right ) high1 = high2 = high3 = mid1 = mid2 = mid3 = low1 = low2 = low3 = center; else{ high1 = ( over == left ) ? over : center; if( over == left ) high2 = gdRScaleGetXel2( over, center, o_r, o_l, u_l ); else if( over == right ) high2 = gdRScaleGetXel2( over, center, o_l, o_r, u_r ); else high2 = center; high3 = ( over == right ) ? over : center; if( over == left ) mid1 = gdRScaleGetXel2( left, center, u_l, o_l, o_r ); else if( under == left ) mid1 = gdRScaleGetXel2( left, center, o_l, u_l, u_r ); else mid1 = center; mid2 = center; if( over == right ) mid3 = gdRScaleGetXel2( right, center, u_r, o_r, o_l ); else if( under == right ) mid3 = gdRScaleGetXel2( right, center, o_r, u_r, u_l ); else mid3 = center; low1 = ( under == left ) ? under : center; if( under == left ) low2 = gdRScaleGetXel2( under, center, u_r, u_l, o_l ); else if( under == right ) low2 = gdRScaleGetXel2( under, center, u_l, u_r, o_r ); else low2 = center; low3 = ( under == right ) ? under : center; } gdImageSetPixel( dst, x * SCALE, y * SCALE, high1 ); gdImageSetPixel( dst, x * SCALE + 1, y * SCALE, high2 ); gdImageSetPixel( dst, x * SCALE + 2, y * SCALE, high3 ); gdImageSetPixel( dst, x * SCALE, y * SCALE + 1, mid1 ); gdImageSetPixel( dst, x * SCALE + 1, y * SCALE + 1, mid2 ); gdImageSetPixel( dst, x * SCALE + 2, y * SCALE + 1, mid3 ); gdImageSetPixel( dst, x * SCALE, y * SCALE + 2, low1 ); gdImageSetPixel( dst, x * SCALE + 1, y * SCALE + 2, low2 ); gdImageSetPixel( dst, x * SCALE + 2, y * SCALE + 2, low3 ); } gdImagePtr gdKScale( gdImagePtr src, int bg_color_d8 ) { gdImagePtr tmp, dst; int width, height, background; int x, y; width = gdImageSX( src ); height = gdImageSY( src ); tmp = gdImageCreateTrueColor( width, height ); dst = gdImageCreateTrueColor( width * SCALE, height * SCALE ); background = gdImageD8ColorAllocate( tmp, bg_color_d8 ); gdImageFill( tmp, 0, 0, background ); gdImageCopy( tmp, src, 0, 0, 0, 0, width, height ); for( y = 0; y < height ; y++ ) for( x = 0; x < width ; x++ ) gdKScaleGetXel( dst, tmp, x, y, background ); gdImageDestroy( tmp ); return dst; } int main( int argc, char *argv[] ) { gdImagePtr im_src, im_dst; int bg_color_d8; /* 0(000: black) - 7(111: white) */ if( argc > 0 ){ bg_color_d8 = atoi( argv[1] ); if( bg_color_d8 < 0 || bg_color_d8 > 7 ) bg_color_d8 = 0; } else bg_color_d8 = 0; im_src = gdImageCreateFromPng( stdin ); if( im_src == NULL ){ fprintf( stderr, "not PNG File\n" ); return 1; } im_dst = gdKScale( im_src, bg_color_d8 ); gdImagePng( im_dst, stdout ); fflush( stdout ); gdImageDestroy( im_src ); gdImageDestroy( im_dst ); }