/*
    基本情報技術者試験          平成9年度・秋期・午後 問7
                                BohYoh Shibata PREPARATION
*/

#include <string.h>
unsigned long gGetPixel( int x , int y );
void gSetPixel( int x , int y , unsigned long Color );
#define MAXTEXT 1024
extern  int Width;
extern  int Height;

void   WriteHiddenText( unsigned char *Ptr )  /* 文字列の埋込み */
{
   unsigned char   Bit2 ;
   unsigned long   Color ;
   int x , y , Cnt , Index = 0 ;
   Cnt = ( strlen( Ptr ) + 1 ) * 4 ;
   for( y = 0 ; y < Height ; y++ ) {
      for( x = 0 ; x < Width ; x++ ) {
         Color = gGetPixel( x , y ) ;
         Color &= 0xfffffc ;
         Bit2 = *Ptr ;
         Bit2 >>= ( 6 - Index * 2 );
         Bit2 &= 0x3 ;
         Color |= Bit2 ;
         gSetPixel( x , y , Color );
         Index++ ;
         if ( Index > 3 ) {
            Ptr++ ; Index = 0 ;
         }
         Cnt-- ;
         if ( Cnt <= 0 ) break ;
      }
      if ( Cnt <= 0 ) break ;
   }
}

int ReadHiddenText( unsigned char *Ptr )  /* 文字列の復元 */
{
   unsigned char    Bit2 ;
   unsigned long    Color ;
   int x , y , Cnt = 1 , Index = 0 ;
   *Ptr = '\0' ;
   for( y = 0 ; y < Height ; y++ ) {
      for( x = 0 ; x < Width ; x++ ) {
         Color = gGetPixel( x , y ) ;
         Bit2 = ( unsigned char )Color ;
         Bit2 &= 0x3 ;
         Bit2 <<= ( 6 - Index * 2 ) ;
         *Ptr |= Bit2;
         if (( Index == 3 ) && ( *Ptr == '\0' ))
            return Cnt ;
         Index++ ;
         if ( Index > 3 ) {
            Ptr++ ; *Ptr = '\0' ; Index = 0 ; Cnt++ ;
            if ( Cnt > MAXTEXT ) {
               *( Ptr - 1 ) = '\0' ;
               return MAXTEXT ;
            }
         }
      }
   }
   return Cnt ;
}