プログラムの説明は、IPFundamentals から確認してください。 By Cryst.
/*
Copyright (c) 2011, Cryst.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of
      conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of
      conditions and the following disclaimer in the documentation and/or other materials
      provided with the distribution.
    * Neither the name of the Cryst nor the names of its contributors may be used to endorse or
      promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Copyright (c)  2011, Cryst.
All rights reserved.

  ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に
  限り、再頒布および使用を許可します。

      * ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および 下記免責条項を
         含めること。
      * バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権
         表示、本条件一覧、および下記免責条項を含めること。
      * 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、
         Cryst の名前またはコントリビューターの名前を使用してはならない。

  本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、
  明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証
  も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター
  も、事由のいかんを問わず、損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか
  厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する
  可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用
  サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定
  されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい
  て、一切責任を負わないものとします。
*/
////////////////////////////////////////////////////////////////////////////////
// 画像処理関数の定義部
// コンパイル方法の例:gcc -O3 -Wall -m486 -o Laplacian Laplacian.c
/**
Laplacian プログラム C 言語 ソースコード

使い方:
1、Laplacian BMP_file_name_in [BMP_file_name_out]
 コマンドラインにBMPファイル名を指定して起動する。
 起動時に、出力ファイル名が省略された場合は、
 画像処理関数名に bmp の拡張子を付けた名前で保存される
*/
////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#pragma pack(push,1)
typedef struct PIXEL { unsigned char B, G, R, A; } PIXEL ;
#pragma pack(pop)

void **alloc2D( void *data, int sizeOfUnit, int lx, int ly )
{ // 連続データを、配列データとしてアクセス可能にする
register void **d, *base=data ;
register int i, s=sizeOfUnit, llx=lx, lly=ly ;

    if( (data==NULL) || (sizeOfUnit<1) || (lx<1) || (ly<1) ) return NULL ;
    if( (d=(void **)malloc(ly*sizeof(void *))) == NULL ) return NULL ;
    for( i=0; i<lly; i++ ) d[i] = (void *)((unsigned char *)base + llx*s*i) ;
    return d ;
}

#define IMAGEPROCESSING Laplacian  // 利用手順 その1:関数名を定義する
int IMAGEPROCESSING(PIXEL *src, PIXEL *dst, int wlx, int wly)
{ // 利用手順 その2:ここに画像処理用の関数を作成する。他の部分は変更せず流用
//// カラー画像を濃淡画像に変換し、Laplacianのオペレーションを行います

register int x, y, lx=wlx, ly=wly, ex=lx-1, ey=ly-1, laplacian, st=1 ;
register PIXEL **w=(PIXEL **)alloc2D(dst, sizeof(PIXEL), wlx, wly) ;
register PIXEL **p=(PIXEL **)alloc2D(src, sizeof(PIXEL), wlx, wly) ;
register int   **m, min=0, max=0, dev, i ;
register double gamma = 0.6;
int             gammaTable[256] ;

    if( (p==NULL) || (w==NULL) || (wlx<3) || (wly<3) ) goto GOOD_BY ;
    for( y=0; y<ly; w[y][0]=p[y][0], w[y][ex]=p[y][ex], y++ ) ;
//処理範囲外領域
    for( x=0; x<lx; w[0][x]=p[0][x], w[ey][x]=p[ey][x], x++ ) ; //処理範囲外領域
    
    for( y=0; y<ly; y++ ) {
// カラー画像から濃淡画像データーを生成する
        for( x=0; x<lx; x++ ) {
            p[y][x].A = ( 306*p[y][x].R + 601*p[y][x].G + 117*p[y][x].B ) >> 10 ;
        }
    }
    m = (int **)w ;
// 出力境域を、作業用(int)に流用する sizeof(int)==sizeof(PIXEL)
    for( y=1; y<ey; y++ ) { // Laplacian 処理結果(int)と最小値、最大値を調べる
        for( x=1; x<ex; x++ ) { // laplacianの計算結果(32ビット整数)
            laplacian = + p[y-1][x-1].A +   p[y-1][ x ].A + p[y-1][x+1].A
                        + p[ y ][x-1].A - 8*p[ y ][ x ].A + p[ y ][x+1].A
                        + p[y+1][x-1].A +   p[y+1][ x ].A + p[y+1][x+1].A ;
            m[y][x] = laplacian ;
// 出力画像用の配列を計算結果の記録エリアとして流用
            if ( x==1 && y==1 ) min = max = laplacian ;   // laplacianの計算結果の最大と最小
            else if ( max < laplacian ) max = laplacian ;
// を算出して、画像として出力
            else if ( min > laplacian ) min = laplacian ; // する時の変換に使用する
        }
    }
    
   
// ガンマー補正用のテーブル作成
    for (i=0; i<256; i++) gammaTable[i] = 255.0*pow((double)i/255.0, 1.0/gamma) ;
    
    dev = (max-min)>0 ? max-min : 1 ;
// 出力用画像に変換する
    for( y=1; y<ey; y++ ) {
// Laplacian 処理結果を 0~255 の範囲に変換し出力バッファへ
        for( x=1; x<ex; x++ ) {
            laplacian = ( 255 * ( m[y][x] - min ) ) / dev ;
// 0 ~ 255 に変換
            laplacian = gammaTable[laplacian] ;             // ガンマー補正処理
            w[y][x].R = laplacian ; // laplacianの計算結果の最小値から最大値の範囲を
            w[y][x].G = laplacian ; // 0 ~ 255 の範囲に変換する
            w[y][x].B = laplacian ; // 濃淡画像(R,G,B の各チャンネルに同じ値)で記録
        }
    }
    st = 0 ;
GOOD_BY: free(p) ; free(w) ;
    return st ;
}
// 利用手順 その3:プログラムを保存し、コンパイル後、処理画像を指定して起動

