/* GDKSCale.c * Copyright(C) 2007 Akiyuki Kishiki */ #include #include #include enum{ SCALE = 2, 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 ); } void gdKScaleGetXel( gdImagePtr dst, gdImagePtr src, int x, int y, int bg_color ) { int center, over, under, left, right; int high1, high2, low1, low2; 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; if( over == under || left == right ) high1 = high2 = low1 = low2 = center; else{ high1 = ( over == left ) ? over : center; high2 = ( over == right ) ? over : center; low1 = ( under == left ) ? under : center; low2 = ( under == right ) ? under : center; } gdImageSetPixel( dst, x * SCALE, y * SCALE, high1 ); gdImageSetPixel( dst, x * SCALE + 1, y * SCALE, high2 ); gdImageSetPixel( dst, x * SCALE, y * SCALE + 1, low1 ); gdImageSetPixel( dst, x * SCALE + 1, y * SCALE + 1, low2 ); } 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 ); }