Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "rasterstyles.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
#include "trastercm.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
//#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
//    TAirbrushRasterStyle  implementation
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TAirbrushRasterStyle::loadData(TInputStreamInterface &is) { is >> m_blur; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TAirbrushRasterStyle::saveData(TOutputStreamInterface &os) const {
Shinya Kitaoka 120a6e
  os << m_blur;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TAirbrushRasterStyle::compute(const Params ¶ms) const {
Shinya Kitaoka 120a6e
  if (m_blur > 0.0) TRop::blur(params.m_r, params.m_r, m_blur, 0, 0, true);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TColorStyle *TAirbrushRasterStyle::clone() const {
Shinya Kitaoka 120a6e
  TColorStyle *cs = new TAirbrushRasterStyle(*this);
Shinya Kitaoka 120a6e
  cs->assignNames(this);
Shinya Kitaoka 120a6e
  return cs;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TAirbrushRasterStyle::makeIcon(const TDimension &d) {
Shinya Kitaoka 120a6e
  TFilePath dir = TEnv::getStuffDir() + "pixmaps";
Shinya Kitaoka 120a6e
  static TRasterP normalIc;
Shinya Kitaoka 120a6e
  if (normalIc == TRasterP())
Shinya Kitaoka 120a6e
    TImageReader::load(dir + "airbrush.bmp", normalIc);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  arrangeIcon(d, normalIc);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TAirbrushRasterStyle::arrangeIcon(const TDimension &d,
Shinya Kitaoka 120a6e
                                       const TRasterP &normalIc) {
Shinya Kitaoka 120a6e
  if (normalIc == TRasterP()) {
Shinya Kitaoka 120a6e
    m_icon = TRaster32P(d);
Shinya Kitaoka 120a6e
    m_icon->fill(TPixel32::Red);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_icon = TRasterP();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (d == TDimension(52, 52))
Shinya Kitaoka 120a6e
    m_icon = normalIc->clone();
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    m_icon = TRaster32P(d);
Shinya Kitaoka 120a6e
    TRop::resample(m_icon, normalIc, TScale(d.lx / 52.0, d.ly / 52.0));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_icon->lock();
Shinya Kitaoka 120a6e
  TPixel *buf  = (TPixel *)m_icon->getRawData();
Shinya Kitaoka 120a6e
  TPixel color = getMainColor();
Shinya Kitaoka 120a6e
  double rd    = (color.r - color.m) / 255.0;
Shinya Kitaoka 120a6e
  double gd    = (color.g - color.m) / 255.0;
Shinya Kitaoka 120a6e
  double bd    = (color.b - color.m) / 255.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(m_icon->getWrap() == m_icon->getSize().lx);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < m_icon->getSize().lx * m_icon->getSize().ly; i++, buf++) {
Shinya Kitaoka 120a6e
    assert(buf->r == buf->g && buf->r == buf->b);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (buf->r < 255) {
Shinya Kitaoka 120a6e
      if (buf->r == 0)
Shinya Kitaoka 120a6e
        *buf = color;
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        int val = 255 - buf->r;
Shinya Kitaoka 120a6e
        buf->r  = (UCHAR)(val * rd + 255);
Shinya Kitaoka 120a6e
        buf->g  = (UCHAR)(val * gd + 255);
Shinya Kitaoka 120a6e
        buf->b  = (UCHAR)(val * bd + 255);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_icon->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
//    TBlendRasterStyle  implementation
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TColorStyle *TBlendRasterStyle::clone() const {
Shinya Kitaoka 120a6e
  return new TBlendRasterStyle(*this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TBlendRasterStyle::compute(const Params ¶ms) const {
Shinya Kitaoka 120a6e
  int i, j;
Shinya Kitaoka 120a6e
  TRasterGR8P r = (TRasterGR8P)params.m_r;
Shinya Kitaoka 120a6e
  assert(r);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double factor = computeFactor(params);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_blur > 0.0) TRop::blur(params.m_r, params.m_r, m_blur, 0, 0, true);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  r->lock();
Shinya Kitaoka 120a6e
  for (i = 0; i < r->getLy(); i++) {
Shinya Kitaoka 120a6e
    TPixelGR8 *bufGr = r->pixels(i);
Shinya Kitaoka 120a6e
    for (j = 0; j < r->getLx(); j++, bufGr++)
Shinya Kitaoka 120a6e
      if (bufGr->value > 0) {
Shinya Kitaoka 120a6e
        double val   = bufGr->value * factor + 0.5;
Shinya Kitaoka 120a6e
        bufGr->value = (UCHAR)((val > 255) ? 255 : val);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  r->unlock();
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
bool inline doIsBorderPix(int index, TPixelCM32 *pix, TPixelGR8 *pixGr,
Shinya Kitaoka 120a6e
                          int paint) {
Shinya Kitaoka 120a6e
  return (pix->getPaint() != paint &&
Shinya Kitaoka 120a6e
          (pix->isPurePaint() || pixGr->value != 0));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool inline isBorderPix(int index, TPixelCM32 *pix, int wrap, TPixelGR8 *pixGr,
Shinya Kitaoka 120a6e
                        int wrapGr, int color) {
Shinya Kitaoka 120a6e
  return doIsBorderPix(index, pix + 1, pixGr + 1, color) ||
Shinya Kitaoka 120a6e
         doIsBorderPix(index, pix - 1, pixGr - 1, color) ||
Shinya Kitaoka 120a6e
         doIsBorderPix(index, pix + wrap, pixGr + wrapGr, color) ||
Shinya Kitaoka 120a6e
         doIsBorderPix(index, pix - wrap, pixGr - wrapGr, color);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TBlendRasterStyle::makeIcon(const TDimension &d) {
Shinya Kitaoka 120a6e
  TFilePath dir = TEnv::getStuffDir() + "pixmaps";
Shinya Kitaoka 120a6e
  static TRasterP normalIc;
Shinya Kitaoka 120a6e
  if (normalIc == TRasterP()) TImageReader::load(dir + "blend.bmp", normalIc);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  arrangeIcon(d, normalIc);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TBlendRasterStyle::computeFactor(const Params ¶ms) const {
Shinya Kitaoka 120a6e
  TRasterCM32P rCm = (TRasterCM32P)params.m_rOrig;
Shinya Kitaoka 120a6e
  TRasterGR8P rGr  = (TRasterGR8P)params.m_r;
Shinya Kitaoka 120a6e
  TPixelGR8 *bufGr;
Shinya Kitaoka 120a6e
  TPixelCM32 *bufCmap;
Shinya Kitaoka 120a6e
  int i, j;
Shinya Kitaoka 120a6e
  int wr         = params.m_rOrig->getWrap();
Shinya Kitaoka 120a6e
  int wrGr       = params.m_r->getWrap();
Shinya Kitaoka 120a6e
  int num_lines  = 0;
Shinya Kitaoka 120a6e
  double tot_sum = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // interactive!
Shinya Kitaoka 120a6e
  // if (Factor == 0)
Shinya Kitaoka 120a6e
  rGr->lock();
Shinya Kitaoka 120a6e
  rCm->lock();
Shinya Kitaoka 120a6e
  for (i = 1; i < params.m_r->getLy() - 1; i++) {
Shinya Kitaoka 120a6e
    int line_sum = 0, num_points = 0;
Shinya Kitaoka 120a6e
    bufGr   = rGr->pixels(i) + 1;
Shinya Kitaoka 120a6e
    bufCmap = rCm->pixels(i) + 1;
Shinya Kitaoka 120a6e
    for (j = 1; j < rGr->getLx() - 1; j++, bufGr++, bufCmap++) {
Shinya Kitaoka 120a6e
      if (bufGr->value > 0) {
Shinya Kitaoka 120a6e
        int paint = bufCmap->getPaint();
Shinya Kitaoka 120a6e
        if ((bufCmap->isPurePaint() || bufGr->value != 0) &&
Shinya Kitaoka 120a6e
            isBorderPix(params.m_colorIndex, bufCmap, wr, bufGr, wrGr, paint)) {
Shinya Kitaoka 120a6e
          line_sum += troundp(bufGr->value);
Shinya Kitaoka 120a6e
          num_points++;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    rGr->unlock();
Shinya Kitaoka 120a6e
    rCm->unlock();
Shinya Kitaoka 120a6e
    if (num_points != 0) {
Shinya Kitaoka 120a6e
      tot_sum += ((float)line_sum / ((float)num_points * 255));
Shinya Kitaoka 120a6e
      num_lines++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (num_lines == 0) return 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return num_lines / tot_sum;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
//    TNoColorRasterStyle  implementation
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TNoColorRasterStyle::makeIcon(const TDimension &d) {
Shinya Kitaoka 120a6e
  TFilePath dir = TEnv::getStuffDir() + "pixmaps";
Shinya Kitaoka 120a6e
  static TRasterP normalIc;
Shinya Kitaoka 120a6e
  if (normalIc == TRasterP()) TImageReader::load(dir + "markup.bmp", normalIc);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (normalIc == TRasterP()) {
Shinya Kitaoka 120a6e
    m_icon = TRaster32P(d);
Shinya Kitaoka 120a6e
    m_icon->fill(TPixel32::Red);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_icon = TRasterP();
Shinya Kitaoka 120a6e
  if (d == TDimension(52, 52) && normalIc != TRasterP())
Shinya Kitaoka 120a6e
    m_icon = normalIc->clone();
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    m_icon = TRaster32P(d);
Shinya Kitaoka 120a6e
    TRop::resample(m_icon, normalIc, TScale(d.lx / 52.0, d.ly / 52.0));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}