Example #1
0
/*******************************************************
* drawEffects()
*
* Description: Case statement to time and handle the
* special effects.
*
* Inputs: none
*
* Outputs: none
*
* Return: none
*******************************************************/
void GLWidget::drawEffects()
{
    int ticksPerSecond = 1000 / GL_TIMER_INTERVAL;

    switch(effectType)
    {
    case EFFECT_NONE:
        if (iEventCounter / ticksPerSecond >= DEFAULT_TRANSITION_SECONDS)
        {
            isEffect = false;
        }
        break;
    case EFFECT_MOVE:
        if (iEventCounter / ticksPerSecond >= MOVE_TRANSITION_SECONDS)
        {
            isEffect = false;

            for (int i = 0; i < MAX_MAP_UNITS; i++)
            {
                if(unit[i].status != NO_UNIT)
                {
                    QStringList splitString = unit[i].imageFileName.split("/");
                    QString maskFileName;
                    maskFileName.append(splitString[0]);
                    maskFileName.append("/mask_");
                    maskFileName.append(splitString[1]);

                    unit[i].mask_image.load(maskFileName);
                    unit[i].maskFileName = maskFileName;
                }
            }
        }
        break;
    case EFFECT_ATTACK:
        if (iEventCounter / ticksPerSecond >= ATTACK_TRANSITION_SECONDS)
        {
            isEffect = false;
        }
        else
        {
            drawAttack();
        }
        break;
    }

    // Update event counter.
    iEventCounter++;
}
void ImageProcessor::findPieces(std::vector<std::vector<PieceSquare> > &ChessDesk) {
  RectArea CurrentSquare=findSquare(0,0);

  // Finding min and max brightness for autocontrast.
  int MaxGrayPixel=0;
  int MinGrayPixel=255;

  for(int i=0;i<8;i++) {
    for(int j=0;j<8;j++) {
      CurrentSquare=findSquare(i,j);
      for(int k=0;k<CurrentSquare.width-1;k++) {
        for(int l=0;l<CurrentSquare.height-1;l++) {
          int CurrentGrayPixel=qGray(image->pixel(CurrentSquare.x+k,CurrentSquare.y+l));
          if (CurrentGrayPixel>MaxGrayPixel) {
            MaxGrayPixel=CurrentGrayPixel;
          }
          if (CurrentGrayPixel<MinGrayPixel) {
            MinGrayPixel=CurrentGrayPixel;
          }
        }
      }
    }
  }
  
  writeLog(QString(tr("Minimum brightness: %1; Maximium brightness: %2;")).arg(MinGrayPixel).arg(MaxGrayPixel));

  
  int a[256];

  for(int i=0;i<256;i++) {a[i]=0;}
  
  for(int i=0;i<8;i++) {
    for(int j=0;j<8;j++) {
      CurrentSquare=findSquare(i,j);

      if ((MaxGrayPixel-MinGrayPixel)<128) {
        filterAutoContrast(CurrentSquare, *image,MinGrayPixel,MaxGrayPixel);
        filterAverage(CurrentSquare, *image);
      }
      filterMedian(CurrentSquare, *image);

      for(int k=0;k<CurrentSquare.width-1;k++) {
        for(int l=0;l<CurrentSquare.height-1;l++) {
          a[qGray(image->pixel(CurrentSquare.x+k,CurrentSquare.y+l))]++;
        }
      }
      CurrentSquare.x--;
      CurrentSquare.y--;
      CurrentSquare.width++;
      CurrentSquare.height++;
      drawRect(CurrentSquare);
    }
  }

  int max=0;

  for(int i=0;i<256;i++) {
    if (a[i]>max) max=a[i];
    //writeLog(QString("G %1 %2").arg(i).arg(a[i]));
  }

  writeLog(QString("MAX %1").arg(max));
  for(int i=0;i<256;i++) {
    for(int j=0;j<static_cast<int>((static_cast<double>(a[i]))*500/max);j++) {
      //image->setPixel(i,j,0x0000ff);
    }
  }
  
  std::vector<int> sorta;
  for(int i=0;i<256;i++) {
    sorta.push_back(a[i]);
  }
  std::sort(&sorta[0], &sorta[sorta.size()]);
  std::vector<int> Maximums;
  for (int i=255;i>=0;i--) {
    int IsInsideExtr=0;
    int EnterExtr=0;
    int LastEnter=-10;
    for(int j=0;j<256;j++) {
      if ((a[j]>sorta[i]) && (!IsInsideExtr)) {
        IsInsideExtr=1;
        EnterExtr++;
        if (abs(LastEnter-j)<10) {
          IsInsideExtr=0;
          EnterExtr--;
          Maximums.pop_back();
        } else {
          LastEnter=j;
          Maximums.push_back(j);
          writeLog(QString("POS %1").arg(j));
        }
      }

      if ((a[j]<sorta[i]) && (IsInsideExtr)) {
        IsInsideExtr=0;
      }
    }
    writeLog(QString("EXT %1").arg(EnterExtr));
    if (EnterExtr>=2) {break;} else Maximums.clear();
  }
  
  std::sort(&Maximums[0], &Maximums[Maximums.size()]);

  while (a[Maximums[0]]>0){Maximums[0]--;}
  while (a[Maximums[1]]>0){Maximums[1]++;}
  writeLog(QString("M1 %1 M2 %2").arg(Maximums[0]).arg(Maximums[1]));
;

  /////////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!
  
  for(int i=0;i<8;i++) {
    for(int j=0;j<8;j++) {
      CurrentSquare=findSquare(i,j);

      int AboveScale=0;
      int BelowScale=0;
      for(int k=0;k<CurrentSquare.width-1;k++) {
        for(int l=0;l<CurrentSquare.height-1;l++) {
          int current=qGray(image->pixel(CurrentSquare.x+k,CurrentSquare.y+l));
          if ((current<Maximums[0]) || (current>Maximums[1])) { 
            image->setPixel(CurrentSquare.x+k,CurrentSquare.y+l,0xffffff);
            if (current<Maximums[0]) BelowScale++;
            if (current>Maximums[1]) AboveScale++;
          } else 
          {
            image->setPixel(CurrentSquare.x+k,CurrentSquare.y+l,0x000000);
          }
        }
      }

      if (AboveScale>BelowScale) {
        ChessDesk[i][j].Color=1;
      } else {
        ChessDesk[i][j].Color=0;
      }
      
      writeLog(QString(tr("X %1 Y %2 C %3")).arg(i+1).arg(j+1).arg(ChessDesk[i][j].Color));

      if (DebugMode) {
        sendImage(*image);
        writeLog(QString(tr("Binarizing square X:%1 Y:%2.")).arg(i+1).arg(j+1));
      }
        
      approximateBorder(CurrentSquare, *image);

      if ((MaxGrayPixel-MinGrayPixel)<128) {
        filterDilation(CurrentSquare, *image);
        filterErosion(CurrentSquare, *image);
      }

      filterErosion(CurrentSquare, *image);
      filterDilation(CurrentSquare, *image);
      filterFigure(CurrentSquare, *image);
      recognizePiece(CurrentSquare, *image, ChessDesk[i][j]);
    }
  }
  
  for(int i=0;i<8;i++) {
    for(int j=0;j<8;j++) {
      if (ChessDesk[i][j].Type) {
        int type=ChessDesk[i][j].Type;
        int color=ChessDesk[i][j].Color;
        if (type==1 && color==0)
          writeLog(QString("X %1 Y %2 F %3").arg(i+1).arg(j+1).arg("Black King"));
        if (type==1 && color==1)
          writeLog(QString("X %1 Y %2 F %3").arg(i+1).arg(j+1).arg("White King"));
        if (type==2 && color==0)
          writeLog(QString("X %1 Y %2 F %3").arg(i+1).arg(j+1).arg("Black Queen"));
        if (type==2 && color==1)
          writeLog(QString("X %1 Y %2 F %3").arg(i+1).arg(j+1).arg("White Queen"));
        if (type==3 && color==0)
          writeLog(QString("X %1 Y %2 F %3").arg(i+1).arg(j+1).arg("Black Pawn"));
        if (type==3 && color==1)
          writeLog(QString("X %1 Y %2 F %3").arg(i+1).arg(j+1).arg("White Pawn"));
      }
    }
  }
  /////////////////////////////////////
  CheckMatePosition CurrentAttack=checkPosition(ChessDesk);

  delete image;
  image = new QImage(*source);
  drawRect(ChessArea,0xff0000);
  for(int i=0;i<8;i++) {
    for(int j=0;j<8;j++) {
      CurrentSquare=findSquare(i,j);
      if (ChessDesk[i][j].Type) {
        int type=ChessDesk[i][j].Type;
        int color=ChessDesk[i][j].Color;
        if (type==1 && color==0) //Black King
          drawRect(CurrentSquare,qRgb(30,89,19),5);
        if (type==1 && color==1) //White King
          drawRect(CurrentSquare,qRgb(121,11,11),5);
        if (type==2 && color==0) //Black Queen
          drawRect(CurrentSquare,qRgb(54,179,35),5);
        if (type==2 && color==1) //White Queen
          drawRect(CurrentSquare,qRgb(237,44,44),5);
        if (type==3 && color==0) //Black Pawn
          drawRect(CurrentSquare,qRgb(177,235,167),5);
        if (type==3 && color==1) //White Pawn
          drawRect(CurrentSquare,qRgb(250,192,192),5);
      }
    }
  }

  
  drawAttack(CurrentAttack);

  sendImage(*image);
}