#define no return
                          /*UtaKata/2004*/#include<stdio.h>
                      /*c=27f5a94f/s=4428^^*/#include<stdlib.h>
                   typedef double s;typedef int _;typedef unsigned
                 char W;FILE*f;W*M,*X,*m,*x,y;char*t[4]={"....r\145"
              "ad\40error","\56\56\56\56write\40error\0'.......","inva"
         "lid\40image","out\40of\40memory"},e[/*W;4T........*/64];s V,v,w
        ,R,G,B,r,g,b,_o=255.,MA,N,a,T,S,U;_/*F'.........*/I,C,H,i,K,A,ta,n;_
         c(){N=U=r;if(N>g)N=g;if(U<g)/*..........*/U=g;if(N>b)N=b;if(U<b)U=/*
       .................................*/b;V=v=w=0;if(N!=U){N=U-N;V=(r==U)?((-
      .0-b+g)/N/*.......*/):(g==U)?((b-r)/N+2):((r-g)/N+4);w=N/(v=U);}no-0;}_ j(_
    k){if(k<C||k>I-C||!(k%C)||!((k+1)  %C))no 1;r=(s)*(X+k*3)/_o;g=(s)*(X+k*3+1)/_o
   ;b=(s)*(X+k*3+2)/_o;c();no  ((.3<    V&&V<  .45)||4.3<V||V<.1)?00:j(k+K);}_ Q(){no
  f?fclose(f):0;}_ q(){;no     puts(     t[K]   );}_ E(_ k){   if(M[k]-255){M[k]=255;if
 ((k%C)<C-1)E(k+1  );if((k      /C)      <H-     1)E(k+C);if(   k%C)E(k-1);if(k/C)E(k-C);
 }no(0);}_ P(){;   x=X-1;      for         (     A=i=0;i<I;++   i){y=*++x;*x=*(x+2);*(x+=2)=
 y;}no+0;}_/*Q*/   O(){;       no    fwrite(      x,n,1,f);}_ r0(W*d,_ D){no(fread(d,D,1,f));}_
main(_ D,char**    d){;       if    (  (D<3))     no  printf("%s\40<in.bmp|in.ppm>\40<outfile>\n",*
d);K=C=0;d++;if     (!(       f     =fopen((  *   d)  ,"rb")))no-q();X=0;if(r0((W*)e,64)-1)no+q();K+=2
;if(*e==66&&'M' ==e[ 1       ]      ){if( *(      _  *)(e+28)- 24)no  q();C=*(_*)(e+18);H=*(_*)(e+22);ta=
(4-((C*3)&3))&   3;n=               54;}else         if(80==*   e&&e[1  ]==54){for(i=A=n=0;i<63;i++)if(10==e
[i])if(++A==3)  {e[n=                ++i]=0          ;i=64;}    if(sscanf (e,"P6\n%d %d\n",&C,&H)-2||!n)no+q();
 ta=0;}if(C<16  ||H<9                               )no-q();    K++;if(!(X= (W*)malloc((I=C*H)*3)))no-q();fseek(f,n,
 i=0);for(x=X;i  <H;                               ++i){r0(x   ,   C*3);x+=C* 3;for(A=0;A++<ta;fgetc(f)     );}Q();f=0;if(*e-80)P();
  if(!(m=(M)=(W                                   *) malloc(  I)))    no-q();x  =X;for(A=0;A<I;A++){r=(s)
  *(x++)/_o;g=(                                   s  )*(x++)/_o;b=(s    )*(x++)/ _o;c();*m=255;if(3.6<V&&V<
   4.24){S=1.29                   *              (  v-.42)*(v-.42)+(w-     .65)*( w-.65);T=1.29*(v-0.6)*(v-.6)
    +(w-.36)*(w-           .36);if                 (S<.6||T<.6)*m=(W)(_o*((   S<.6 )?(S/.6):(T/.6)));}m++;}for(i
     =A =K=0;A<C                                   ;){E(A);E(A+C);E(A+I-C*2);E(  I+   A++-C);}   for(;K<H;){E(K*C);E
       ( K*C+1);E                                 ((K+1)*C-2);E(++K*C-1);}for(m=M;i    <I;i++){if   (*m-255)if((j(i+(K=
        1  ))?1: j(                             i+(K=-1))?1:j(i+(K=C))?1:j(i+(K=-C))  ?1 :0))E(i);      ++m;}x=X;m=M;for(i
            =0;i   <I;                        ++i  ){if(*m-255){S=(s)*m/_o;T=1-S;r=R =(s )*x/_o;g=G=       (s)*(x+1)/_o;b=B=
              (s)     *(x+2)        /_o;c();if     ((V+=2)>=6)V-=6;if((w+=.7)>1)w=1;if ((v +=.1)>1)v=1           ;K=(_)V;MA=V-
               (s)          K;N=v*(1-              w);U=v*(1-w*MA);a=v*(1-w*(1-MA));r=(  K <4)?(K<2)?K?               U:v:N:(K>4)?
                 v:                                    a;g=(K<3)?K?v:a:K>3?N:U;b=(K<3)?(K    >1)?a:N:(K>                    4)?U:v;r=R
                  *S                                     +r*T;g=R*S+g*T;b=R*S+b*T;*x=(W)(r   *_o);x[1]=(W)
                                                              (g*_o);x[2]=(W)(b*_o);}x+=3;++    m;}free(M);if
                                                                     (*e!='P')P();K=1;if(!(f  =   fopen(    *++
                                                                              d,"wb+")))no+q(  )  ;x=(W*)      e;O
                                                                                      ();n=C*3;x=X;for(A=0;
                                                                                            A<H;A++){O();for(
                                                                                                K=0;K<ta;K++)fputc(
                                                                                                      0,f);x+=C*3;}free
                                                                                                               (X);no-Q();}