/* sobel operatorで輝度のみ */
#include <stdlib.h>
#include <math.h>
char * sobel_mask( char * src, int width, int height, int pitch)
{
const int sobel_h[ ] = { 1 , 0 , - 1 , 2 , 0 , - 2 , 1 , 0 , - 1 } ;
const int sobel_v[ ] = { 1 , 2 , 1 , 0 , 0 , 0 , - 1 , - 2 , - 1 } ;
char * dst
= calloc ( width
* height
, 1 ) ; if ( ! dst) {
return NULL;
}
char * srcp = src;
char * dstp = dst;
/* 画像端の処理は省略 */
for ( int y = 1 ; y < width - 1 ; y++ ) {
char * top = srcp;
srcp += pitch;
char * bottom = srcp + pitch;
dstp += width;
for ( int x = 1 ; x < width - 1 ; x++ ) {
char array[ ] = {
top[ x - 1 ] , top[ x] , top[ x + 1 ] ,
srcp[ x - 1 ] , srcp[ x] , srcp[ x + 1 ] ,
bottom[ x - 1 ] , bottom[ x] , bottom[ x + 1 ]
}
int val_h = 0 ;
int val_v = 0 ;
for ( int i = 0 ; i < 9 ; i++ ) {
val_h += array[ i] * sobel_h[ i] ;
val_v += array[ i] * sobel_v[ i] ;
}
dstp
[ x
] = ( char ) ( sqrt ( ( double ) ( val_h
* val_h
+ val_v
* val_v
) ) / 8 ) ; }
}
return dst;
}
Lyogc29iZWwgb3BlcmF0b3LjgafovJ3luqbjga7jgb8gKi8KCiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCmNoYXIgKnNvYmVsX21hc2soY2hhciAqc3JjLCBpbnQgd2lkdGgsIGludCBoZWlnaHQsIGludCBwaXRjaCkKewogICAgY29uc3QgaW50IHNvYmVsX2hbXSA9IHsxLCAwLCAtMSwgMiwgMCwgLTIsIDEsIDAsIC0xfTsKICAgIGNvbnN0IGludCBzb2JlbF92W10gPSB7MSwgMiwgMSwgMCwgMCwgMCwgLTEsIC0yLCAtMX07CiAgICAKICAgIGNoYXIgKmRzdCA9IGNhbGxvYyh3aWR0aCAqIGhlaWdodCwgMSk7CiAgICBpZiAoIWRzdCkgewogICAgICAgIHJldHVybiBOVUxMOwogICAgfQoKICAgIGNoYXIgKnNyY3AgPSBzcmM7CiAgICBjaGFyICpkc3RwID0gZHN0OwogICAgLyog55S75YOP56uv44Gu5Yem55CG44Gv55yB55WlICovCiAgICBmb3IgKGludCB5ID0gMTsgeSA8IHdpZHRoIC0gMTsgeSsrKSB7CiAgICAgICAgY2hhciAqdG9wID0gc3JjcDsKICAgICAgICBzcmNwICs9IHBpdGNoOwogICAgICAgIGNoYXIgKmJvdHRvbSA9IHNyY3AgKyBwaXRjaDsKICAgICAgICBkc3RwICs9IHdpZHRoOwogICAgICAgIGZvciAoaW50IHggPSAxOyB4IDwgd2lkdGggLTE7IHgrKykgewogICAgICAgICAgICBjaGFyIGFycmF5W10gPSB7CiAgICAgICAgICAgICAgICB0b3BbeCAtIDFdLCB0b3BbeF0sIHRvcFt4ICsgMV0sCiAgICAgICAgICAgICAgICBzcmNwW3ggLSAxXSwgc3JjcFt4XSwgc3JjcFt4ICsgMV0sCiAgICAgICAgICAgICAgICBib3R0b21beCAtIDFdLCBib3R0b21beF0sIGJvdHRvbVt4ICsgMV0KICAgICAgICAgICAgfQogICAgICAgICAgICBpbnQgdmFsX2ggPSAwOwogICAgICAgICAgICBpbnQgdmFsX3YgPSAwOwogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IDk7IGkrKykgewogICAgICAgICAgICAgICAgdmFsX2ggKz0gYXJyYXlbaV0gKiBzb2JlbF9oW2ldOwogICAgICAgICAgICAgICAgdmFsX3YgKz0gYXJyYXlbaV0gKiBzb2JlbF92W2ldOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGRzdHBbeF0gPSAoY2hhcikoc3FydCgoZG91YmxlKSh2YWxfaCAqIHZhbF9oICsgdmFsX3YgKiB2YWxfdikpIC8gOCk7CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gZHN0Owp9