////////////////////////////////////////////////////////////////////////////////
// 以下 上記関数を実際に利用する為に必要な処理の記載部
//
// 画像データ(カラーBMPファイル)のアクセスや保存処理を定義した共通部
// windowsの 24,23ビット無圧縮形式BMPファイル対応版
// 通常ヘッダーファイルは多重読み込みに対応しているので何度インクルドしても良い

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#pragma pack(push,2)
typedef struct HEADER {
    unsigned short bfType;       // 0x4D42 BMP identification 0x42='B', 0x4D='M'
    unsigned int   bfSize;       // ??  
    unsigned short bfReserved1;  //  0  
    unsigned short bfReserved2;  //  0  
    unsigned int   bfOffBits;    // 54  sizeof(HEADER)
    unsigned int   biSize;          // 40 sizeof(biSize ~ biClrImportant)
    signed int     biWidth;         // lx
    signed int     biHeight;        // ly
    unsigned short biPlanes;        //  1
    unsigned short biBitCount;      // 32
    unsigned int   biCompression;   //  0
    unsigned int   biSizeImage;     // ??
    signed int     biXPelsPerMeter; //  0
    signed int     biYPelsPerMeter; //  0
    unsigned int   biClrUsed;       //  0
    unsigned int   biClrImportant;  //  0
} HEADER ;
#pragma pack(pop)

#define BMPID      0x4D42
#define BMPIFHSIZE 40

int saveDataAsBMP32( char *fname, void *b, int lx, int ly )
{/** 処理結果を 32ビット形式の BMPファイルとして書き出す。 */
HEADER h = {BMPID, lx*abs(ly)*sizeof(PIXEL)+sizeof(HEADER), 0, 0, sizeof(HEADER),
            BMPIFHSIZE, lx, ly, 1, 32, 0, lx*abs(ly)*sizeof(PIXEL), 0, 0, 0, 0} ;
FILE *fp ;

    if( (fp=fopen(fname, "wb")) == NULL ) {
        fprintf( stderr, "File '%s' cannot create.\n", fname );
        return -1;
    } else if ( fwrite( &h, sizeof(HEADER), 1, fp ) != 1 ) {
        fclose(fp) ; return -1;
    } else if ( fwrite( b, h.biSizeImage, 1, fp ) != 1 ) {
        fclose(fp) ; return -1;
    } else fclose(fp) ;
    return 0;
}

void raw2pixel(void *raw, void *pix, int lenX, int lenY, int BitCount)
{
register char    *src=(char *)raw, *dst=(char *)pix, dummy ;
register int     x, y, lx=lenX, ly=lenY, byteParLine=(lx*(BitCount/8)+3)&(~3) ;
register int     i, l=lx*ly, *s=(int *)raw, *d=(int *)pix ;

    if( BitCount!=24 && BitCount!=32 ) for( y=0; y<ly; y++ ) for( x=0, dummy=0; x<lx; *d++=dummy++, x++ ) ;
    else if( BitCount == 32 ) for( i=0; i<l; *d++=*s++, i++ ) ; // same as d[i]=s[i], i++
    else {// BitCount == 24
            for( y=0; y<ly; y++ ) {
                src = (char *)raw + byteParLine * y ;
                for( x=0; x<lx; x++ ) {
                    *dst++ = *src++ ; // B
                    *dst++ = *src++ ; // G
                    *dst++ = *src++ ; // R
                    *dst++ = 0 ;
                }
            }
    }
}

