/*
Copyright (c) 2016, 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)  2016, Cryst.
All rights reserved.

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

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

  本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供さ
  れており、明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性
  に関する暗黙の保証も含め、またそれに限定されない、いかなる保証もありません。
  著作権者もコントリビューターも、事由のいかんを問わず、損害発生の原因いかんを問
  わず、かつ責任の根拠が契約であるか厳格責任であるか(過失その他の)不法行為であ
  るかを問わず、仮にそのような損害が発生する可能性を知らされていたとしても、
  本ソフトウェアの使用によって発生した(代替品または代用サービスの調達、使用の
  喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定されない)
  直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害について、
  一切責任を負わないものとします。
*/
////////////////////////////////////////////////////////////////////////////////
// 画像処理関数の定義部
// コンパイル方法の例:gcc -O3 -Wall -W -m486 -o nega nega.c
/** bmp ファイルを表示するプログラムの雛形
使い方:
1、nega BMP_file_name_in [BMP_file_name_out]
 コマンドラインにBMPファイル名を指定して起動する。
 起動時に、出力ファイル名が省略された場合は、
 画像処理関数名に bmp の拡張子を付けた名前で保存される
*/
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>

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

extern  void **alloc2D( void *, int, int, int );
extern  void **alloc2Darray( int, int, int );
#define FINALFNAMEIS(foname) #foname
#define EXTRACTING(foname)   FINALFNAMEIS(foname.bmp)
#define FNAMEIS(foname)      EXTRACTING(foname)
#define IFNAMEDEFAULT        "source.bmp"

#define IMAGEPROCESSING nega  // 利用手順 その1:関数名を定義する
int IMAGEPROCESSING(PIXEL *src, PIXEL *dst, int wlx, int wly)
{ // 利用手順 その2:ここに画像処理用の関数を作成する。他の部分は変更せず流用
//// カラー画像をR, G, B 各チャンネル毎に反転処理する
register int x, y, lx=wlx, ly=wly, st=1 ;
register PIXEL **s=(PIXEL **)alloc2D(src, sizeof(PIXEL), wlx, wly) ;
register PIXEL **d=(PIXEL **)alloc2D(dst, sizeof(PIXEL), wlx, wly) ;

    if( (d==NULL) || (s==NULL) ) goto GOOD_BY ;
    for( y=0; y<ly; y++ ) {
        for( x=0; x<lx; x++ ) {
            d[y][x].R = ~s[y][x].R ;
            d[y][x].G = ~s[y][x].G ;
            d[y][x].B = ~s[y][x].B ;
            d[y][x].A = 0 ;
        }
    }
    st = 0 ;
GOOD_BY: free(d) ; free(s) ;
    return st ;
} // 利用手順 その3:プログラムを保存し、コンパイル後、処理画像を指定して起動

////////////////////////////////////////////////////////////////////////////////
// 以下は共通ルーチン、画像処理部分はここより上に記載可能
////////////////////////////////////////////////////////////////////////////////
// 画像データ(カラーBMPファイル)のアクセスや保存処理を定義した共通部
// windowsの 24,23ビット無圧縮形式BMPファイル対応版
// windows形式のBMPファイルのみ (OS2形式はサポート対象外)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#pragma pack(push,2)
typedef struct HEADER {       // BMP => bfType==0x4D42 (0x42,0x4D) little endian
    unsigned short bfType;          // 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
    ////////////////////////////////////////////////////////////////////////////
    unsigned char  lut[];
} HEADER ;
#pragma pack(pop)

#pragma pack(push,2)
typedef struct HEADERBW {    // BMP => bfType==0x4D42 (0x42,0x4D) little endian
    unsigned short bfType;          // identification  0x42='B', 0x4D='M'
    unsigned int   bfSize;          // ?? 
    unsigned short bfReserved1;     //  0 
    unsigned short bfReserved2;     //  0 
    unsigned int   bfOffBits;       // 62  sizeof(HEADERBW) 54 + 8
    ////////////////////////////////////////////////////////////////////////////
    unsigned int   biSize;          // 40  sizeof(biSize ~ biClrImportant)
    signed int     biWidth;         // lx
    signed int     biHeight;        // ly
    unsigned short biPlanes;        //  1
    unsigned short biBitCount;      //  1
    unsigned int   biCompression;   //  0
    unsigned int   biSizeImage;     // ??
    signed int     biXPelsPerMeter; //  0 or 50 ...
    signed int     biYPelsPerMeter; //  0 or 50 ...
    unsigned int   biClrUsed;       //  2
    unsigned int   biClrImportant;  //  2
    ////////////////////////////////////////////////////////////////////////////
    PIXEL          lut[2];          //  color for 0 and 1
} HEADERBW ;
#pragma pack(pop)

