Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
#include "img.h"
Toshihiro Shimizu 890ddd
#include "file.h"
Toshihiro Shimizu 890ddd
#include "toonz.h"
Toshihiro Shimizu 890ddd
#include "raster.h"
Toshihiro Shimizu 890ddd
#include "tmsg.h"
Toshihiro Shimizu 890ddd
#include "copP.h"*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
UCHAR First_preseed_table[256] = {
Shinya Kitaoka 120a6e
    8, 0, 1, 0, 2, 2, 1, 0, 3, 3, 3, 3, 2, 2, 3, 3, 4, 4, 1, 0, 2, 2, 1, 0,
Shinya Kitaoka 120a6e
    4, 4, 3, 3, 2, 2, 3, 3, 5, 5, 1, 5, 2, 2, 1, 5, 5, 5, 5, 5, 2, 2, 5, 5,
Shinya Kitaoka 120a6e
    4, 4, 1, 5, 2, 2, 1, 5, 4, 4, 5, 5, 2, 2, 5, 5, 6, 6, 1, 6, 2, 2, 1, 6,
Shinya Kitaoka 120a6e
    6, 6, 6, 6, 2, 2, 6, 6, 4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 8, 8, 2, 2, 8, 8,
Shinya Kitaoka 120a6e
    6, 6, 1, 6, 2, 2, 1, 6, 6, 6, 6, 6, 2, 2, 6, 6, 4, 4, 1, 0, 2, 2, 1, 0,
Shinya Kitaoka 120a6e
    4, 4, 8, 8, 2, 2, 8, 8, 7, 7, 1, 7, 2, 2, 1, 7, 7, 7, 7, 7, 2, 2, 7, 7,
Shinya Kitaoka 120a6e
    4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 3, 3, 2, 2, 3, 3, 7, 7, 1, 7, 2, 2, 1, 7,
Shinya Kitaoka 120a6e
    7, 7, 7, 7, 2, 2, 7, 7, 4, 4, 1, 5, 2, 2, 1, 5, 4, 4, 5, 5, 2, 2, 5, 5,
Shinya Kitaoka 120a6e
    7, 7, 1, 7, 2, 2, 1, 7, 7, 7, 7, 7, 2, 2, 7, 7, 4, 4, 1, 0, 2, 2, 1, 0,
Shinya Kitaoka 120a6e
    4, 4, 8, 8, 2, 2, 8, 8, 7, 7, 1, 7, 2, 2, 1, 7, 7, 7, 7, 7, 2, 2, 7, 7,
Shinya Kitaoka 120a6e
    4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 8, 8, 2, 2, 8, 8};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