int main(int argc, char *argv[])
{
HEADER           h ;
struct stat      fileInfo ;
register FILE    *fp ;
register char    *raw=NULL, *image=NULL, *fname=NULL ;
register PIXEL   *source=NULL, *destination=NULL ;
register int     lx, ly, size, st=1 ;

    if ( argc != 2 && argc != 3 ) { // 画像データをメモリーに読み込む
        fprintf( stderr, "usage: %s input_BMP_name [output_BMP_name]\n", argv[0] );
        goto GOOD_BY ;
    } else if ( (fp=fopen(argv[1],"rb")) == NULL ) {
        fprintf( stderr, "'%s' open error.\n", argv[1] );               goto GOOD_BY ;
    } else if ( fstat(fileno(fp), &fileInfo) != 0 ) {
        fprintf( stderr, "'%s' stat error.\n", argv[1] );  fclose(fp) ; goto GOOD_BY ;
    } else if( fileInfo.st_size < sizeof(HEADER) ) {
        fprintf( stderr, "'%s' file size erro. too small(%d Byte)\n", argv[1], (int)fileInfo.st_size );
        fclose(fp) ; goto GOOD_BY ;
    } else if( (raw=(char *)malloc(fileInfo.st_size)) == NULL ) {
        fprintf( stderr, "Couldn't allocate memory.\n" ) ; fclose(fp) ; goto GOOD_BY ;
    } else if ( fread(raw, fileInfo.st_size, 1, fp) != 1 ) {
        fprintf( stderr, "'%s' read error.\n", argv[1] );  fclose(fp) ; goto GOOD_BY ;
    } else fclose(fp) ;

    // 画像データの確認:(画像データの確認と、作業用環境の構築)
    if( (h=*(HEADER *)raw).bfType!=BMPID || h.bfReserved1!=0 || h.bfReserved2!=0 ) {
        fprintf( stderr, "%s' is not a BMP file.\n", argv[1] );
        goto GOOD_BY ;
    } else if( h.bfOffBits!=sizeof(HEADER) || h.biSize!=BMPIFHSIZE || h.biPlanes!=1 || (h.biBitCount!=24 && h.biBitCount!=32) || h.biCompression!=0 ) {
        fprintf( stderr, "The BMP file, '%s', is not a 32(24)bit non-compression format.\n", argv[1] );
        goto GOOD_BY ;
    } else if ( fileInfo.st_size<(h.biSizeImage+h.bfOffBits) || h.biWidth*abs(h.biHeight)*(h.biBitCount/8)>h.biSizeImage ) {
        fprintf( stderr, "Bad BMP file format.\n" );
        fprintf( stderr, "%d, %d, %d, %d, %d\n", (int)fileInfo.st_size, h.biWidth, abs(h.biHeight), h.biWidth*abs(h.biHeight)*(h.biBitCount/8), h.biSizeImage );
        goto GOOD_BY ;
    } else {
        lx   = h.biWidth ;
        ly   = abs(h.biHeight) ; // 座標系が 左上原点でも 左下原点でも処理に支障は無く、必要なのは大きさのみ
        size = lx * ly * sizeof(PIXEL) ;
    }
    // 作業用環境の構築:(画像データの確認と、作業用環境の構築)
    if( (destination=(PIXEL *)malloc(size)) == NULL ) {
        fprintf( stderr, "Couldn't allocate memory. destination\n" ) ; goto GOOD_BY ;
    } else if( (source=(PIXEL *)malloc(size)) == NULL ) {
        fprintf( stderr, "Couldn't allocate memory. source\n" ) ;      goto GOOD_BY ;
    } else { // 読み込んだ画像を処理バッファーに格納(PIXEL形式への変換を含む)等
#define FINALFNAMEIS(foname) #foname                 // "MyFunction.bmp"
#define EXTRACTING(foname) FINALFNAMEIS(foname.bmp)  // MyFunction.bmp
#define FNAMEIS(foname) EXTRACTING(foname)           // IMAGEPROCESSING -> MyFunction
        fname = argc==2 ? FNAMEIS(IMAGEPROCESSING) : argv[2] ;
        image = (char *)raw + sizeof(HEADER) ;
        raw2pixel(image, source, lx, ly, h.biBitCount) ;
    }
    
    
    // 実際の画像処理と、処理結果の保存
    st = IMAGEPROCESSING(source, destination, lx, ly) ; // ここで画像処理ルーチンを呼び出す
    if( st == 0 ) saveDataAsBMP32( fname, destination, lx, h.biHeight) ; // biHeight (not ly)
    else fprintf( stderr, "Image processing is not completed.\n" ) ;

    
    
GOOD_BY: // 後始末とプログラムの終了
    free(destination) ;
    free(source) ;
    free(raw) ;
    return st ;
}

go to TopPage go to CategoryTop