プログラムの説明は、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 の名前またはコントリビューターの名前を使用してはならない。
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、
明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証
も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター
も、事由のいかんを問わず、損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか
厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する
可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用
サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定
されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい
て、一切責任を負わないものとします。
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/stat.h>
#include "allocArray.h"
/* 画像処理プログラムのサンプル 画像間演算 C 言語 ソースコード
gcc -O4 -Wall -mno-cygwin -m486 -o op4bmp op4bmp.c
*/
// デバッグ用の関数定義
#ifdef DEBUG
#define DPRINTF( format, ... ) fprintf(stderr, "%s:%d\t" format, __FILE__,__LINE__,__VA_ARGS__)
#define IMHERE( ... ) fprintf(stderr, "%s:%d\t" __VA_ARGS__ "\n", __FILE__,__LINE__)
#else
#define DPRINTF( format, ... )
#define IMHERE( ... )
#endif
#pragma pack(push,1)
typedef struct PIXEL { unsigned char B, G, R, A; } PIXEL ;
#pragma pack(pop)
#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;
}
inline void operationSET( PIXEL **dst, int number, int wlx, int wly)
{IMHERE("operationSET");
register unsigned int *d=(unsigned int *)&dst[0][0] ;
register int x, y, lx=wlx, ly=wly ;
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
*d++ = number ;
}
}
}
inline void operationNOT( PIXEL **dst, PIXEL **src, int wlx, int wly )
{IMHERE("operationNOT"); // 画像間の論理演算 RGBデータ一を一括処理 NOT
register unsigned int *d=(unsigned int *)&dst[0][0], *s=(unsigned int *)&src[0][0] ;
register int x, y, lx=wlx, ly=wly ;
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; d++, s++, x++ ) {
*d = 0x00FFFFFF & (~*s) ;
}
}
}
inline void operationAND( PIXEL **dst, PIXEL **src1, PIXEL **src2, int number, int wlx, int wly)
{IMHERE("operationAND");
register PIXEL **d=dst, **s1=src1, **s2=src2 ;
register int x, y, lx=wlx, ly=wly ;
if( src2 ) {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = s1[y][x].B & s2[y][x].B ;
d[y][x].G = s1[y][x].G & s2[y][x].G ;
d[y][x].R = s1[y][x].R & s2[y][x].R ;
}
}
} else {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = s1[y][x].B & number ;
d[y][x].G = s1[y][x].G & number ;
d[y][x].R = s1[y][x].R & number ;
}
}
}
}
inline void operationOR( PIXEL **dst, PIXEL **src1, PIXEL **src2, int number, int wlx, int wly)
{IMHERE("operationOR");
register PIXEL **d=dst, **s1=src1, **s2=src2 ;
register int x, y, lx=wlx, ly=wly ;
if( src2 ) {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = s1[y][x].B | s2[y][x].B ;
d[y][x].G = s1[y][x].G | s2[y][x].G ;
d[y][x].R = s1[y][x].R | s2[y][x].R ;
}
}
} else {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = s1[y][x].B | number ;
d[y][x].G = s1[y][x].G | number ;
d[y][x].R = s1[y][x].R | number ;
}
}
}
}
inline void operationEXOR( PIXEL **dst, PIXEL **src1, PIXEL **src2, int number, int wlx, int wly)
{IMHERE("operationEXOR");
register PIXEL **d=dst, **s1=src1, **s2=src2 ;
register int x, y, lx=wlx, ly=wly ;
if( src2 ) {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = s1[y][x].B ^ s2[y][x].B ;
d[y][x].G = s1[y][x].G ^ s2[y][x].G ;
d[y][x].R = s1[y][x].R ^ s2[y][x].R ;
}
}
} else {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = s1[y][x].B ^ number ;
d[y][x].G = s1[y][x].G ^ number ;
d[y][x].R = s1[y][x].R ^ number ;
}
}
}
}
inline void operationSUB( PIXEL **dst, PIXEL **src1, PIXEL **src2, int number, int wlx, int wly)
{IMHERE("operationSUB");
register PIXEL **d=dst, **s1=src1, **s2=src2 ;
register int x, y, lx=wlx, ly=wly ;
if( src2 ) {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = (s1[y][x].B-s2[y][x].B)<0 ? 0 : s1[y][x].B-s2[y][x].B ;
d[y][x].G = (s1[y][x].G-s2[y][x].G)<0 ? 0 : s1[y][x].G-s2[y][x].G ;
d[y][x].R = (s1[y][x].R-s2[y][x].R)<0 ? 0 : s1[y][x].R-s2[y][x].R ;
}
}
} else {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = (s1[y][x].B-number)<0 ? 0 : s1[y][x].B-number ;
d[y][x].G = (s1[y][x].G-number)<0 ? 0 : s1[y][x].G-number ;
d[y][x].R = (s1[y][x].R-number)<0 ? 0 : s1[y][x].R-number ;
}
}
}
}
inline void operationADD( PIXEL **dst, PIXEL **src1, PIXEL **src2, int number, int wlx, int wly)
{IMHERE("operationADD");
register PIXEL **d=dst, **s1=src1, **s2=src2 ;
register int x, y, lx=wlx, ly=wly ;
if( src2 ) {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = (s1[y][x].B+s2[y][x].B)>255 ? 255 : s1[y][x].B+s2[y][x].B ;
d[y][x].G = (s1[y][x].G+s2[y][x].G)>255 ? 255 : s1[y][x].G+s2[y][x].G ;
d[y][x].R = (s1[y][x].R+s2[y][x].R)>255 ? 255 : s1[y][x].R+s2[y][x].R ;
}
}
} else {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = (s1[y][x].B+number)>255 ? 255 : s1[y][x].B+number ;
d[y][x].G = (s1[y][x].G+number)>255 ? 255 : s1[y][x].G+number ;
d[y][x].R = (s1[y][x].R+number)>255 ? 255 : s1[y][x].R+number ;
}
}
}
}
inline void operationMUL( PIXEL **dst, PIXEL **src1, PIXEL **src2, int number, int wlx, int wly)
{IMHERE("operationMUL");
register PIXEL **d=dst, **s1=src1, **s2=src2 ;
register int x, y, lx=wlx, ly=wly ;
if( src2 ) {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = (s1[y][x].B*s2[y][x].B)>255 ? 255 : s1[y][x].B*s2[y][x].B ;
d[y][x].G = (s1[y][x].G*s2[y][x].G)>255 ? 255 : s1[y][x].G*s2[y][x].G ;
d[y][x].R = (s1[y][x].R*s2[y][x].R)>255 ? 255 : s1[y][x].R*s2[y][x].R ;
}
}
} else {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = (s1[y][x].B*number)>255 ? 255 : s1[y][x].B*number ;
d[y][x].G = (s1[y][x].G*number)>255 ? 255 : s1[y][x].G*number ;
d[y][x].R = (s1[y][x].R*number)>255 ? 255 : s1[y][x].R*number ;
}
}
}
}
inline void operationDIV( PIXEL **dst, PIXEL **src1, PIXEL **src2, int number, int wlx, int wly)
{IMHERE("operationDIV");
register PIXEL **d=dst, **s1=src1, **s2=src2 ;
register int x, y, lx=wlx, ly=wly ;
if( src2 ) {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = s1[y][x].B / s2[y][x].B ;
d[y][x].G = s1[y][x].G / s2[y][x].G ;
d[y][x].R = s1[y][x].R / s2[y][x].R ;
}
}
} else {
for( y=0; y<ly; y++ ) {
for( x=0; x<lx; x++ ) {
d[y][x].B = s1[y][x].B / number ;
d[y][x].G = s1[y][x].G / number ;
d[y][x].R = s1[y][x].R / number ;
}
}
}
}
void execution( PIXEL **dst, char op, PIXEL **src1, PIXEL **src2, int number, int lx, int ly )
{
switch(op) {
case '~': operationNOT(dst, src1, lx, ly) ; break;
case '@': operationSET(dst, number, lx, ly) ; break;
case '&': operationAND(dst, src1, src2, number, lx, ly) ; break;
case '|': operationOR(dst, src1, src2, number, lx, ly) ; break;
case '^': operationEXOR(dst, src1, src2, number, lx, ly) ; break;
case '+': operationADD(dst, src1, src2, number, lx, ly) ; break;
case '-': operationSUB(dst, src1, src2, number, lx, ly) ; break;
case '*': operationMUL(dst, src1, src2, number, lx, ly) ; break;
case '/': operationDIV(dst, src1, src2, number, lx, ly) ; break;
default: fprintf( stderr, "Unknown operation.\n" ) ; break;
}
}
void *loadRawData(char *fname, int *dataLen)
{
struct stat fileInfo ;
register char *raw=NULL ;
register FILE *fp ;
*dataLen = 0 ;
if ( (fp=fopen(fname,"rb")) == NULL ) {
fprintf( stderr, "'%s' open error.\n", fname ); return NULL ;
} else if ( fstat(fileno(fp), &fileInfo) != 0 ) {
fprintf( stderr, "'%s' stat error.\n", fname ); fclose(fp) ; return NULL ;
} else if( fileInfo.st_size <= sizeof(HEADER) ) {
fprintf( stderr, "'%s' file size erro. (%d@%d Byte)\n",
fname, (int)fileInfo.st_size, sizeof(HEADER) ); fclose(fp) ; return NULL ;
} else if( (raw=(char *)malloc(fileInfo.st_size)) == NULL ) {
fprintf( stderr, "Couldn't allocate memory.\n" ) ; fclose(fp) ; return NULL ;
} else if ( fread(raw, fileInfo.st_size, 1, fp) != 1 ) {
fprintf( stderr, "'%s' read error.\n", fname );
free(raw) ; fclose(fp) ; return NULL ;//<-free(raw); fclose(fp) ; return NULL ;
} else fclose(fp) ;
*dataLen = fileInfo.st_size ;
return raw ;
}
int bmpFileCheck(char *raw, int size)
{
HEADER h=*(HEADER *)raw ;
register int lx=h.biWidth, ly=abs(h.biHeight), byte=h.biBitCount/8 ;
register int padding=((byte*lx+3)&~3) - byte*lx ;
if ( h.bfType != BMPID ) return -1 ;
if ( (h.bfReserved1!=0) || (h.bfReserved2!=0) ) return -1 ;
if ( (h.bfOffBits!=sizeof(HEADER)) || (h.biSize!=40) ) return -1 ;
if ( (h.biBitCount!=24) && (h.biBitCount!=32) ) return -1 ;
if ( (h.biPlanes!=1) || (h.biCompression!=0) ) return -1 ;
if ( ((byte*lx+padding)*ly+sizeof(HEADER))>size ) return -1 ;
return 0 ;
}
void imageDataCopyToArray(char *bmp, PIXEL **array)
{
HEADER h=*(HEADER *)bmp ;
register char *d=(char *)bmp + h.bfOffBits ;
register int i, x, y, lx=h.biWidth, ly=abs(h.biHeight), mode=h.biHeight ;
register int padding = (((h.biBitCount/8)*lx+3)&~3) -(h.biBitCount/8)*lx ;
register PIXEL **a=array ;
switch (h.biBitCount) {
case 32:
for( y=0, i=0; y<ly; y++ ) {
if( mode<0 ) i = (ly-1-y)*lx*4 ;
for( x=0; x<lx; x++ ) {
a[y][x].B = d[i++] ;
a[y][x].G = d[i++] ;
a[y][x].R = d[i++] ;
a[y][x].A = d[i++] ;
}
}
break;
case 24:
for( y=0, i=0; y<ly; y++ ) {
if( mode<0 ) i = (ly-1-y)*(3*lx+padding) ;
for( x=0; x<lx; x++ ) {
a[y][x].B = d[i++] ;
a[y][x].G = d[i++] ;
a[y][x].R = d[i++] ;
a[y][x].A = 0 ;
}
i += padding ;
}
break;
default:memset(&a[0][0], 0, sizeof(PIXEL)*lx*ly) ;
break;
}
}
int processing(char *fnameD, char op, char *fnameS1, char *fnameS2, int number )
{
HEADER h1, h2 ;
register char *raw1=NULL, *raw2=NULL ;
register PIXEL **dst=NULL, **bmp1=NULL, **bmp2=NULL ;
register int lx=0, ly=0, st=1 ;
static int sizeS1=0, sizeS2=0 ;
if( strlen(fnameS2) ) {
fprintf( stdout, "\n'%s' = '%s' %c '%s'\n\n", fnameD, fnameS1, op, fnameS2 ) ;
if( (raw1=(char *)loadRawData(fnameS1, &sizeS1)) == NULL ) goto GOOD_BY ;
if( bmpFileCheck(raw1,sizeS1) != 0 ) goto GOOD_BY ;
h1 = *(HEADER *)raw1 ;
lx = h1.biWidth ; // ((HEADER *)raw1)->biWidth or (*(HEADER *)raw1).biWidth
ly = abs(h1.biHeight) ; // abs(((HEADER *)raw1)->biHeight)
if( (raw2=(char *)loadRawData(fnameS2, &sizeS2)) == NULL ) goto GOOD_BY ;
if( bmpFileCheck(raw2,sizeS2) != 0 ) goto GOOD_BY ;
h2 = *(HEADER *)raw2 ;
if( (lx!=h2.biWidth) || (ly!=abs(h2.biHeight)) ) goto GOOD_BY ;
if( (bmp1=(PIXEL **)malloc2Darray(sizeof(PIXEL),lx,ly)) == NULL ) goto GOOD_BY ;
if( (bmp2=(PIXEL **)malloc2Darray(sizeof(PIXEL),lx,ly)) == NULL ) goto GOOD_BY ;
if( (dst =(PIXEL **)calloc2Darray(sizeof(PIXEL),lx,ly)) == NULL ) goto GOOD_BY ;
imageDataCopyToArray(raw1, bmp1) ;
imageDataCopyToArray(raw2, bmp2) ;
execution( dst, op, bmp1, bmp2, number, lx, ly ) ;
} else {
if( op=='@' ) fprintf( stdout, "\n'%s' = '%s' %c %d (0x%X)\n\n", fnameD, fnameS1, op, number, number ) ;
else if( op=='~' ) fprintf( stdout, "\n'%s' = %c '%s'\n\n", fnameD, op, fnameS1 ) ;
else fprintf( stdout, "\n'%s' = '%s' %c %d (0x%X)\n\n", fnameD, fnameS1, op, number, number ) ;
if( (raw1=(char *)loadRawData(fnameS1, &sizeS1)) == NULL ) goto GOOD_BY ;
if( bmpFileCheck(raw1,sizeS1) != 0 ) goto GOOD_BY ;
h1 = *(HEADER *)raw1 ;
lx = h1.biWidth ; // ((HEADER *)raw2)->biWidth or (*(HEADER *)raw2).biWidth
ly = abs(h1.biHeight) ; // abs(((HEADER *)raw2)->biHeight)
if( (bmp1=(PIXEL **)malloc2Darray(sizeof(PIXEL),lx,ly)) == NULL ) goto GOOD_BY ;
if( (dst =(PIXEL **)calloc2Darray(sizeof(PIXEL),lx,ly)) == NULL ) goto GOOD_BY ;
imageDataCopyToArray(raw1, bmp1) ;
execution( dst, op, bmp1, bmp2, number, lx, ly ) ; // bmp2 MUST BE NULL
}
saveDataAsBMP32( fnameD, &dst[0][0], lx, ly ) ;
st = 0 ;
GOOD_BY:
free(dst) ; free(bmp2) ; free(bmp1) ; free(raw2) ; free(raw1) ;
return st ;
}
void usage( int status )
{IMHERE("usage"); // コマンドの使い方の説明
fprintf( stderr, "\n\n" ) ;
fprintf( stderr, "Usage: op4bmp dist.bmp = { OPE1 src.bmp | src1.bmp OPE2 {src2.bmp|:NUMBER} }\n\n" ) ;
fprintf( stderr, " dist.bmp = not src.bmp \n" ) ;
fprintf( stderr, " dist.bmp = src1.bmp {and|or|exor|add|sub|mul|div} {src2.bmp|:NUMBER}\n" ) ;
fprintf( stderr, " dist.bmp = src1.bmp set :NUMBER\n" ) ;
fprintf( stderr, "\n" ) ;
fprintf( stderr, " (24|32bit no-compressed Windows BMP file format. \"NOT OS2 format\")\n\n" ) ;
fprintf( stderr, " NUMBER data format -> ':' + digit[,digit,digit,..]\n" ) ;
fprintf( stderr, " NUMBER data format -> ':' + 0x[,xdigit,xdigit,...]\n" ) ;
fprintf( stderr, " Ex. :123456, :0x00f0f0f0 (from 0 to 16777215 0xffffff)\n\n" ) ;
exit( status ) ;
}
char opCodeCheck(char *op)
{IMHERE("opCodeCheck"); // 指定されたオペレーションワードが正しいかの確認 or and not sub add mul div exor
register int l = strlen(op) ;
if( l>4 || l<2 ) return '\0' ;
else if ( l==2 ) {
if (tolower(op[0])=='o' && tolower(op[1])=='r' ) return '|' ;
} else if ( l==3 ) {
if (tolower(op[0])=='a' && tolower(op[1])=='n' && tolower(op[2])=='d' ) return '&' ;
if (tolower(op[0])=='n' && tolower(op[1])=='o' && tolower(op[2])=='t' ) return '~' ;
if (tolower(op[0])=='a' && tolower(op[1])=='d' && tolower(op[2])=='d' ) return '+' ;
if (tolower(op[0])=='s' && tolower(op[1])=='u' && tolower(op[2])=='b' ) return '-' ;
if (tolower(op[0])=='m' && tolower(op[1])=='u' && tolower(op[2])=='l' ) return '*' ;
if (tolower(op[0])=='d' && tolower(op[1])=='i' && tolower(op[2])=='v' ) return '/' ;
if (tolower(op[0])=='s' && tolower(op[1])=='e' && tolower(op[2])=='t' ) return '@' ;
} else if ( l==4 ) {
if (tolower(op[0])=='e' && tolower(op[1])=='x' && tolower(op[2])=='o' && tolower(op[3])=='r' ) return '^' ;
}
return '\0' ;
}
int numberCheck(char *str, int *number)
{IMHERE("numberCheck"); // 指定されたオペレーションワードが正しいかの確認 or and not sub add mul div exor...
register char *s=str ;
register int i, l = strlen(s), xdigit=0 ;
if( l<1 ) return -1 ;
if( l>2 && str[0]=='0' && str[1]=='x' ) {
if( l>(sizeof(int)*2+2) ) return -1 ;
for( i=2; i<l; i++ ) {
if( isxdigit(s[i])==0 ) return -1 ;
else xdigit = (xdigit*16) + ( isdigit(s[i])!=0 ? s[i]-'0' : (tolower(s[i])-'a')+10 ) ;
}
*number = xdigit ;
} else {
for( i=0; i<l; i++ ) if( isdigit(s[i])==0 ) return -1 ;
*number = atoi(str) ;
}
return 0 ;
}
int main(int argc, char *argv[])
{// BMPファイルを用いた、画像間の演算プログラム
int number = -1 ;
char op='\0', fnameSrc1[MAX_PATH]={0}, fnameSrc2[MAX_PATH]={0}, fnameDist[MAX_PATH]={0} ;
if ( (argc<5) || (argc>6) || (strlen(argv[2])!=1) || ((*argv[2])!='=') ) {
usage(1) ;
} else if ( argc == 5 ) { // 'prog' 'dst_bmp' '=' 'op' 'src_bmp'
if ( (op=opCodeCheck(argv[3])) == '\0') usage(1) ;
else if (op!='~') usage(1) ;
snprintf(fnameDist, MAX_PATH, "%s" , argv[1]) ;
snprintf(fnameSrc1, MAX_PATH, "%s" , argv[4]) ;
fnameSrc2[0] = '\0' ;
} else {//argv[0]⇔'prog' // 'prog' 'dst_bmp' '=' 'src1_bmp' 'op' 'src2_bmp or 数値'
if ( (op=opCodeCheck(argv[4])) == '\0') usage(1) ;
else if (op=='~') usage(1) ;
else if (op=='@'&& argv[5][0]!=':') usage(1) ; // @ ⇔ set number data
snprintf(fnameDist, MAX_PATH, "%s" , argv[1]) ;
snprintf(fnameSrc1, MAX_PATH, "%s" , argv[3]) ;
if( argv[5][0] == ':' ) { // number data format ':' + xdigit[,xdigit,xdigit,...]
if( numberCheck(&argv[5][1], &number) != 0 ) usage(1) ;
} else {
snprintf(fnameSrc2, MAX_PATH, "%s" , argv[5]) ;
}
}
return processing(fnameDist, op, fnameSrc1, fnameSrc2, number ) ;
}