static UCHAR Next_point_table[2048] = {
Shinya Kitaoka 120a6e
    8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Shinya Kitaoka 120a6e
    0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2,
Shinya Kitaoka 120a6e
    1, 2, 2, 2, 2, 2, 2, 0, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0,
Shinya Kitaoka 120a6e
    0, 0, 0, 0, 0, 3, 3, 1, 1, 1, 1, 1, 1, 3, 0, 1, 1, 1, 1, 1, 1, 3, 3, 3, 2,
Shinya Kitaoka 120a6e
    2, 2, 2, 2, 3, 0, 0, 2, 2, 2, 2, 2, 3, 3, 1, 2, 2, 2, 2, 2, 3, 0, 1, 2, 2,
Shinya Kitaoka 120a6e
    2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 4, 0, 4, 4, 4, 4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aoka 120a6e
    2, 6, 4, 4, 3, 3, 3, 6, 3, 6, 4, 4, 3, 0, 0, 6, 0, 6, 4, 4, 3, 3, 1, 6, 1,
Shinya Kitaoka 120a6e
    6, 4, 4, 3, 0, 1, 6, 1, 6, 4, 4, 3, 3, 3, 6, 2, 6, 4, 4, 3, 0, 0, 6, 2, 6,
Shinya Kitaoka 120a6e
    4, 4, 3, 3, 1, 6, 2, 6, 4, 4, 3, 0, 1, 6, 2, 6, 4, 4, 5, 5, 5, 5, 5, 6, 5,
Shinya Kitaoka 120a6e
    5, 5, 0, 0, 5, 0, 6, 0, 0, 5, 5, 1, 5, 1, 6, 1, 1, 5, 0, 1, 5, 1, 6, 1, 1,
Shinya Kitaoka 120a6e
    5, 5, 5, 5, 2, 6, 2, 2, 5, 0, 0, 5, 2, 6, 2, 2, 5, 5, 1, 5, 2, 6, 2, 2, 5,
Shinya Kitaoka 120a6e
    0, 1, 5, 2, 6, 2, 2, 3, 3, 3, 5, 3, 6, 3, 3, 3, 0, 0, 5, 0, 6, 0, 0, 3, 3,
Shinya Kitaoka 120a6e
    1, 5, 1, 6, 1, 1, 3, 0, 1, 5, 1, 6, 1, 1, 3, 3, 3, 5, 2, 6, 2, 2, 3, 0, 0,
Shinya Kitaoka 120a6e
    5, 2, 6, 2, 2, 3, 3, 1, 5, 2, 6, 2, 2, 3, 0, 1, 5, 2, 6, 2, 2, 5, 5, 5, 5,
Shinya Kitaoka 120a6e
    5, 6, 4, 4, 5, 0, 0, 5, 0, 6, 4, 4, 5, 5, 1, 5, 1, 6, 4, 4, 5, 0, 1, 5, 1,
Shinya Kitaoka 120a6e
    6, 4, 4, 5, 5, 5, 5, 2, 6, 4, 4, 5, 0, 0, 5, 2, 6, 4, 4, 5, 5, 1, 5, 2, 6,
Shinya Kitaoka 120a6e
    4, 4, 5, 0, 1, 5, 2, 6, 4, 4, 3, 3, 3, 5, 3, 6, 4, 4, 3, 0, 0, 5, 0, 6, 4,
Shinya Kitaoka 120a6e
    4,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 6, 7, 1, 5, 0, 1, 5, 1, 6, 7, 1, 5,
Shinya Kitaoka 120a6e
    5, 5, 5, 2, 6, 7, 2, 5, 0, 0, 5, 2, 6, 7, 2, 5, 5, 1, 5, 2, 6, 7, 2, 5, 0,
Shinya Kitaoka 120a6e
    1, 5, 2, 6, 7, 2, 3, 3, 3, 5, 3, 6, 7, 3, 3, 0, 0, 5, 0, 6, 7, 0, 3, 3, 1,
Shinya Kitaoka 120a6e
    5, 1, 6, 7, 1, 3, 0, 1, 5, 1, 6, 7, 1, 3, 3, 3, 5, 2, 6, 7, 2, 3, 0, 0, 5,
Shinya Kitaoka 120a6e
    2, 6, 7, 2, 3, 3, 1, 5, 2, 6, 7, 2, 3, 0, 1, 5, 2, 6, 7, 2, 5, 5, 5, 5, 5,
Shinya Kitaoka 120a6e
    6, 7, 4, 5, 0, 0, 5, 0, 6, 7, 4, 5, 5, 1, 5, 1, 6, 7, 4, 5, 0, 1, 5, 1, 6,
Shinya Kitaoka 120a6e
    7, 4, 5, 5, 5, 5, 2, 6, 7, 4, 5, 0, 0, 5, 2, 6, 7, 4, 5, 5, 1, 5, 2, 6, 7,
Shinya Kitaoka 120a6e
    4, 5, 0, 1, 5, 2, 6, 7, 4, 3, 3, 3, 5, 3, 6, 7, 4, 3, 0, 0, 5, 0, 6, 7, 4,
Shinya Kitaoka 120a6e
    3, 3, 1, 5, 1, 6, 7, 4, 3, 0, 1, 5, 1, 6, 7, 4, 3, 3, 3, 5, 2, 6, 7, 4, 3,
Shinya Kitaoka 120a6e
    0, 0, 5, 2, 6, 7, 4, 3, 3, 1, 5, 2, 6, 7, 4, 3, 0, 1, 5, 2, 6, 7, 4};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int Lx;
