/* 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 < height - 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;
}
Lyogc29iZWwgb3BlcmF0b3LjgafovJ3luqbjga7jgb8gKi8KCiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCmNoYXIgKnNvYmVsX21hc2soY2hhciAqc3JjLCBpbnQgd2lkdGgsIGludCBoZWlnaHQsIGludCBwaXRjaCkKewogICAgY29uc3QgaW50IHNvYmVsX2hbXSA9IHsxLCAwLCAtMSwgMiwgMCwgLTIsIDEsIDAsIC0xfTsKICAgIGNvbnN0IGludCBzb2JlbF92W10gPSB7MSwgMiwgMSwgMCwgMCwgMCwgLTEsIC0yLCAtMX07CiAgICAKICAgIGNoYXIgKmRzdCA9IGNhbGxvYyh3aWR0aCAqIGhlaWdodCwgMSk7CiAgICBpZiAoIWRzdCkgewogICAgICAgIHJldHVybiBOVUxMOwogICAgfQoKICAgIGNoYXIgKnNyY3AgPSBzcmM7CiAgICBjaGFyICpkc3RwID0gZHN0OwogICAgLyog55S75YOP56uv44Gu5Yem55CG44Gv55yB55WlICovCiAgICBmb3IgKGludCB5ID0gMTsgeSA8IGhlaWdodCAtIDE7IHkrKykgewogICAgICAgIGNoYXIgKnRvcCA9IHNyY3A7CiAgICAgICAgc3JjcCArPSBwaXRjaDsKICAgICAgICBjaGFyICpib3R0b20gPSBzcmNwICsgcGl0Y2g7CiAgICAgICAgZHN0cCArPSB3aWR0aDsKICAgICAgICBmb3IgKGludCB4ID0gMTsgeCA8IHdpZHRoIC0gMTsgeCsrKSB7CiAgICAgICAgICAgIGNoYXIgYXJyYXlbXSA9IHsKICAgICAgICAgICAgICAgIHRvcFt4IC0gMV0sIHRvcFt4XSwgdG9wW3ggKyAxXSwKICAgICAgICAgICAgICAgIHNyY3BbeCAtIDFdLCBzcmNwW3hdLCBzcmNwW3ggKyAxXSwKICAgICAgICAgICAgICAgIGJvdHRvbVt4IC0gMV0sIGJvdHRvbVt4XSwgYm90dG9tW3ggKyAxXQogICAgICAgICAgICB9CiAgICAgICAgICAgIGludCB2YWxfaCA9IDA7CiAgICAgICAgICAgIGludCB2YWxfdiA9IDA7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgOTsgaSsrKSB7CiAgICAgICAgICAgICAgICB2YWxfaCArPSBhcnJheVtpXSAqIHNvYmVsX2hbaV07CiAgICAgICAgICAgICAgICB2YWxfdiArPSBhcnJheVtpXSAqIHNvYmVsX3ZbaV07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZHN0cFt4XSA9IChjaGFyKShzcXJ0KChkb3VibGUpKHZhbF9oICogdmFsX2ggKyB2YWxfdiAqIHZhbF92KSkgLyA4KTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIHJldHVybiBkc3Q7Cn0=