#define BMPID      0x4D42
#define BMPIFHSIZE 40

void **alloc2D( void *data, int s, int lx, int ly )
{// データ(lx*ly個)の1次元配列を、array[y][x] 形式で参照できるようにする
void **d, *base=data ;
long i;

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

#define calloc2Darray alloc2Darray
void **alloc2Darray( int s, int lx, int ly )
{// array[y][x] 形式で参照できる、横:lx、縦:ly の配列を動的に確保する
void **d, *base ;
long i ;

    if( (s<1) || (lx<1) || (ly<1) ) return NULL ;
    if( (d=(void **)calloc(1,ly*sizeof(void *)+(lx*ly*s))) == NULL ) return NULL ;
    base = (unsigned char *)d + ly*sizeof(void *) ;
    for( i=0; i<ly; i++ ) d[i] = (void *)((unsigned char *)base + lx*s*i) ;
    return d ;
}

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;
}

int bmp2pixel( void *bmp, void *pix )
{// メモリー上の BMP ファイルを、PIXEL 形式のデータに変換する
static char     f[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
HEADER *h=(HEADER *)bmp;
PIXEL  *d=(PIXEL*)pix, *lut=(PIXEL*)h->lut;
int    x, y, i, top=((h->bfOffBits)-sizeof(HEADER))/sizeof(PIXEL);
int    sign, lx=h->biWidth, ly=abs((sign=h->biHeight));
int    BitCount=h->biBitCount, byteParLine=(lx*(BitCount/8)+3)&(~3);
int    byteParLineBW = (((lx+7)/8)+3)&(~3);
unsigned char *src=(unsigned char*)bmp + h->bfOffBits;
unsigned char *dst=(unsigned char*)pix, *here;

    if( BitCount == 32 ) {
        for( y=0; y<ly; y++ ) {
            here = src + byteParLine * ( sign>0 ? y :ly-1 -y );
            for( x=0; x<lx; x++ ) {
                *dst++ = *here++ ; // B
                *dst++ = *here++ ; // G
                *dst++ = *here++ ; // R
                *dst++ = *here++ ; // A
            }
        }
    } else if( BitCount == 24 ) {
        for( y=0; y<ly; y++ ) {
            here = src + byteParLine * ( sign>0 ? y :ly-1 -y );
            for( x=0; x<lx; x++ ) {
                *dst++ = *here++ ; // B
                *dst++ = *here++ ; // G
                *dst++ = *here++ ; // R
                *dst++ = 0 ;
            }
        }
    } else if( BitCount == 8 ) {
        int *u=(int*)pix, *t=(int*)h->lut;
        for( y=0, i=0; y<ly; y++ ) {
            here = src + byteParLine * ( sign>0 ? y :ly-1 -y );
            for( x=0; x<lx; x++ ) u[i++] = here[x]<top ? t[here[x]] : 0;
        }
    } else if( BitCount == 1 ) {
        #define getBW(t,x) (((t)&(f[0x7&(x)]))?1:0)
        for( y=0, i=0; y<ly; y++ ) {
            here = src + byteParLineBW * ( sign>0 ? y :ly-1 -y );
            for( x=0; x<lx; x++ ) d[i++] = lut[getBW(here[x>>3],x)];
        }
    } else return 1;
    return 0;
}

int Check_bmpfile( void *s, unsigned long length )
{// メモリー上のデータが、処理可能な BMP ファイル形式であるかをチェックする
    if( s==NULL || length<=sizeof(HEADER) )  return 1;            // return 1;
    HEADER h=*(HEADER *)s;
    unsigned long isize=h.biSizeImage, fsize=isize+h.bfOffBits;
    unsigned long psize=h.biWidth*abs(h.biHeight)*(h.biBitCount/8);
    if( h.bfType!=BMPID || h.bfReserved1!=0 || h.bfReserved2!=0 )    return 2;
    else if( h.biPlanes!=1 || h.biCompression!=0 )                   return 3;
    else if( h.biSizeImage!=0 && (length<fsize || isize<psize) )     return 4;
    else if( h.biBitCount==32 && h.bfOffBits!=sizeof(HEADER) )       return 5;
    else if( h.biBitCount==24 && h.bfOffBits!=sizeof(HEADER) )       return 6;
    else if( h.biBitCount== 8 && (h.bfOffBits-sizeof(HEADER))%sizeof(PIXEL) ) {
       fprintf( stderr,"Unexpected lut size. %d, %d\n", h.bfOffBits,
              (int)((h.bfOffBits-sizeof(HEADER))/sizeof(PIXEL)) );   return 7;
    } else if(h.biBitCount==1 && (h.bfOffBits-sizeof(HEADER))!=2*sizeof(PIXEL)){
       fprintf( stderr,"Unexpected, requirement is 2, lut size. %d, %d\n",
              h.bfOffBits, (int)((h.bfOffBits-sizeof(HEADER))/sizeof(PIXEL)) );
                                                                     return 8;
    }
    return 0;
}

int file2memory( char *fname, char **image, long *size )
{// ディスク上のファイルを、メモリーに一括で読み込む
struct stat      fileInfo ;
FILE    *fp ;
char    *raw ;

    *image=NULL; *size=0;
    if ( (fp=fopen(fname,"rb")) == NULL ) {
        fprintf( stderr, "'%s' open error.\n", fname );               return 11;
    } else if ( fstat(fileno(fp), &fileInfo) != 0 ) {
        fprintf( stderr, "'%s' stat error.\n", fname );  fclose(fp);  return 12;
    } else if( fileInfo.st_size <= (int)sizeof(HEADER) ) {
        fprintf( stderr, "'%s' file size erro. too small(%d Byte)\n",
                         fname, (int)fileInfo.st_size ); fclose(fp);  return 13;
    } else if( (raw=(char *)malloc(fileInfo.st_size)) == NULL ) {
        fprintf( stderr, "Couldn't allocate memory.\n" ) ; fclose(fp);return 14;
    } else if ( fread(raw, fileInfo.st_size, 1, fp) != 1 ) {
        fprintf( stderr, "'%s' read error.\n", fname );  fclose(fp);  return 15;
    } else fclose(fp) ;
    *image= raw;
    *size = fileInfo.st_size;
    return 0;
}

void argumentAnalysis(int argc, char *argv[], char **iname, char **oname)
{   int  i;   // プログラム起動時の引数解析処理
    for( i=1; i<argc; i++ ) {
        if( argv[i][0]=='-' ) goto GOOD_BY;
        else if( *iname==NULL ) *iname = argv[i];
        else if( *oname==NULL ) *oname = argv[i];
        else goto GOOD_BY;
    }
    return;
GOOD_BY:;
    char *p=argv[0], *op="";
    for( i=strlen(p); i>0 && p[i]!='\\' && p[i]!='/'; i-- ); if(i) p++;
    fprintf( stderr, "\nusage: %s %s [input_bmp [output_bmp]]\n\n", p, op );
    exit(1);
}

int main(int argc, char *argv[])
{// 引数解析後データを読み込み、画像処理ルーチンを呼び出す。正常終了時,BMPを作成
long  st=1, filesize=0;
char  *raw=NULL, *ifname=NULL, *ofname=NULL;
PIXEL *source=NULL, *destination=NULL ;

    argumentAnalysis( argc, argv, &ifname, &ofname );    // 引数解析
    if( ifname==NULL ) ifname = IFNAMEDEFAULT;           // 入力ファイル名の設定
    if( ofname==NULL ) ofname = FNAMEIS(IMAGEPROCESSING);// 出力ファイル名の設定
    
    // 画像データの読み込み(ディスクからメモリーへ)
    if( (st=file2memory( ifname, &raw, &filesize ))!=0 )           goto GOOD_BY;

    // 画像データの確認:(画像データの確認と、作業用環境の構築)
    if( (st=Check_bmpfile( raw, filesize )) )                      goto GOOD_BY;
    
    HEADER *h=(HEADER *)raw;
    int    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;
    } //読み込んだ画像を処理バッファーに格納 PIXEL形式への変換を等含む
    bmp2pixel( raw, source ) ;

    // 実際の画像処理と、処理結果の保存, 画像処理ルーチンの呼び出し
    if( (st=IMAGEPROCESSING( source, destination, lx, ly ))==0 ) {
        saveDataAsBMP32( ofname, destination, lx, ly );    // BMPファイルで保存
    } else if( st>0 ) fprintf( stderr, "Image processing is not completed.\n" );
    
GOOD_BY:; // 後始末とプログラムの終了
    free(source); free(destination); free(raw);
    return st;
}