Toshihiro Shimizu 890ddd
int Displace_vector[8];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define E_PIX(pix) (*(pix + 1))
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define W_PIX(pix) (*(pix - 1))
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define N_PIX(pix) (*(pix + Lx))
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define S_PIX(pix) (*(pix - Lx))
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define SW_PIX(pix) (*(pix - Lx - 1))
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define NW_PIX(pix) (*(pix + Lx - 1))
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define NE_PIX(pix) (*(pix + Lx + 1))
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define SE_PIX(pix) (*(pix - Lx + 1))
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define NEIGHBOURS_CODE(pix)                                                   \
Shinya Kitaoka 120a6e
  (((SW_PIX(pix) != 0) << 0) | ((S_PIX(pix) != 0) << 1) |                      \
Shinya Kitaoka 120a6e
   ((SE_PIX(pix) != 0) << 2) | ((W_PIX(pix) != 0) << 3) |                      \
Shinya Kitaoka 120a6e
   ((E_PIX(pix) != 0) << 4) | ((NW_PIX(pix) != 0) << 5) |                      \
Shinya Kitaoka 120a6e
   ((N_PIX(pix) != 0) << 6) | ((NE_PIX(pix) != 0) << 7))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define FIRST_PRESEED(code) First_preseed_table[code]
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define NEXT_POINT(code, preseed) Next_point_table[(code << 3) | preseed]
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define UPDATE_POS(displ_code, x_pos, y_pos)                                   \
Shinya Kitaoka 120a6e
  switch (displ_code) {                                                        \
Shinya Kitaoka 120a6e
  case 0:                                                                      \
Shinya Kitaoka 120a6e
    x_pos--;                                                                   \
Shinya Kitaoka 120a6e
    y_pos--;                                                                   \
Shinya Kitaoka 120a6e
    break;                                                                     \
Shinya Kitaoka 120a6e
  case 1:                                                                      \
Shinya Kitaoka 120a6e
    y_pos--;                                                                   \
Shinya Kitaoka 120a6e
    break;                                                                     \
Shinya Kitaoka 120a6e
  case 2:                                                                      \
Shinya Kitaoka 120a6e
    x_pos++;                                                                   \
Shinya Kitaoka 120a6e
    y_pos--;                                                                   \
Shinya Kitaoka 120a6e
    break;                                                                     \
Shinya Kitaoka 120a6e
  case 3:                                                                      \
Shinya Kitaoka 120a6e
    x_pos--;                                                                   \
Shinya Kitaoka 120a6e
    break;                                                                     \
Shinya Kitaoka 120a6e
  case 4:                                                                      \
Shinya Kitaoka 120a6e
    x_pos++;                                                                   \
Shinya Kitaoka 120a6e
    break;                                                                     \
Shinya Kitaoka 120a6e
  case 5:                                                                      \
Shinya Kitaoka 120a6e
    x_pos--;                                                                   \
Shinya Kitaoka 120a6e
    y_pos++;                                                                   \
Shinya Kitaoka 120a6e
    break;                                                                     \
Shinya Kitaoka 120a6e
  case 6:                                                                      \
Shinya Kitaoka 120a6e
    y_pos++;                                                                   \
Shinya Kitaoka 120a6e
    break;                                                                     \
Shinya Kitaoka 120a6e
  case 7:                                                                      \
Shinya Kitaoka 120a6e
    x_pos++;                                                                   \
Shinya Kitaoka 120a6e
    y_pos++;                                                                   \
Shinya Kitaoka 120a6e
    break;                                                                     \
Shinya Kitaoka 120a6e
  default:                                                                     \
otakuto a18e65
    assert(false);                                                             \
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int fillByteRaster(const TRaster32P &rin, TRasterGR8P &rout) {
Shinya Kitaoka 120a6e
  bool ret = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(rin->getLx() == rout->getLx() - 2 &&
Shinya Kitaoka 120a6e
         rin->getLy() == rout->getLy() - 2);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  memset(rout->getRawData(), 0, rout->getLx() * rout->getLy());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < rin->getLy(); i++) {
Shinya Kitaoka 120a6e
    TPixel32 *bufIn = (TPixel32 *)rin->getRawData() + i * rin->getWrap();
Shinya Kitaoka 120a6e
    UCHAR *bufOut = (UCHAR *)rout->getRawData() + (i + 1) * rout->getWrap() + 1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    for (int j = 0; j < rin->getLx(); j++, bufIn++, bufOut++)
Shinya Kitaoka 120a6e
      if (bufIn->m != 0) {
Shinya Kitaoka 120a6e
        ret     = true;
Shinya Kitaoka 120a6e
        *bufOut = 1;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void initialize_displace_vector(void) {
Shinya Kitaoka 120a6e
  Displace_vector[0] = -Lx - 1;
Shinya Kitaoka 120a6e
  Displace_vector[1] = -Lx;
Shinya Kitaoka 120a6e
  Displace_vector[2] = -Lx + 1;
Shinya Kitaoka 120a6e
  Displace_vector[3] = -1;
Shinya Kitaoka 120a6e
  Displace_vector[4] = +1;
Shinya Kitaoka 120a6e
  Displace_vector[5] = Lx - 1;
Shinya Kitaoka 120a6e
  Displace_vector[6] = Lx;
Shinya Kitaoka 120a6e
  Displace_vector[7] = Lx + 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
otakuto a18e65
static int find_next_seed(const TRasterGR8P &r, bool first_seed, UCHAR *&seed,
Shinya Kitaoka 120a6e
                          int &x_pos, int &y_pos) {
Shinya Kitaoka 120a6e
  static int Curr_x = 0, Curr_y = 0;
Shinya Kitaoka 120a6e
  UCHAR *pix;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (first_seed) {
Shinya Kitaoka 120a6e
    Curr_x = 0;
Shinya Kitaoka 120a6e
    Curr_y = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  seed = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (; Curr_y < r->getLy(); Curr_y++) {
Shinya Kitaoka 120a6e
    pix = (UCHAR *)r->getRawData() + Curr_y * r->getWrap() + Curr_x;
Shinya Kitaoka 120a6e
    for (; Curr_x < r->getLx(); Curr_x++, pix++)
Shinya Kitaoka 120a6e
      if (*pix == 1 &&
Shinya Kitaoka 120a6e
          (!E_PIX(pix) || !W_PIX(pix) || !N_PIX(pix) || !S_PIX(pix))) {
Shinya Kitaoka 120a6e
        seed  = pix;
Shinya Kitaoka 120a6e
        x_pos = Curr_x;
Shinya Kitaoka 120a6e
        y_pos = Curr_y;
Shinya Kitaoka 120a6e
        return true;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    Curr_x = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define COMPUTE_NUM_DEN(x_pos, y_pos, old_x_pos, old_y_pos)                    \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    aux = old_x_pos * y_pos - x_pos * old_y_pos;                               \
Shinya Kitaoka 120a6e
    den += aux;                                                                \
Shinya Kitaoka 120a6e
    x_num += (x_pos + old_x_pos) * aux;                                        \
Shinya Kitaoka 120a6e
    y_num += (y_pos + old_y_pos) * aux;                                        \
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Campbell Barton b3bd84
static void doComputeCentroid(const TRasterGR8P &r, TPoint &cp) {
Shinya Kitaoka 120a6e
  int preseed, displ, prewalker, code;
Shinya Kitaoka 120a6e
  UCHAR *seed, *walker;
Shinya Kitaoka 120a6e
  int x_num = 0, y_num = 0, den = 0;
Shinya Kitaoka 120a6e
  int x_pos, y_pos, old_x_pos, old_y_pos;
Shinya Kitaoka 120a6e
  int aux, old_displ = -1;
otakuto a18e65
  bool first_seed;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Lx                 = r->getLx();
Shinya Kitaoka 120a6e
  Displace_vector[0] = -Lx - 1;
Shinya Kitaoka 120a6e
  Displace_vector[1] = -Lx;
Shinya Kitaoka 120a6e
  Displace_vector[2] = -Lx + 1;
Shinya Kitaoka 120a6e
  Displace_vector[3] = -1;
Shinya Kitaoka 120a6e
  Displace_vector[4] = +1;
Shinya Kitaoka 120a6e
  Displace_vector[5] = Lx - 1;
Shinya Kitaoka 120a6e
  Displace_vector[6] = Lx;
Shinya Kitaoka 120a6e
  Displace_vector[7] = Lx + 1;
Toshihiro Shimizu 890ddd
otakuto a18e65
  first_seed = true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  while (find_next_seed(r, first_seed, seed, x_pos, y_pos)) {
otakuto a18e65
    first_seed = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    *seed |= 0x2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    code    = NEIGHBOURS_CODE(seed);
Shinya Kitaoka 120a6e
    preseed = FIRST_PRESEED(code);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (preseed == 8) continue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    displ = NEXT_POINT(code, preseed);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    walker = seed + Displace_vector[displ];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    old_x_pos = x_pos;
Shinya Kitaoka 120a6e
    old_y_pos = y_pos;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    UPDATE_POS(displ, x_pos, y_pos)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    COMPUTE_NUM_DEN(x_pos, y_pos, old_x_pos, old_y_pos)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    prewalker = displ ^ 0x7;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    while ((walker != seed) || (preseed != prewalker)) {
Shinya Kitaoka 120a6e
      *walker |= 0x2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      displ = NEXT_POINT(NEIGHBOURS_CODE(walker), prewalker);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      walker += Displace_vector[displ];
Shinya Kitaoka 120a6e
      old_x_pos = x_pos;
Shinya Kitaoka 120a6e
      old_y_pos = y_pos;
Shinya Kitaoka 120a6e
      UPDATE_POS(displ, x_pos, y_pos)
Shinya Kitaoka 120a6e
      COMPUTE_NUM_DEN(x_pos, y_pos, old_x_pos, old_y_pos)
Shinya Kitaoka 120a6e
      prewalker = displ ^ 0x7;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (den)
Shinya Kitaoka 120a6e
    cp = TPoint(tround(x_num / (3.0 * den)), tround(y_num / (3.0 * den)));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPoint computeCentroid(const TRaster32P &r) {
Shinya Kitaoka 120a6e
  TPoint ret(1, 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterGR8P raux(r->getLx() + 2, r->getLy() + 2);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (fillByteRaster(r, raux)) doComputeCentroid(raux, ret);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ret.x--;
Shinya Kitaoka 120a6e
  ret.y--; /* per il bordo aggiunto */
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
}