void TransferToWx(const Map& map, wxImage& wx)
{
    wx.Create(map.width * 8, map.height * 8);
    if (map.tileset->bpp == 4)
    {
        for (unsigned int i = 0; i < map.data.size(); i++)
        {
            int x = i % map.width;
            int y = i / map.width;
            int tile_id = map.data[i] & 0x3FF;
            int pal_id = (map.data[i] >> 12) & 0xF;
            const Tile& tile = map.tileset->tilesExport[tile_id];
            const PaletteBank& palette = map.tileset->paletteBanks[pal_id];
            for (unsigned int j = 0; j < TILE_SIZE; j++)
            {
                unsigned char pix = tile.pixels[j];
                if (!pix) continue;
                const auto& c = palette.At(pix);
                wx.SetRGB(x * 8 + j % 8, y * 8 + j / 8, c.r << 3, c.g << 3, c.b << 3);
            }
        }
    }
    else
    {
        for (unsigned int i = 0; i < map.data.size(); i++)
void TransferToWx(const Palette& palette, wxImage& wx)
{
    wx.Create(16, 16);
    for (unsigned int i = 0; i < palette.Size(); i++)
    {
        const Color16& c = palette.At(i);
        wx.SetRGB(i % 16, i / 16, c.r << 3, c.g << 3, c.b << 3);
    }
}
void TransferToWx(const Tileset& tileset, wxImage& wx)
{
    // 16 x 64
    const int tilesX = 32;
    const int tilesY = 32;
    wx.Create(tilesX * 8, tilesY * 8);
    int bpp = tileset.bpp;
    if (bpp == 4)
    {
        for (unsigned int i = 0; i < tileset.tilesExport.size(); i++)
        {
            const Tile& tile = tileset.tilesExport[i];
            const PaletteBank& palette = tileset.paletteBanks[tile.palette_bank];
            int tx = i % tilesX;
            int ty = i / tilesX;
            for (unsigned int j = 0; j < TILE_SIZE; j++)
            {
                unsigned char pix = tile.pixels[j];
                const auto& c = palette.At(pix);
                wx.SetRGB(tx * 8 + j % 8, ty * 8 + j / 8, c.r << 3, c.g << 3, c.b << 3);
            }
        }
    }
    else
    {
        const Palette& palette = *tileset.palette;
        for (unsigned int i = 0; i < tileset.tilesExport.size(); i++)
        {
            const Tile& tile = tileset.tilesExport[i];
            int tx = i % tilesX;
            int ty = i / tilesX;
            for (unsigned int j = 0; j < TILE_SIZE; j++)
            {
                unsigned char pix = tile.pixels[j];
                const auto& c = palette.At(pix);
                wx.SetRGB(tx * 8 + j % 8, ty * 8 + j / 8, c.r << 3, c.g << 3, c.b << 3);
            }
        }
    }
}
void TransferToWx(const Image16Bpp& image, wxImage& wx)
{
    wx.Create(image.width, image.height);

    for (unsigned int i = 0; i < image.height; i++)
    {
        for (unsigned int j = 0; j < image.width; j++)
        {
            const Color16& c = image.At(j, i);
            wx.SetRGB(j, i, c.r << 3, c.g << 3, c.b << 3);
        }
    }
}
void BM2CMP_FRAME::NegateGreyscaleImage( )
{
    unsigned char  pix;
    int             h = m_Greyscale_Image.GetHeight();
    int             w = m_Greyscale_Image.GetWidth();

    for( int y = 0; y < h; y++ )
        for( int x = 0; x < w; x++ )
        {
            pix   = m_Greyscale_Image.GetGreen( x, y );
            pix = ~pix;
            m_Greyscale_Image.SetRGB( x, y, pix, pix, pix );
        }
}
void TransferToWx(const Sprite& sprite, const std::vector<PaletteBank>& banks, wxImage& wx)
{
    wx.Create(sprite.width * 8, sprite.height * 8);
    int tilesX = sprite.width;
    if (sprite.bpp == 4)
    {
        for (unsigned int i = 0; i < sprite.data.size(); i++)
        {
            const Tile& tile = sprite.data[i];
            const PaletteBank& palette = banks[sprite.palette_bank];
            int tx = i % tilesX;
            int ty = i / tilesX;
            for (unsigned int j = 0; j < TILE_SIZE; j++)
            {
                unsigned char pix = tile.pixels[j];
                const auto& c = palette.At(pix);
                wx.SetRGB(tx * 8 + j % 8, ty * 8 + j / 8, c.r << 3, c.g << 3, c.b << 3);
            }
        }
    }
    else
    {
        const Palette& palette = *sprite.palette;
        for (unsigned int i = 0; i < sprite.data.size(); i++)
        {
            const Tile& tile = sprite.data[i];
            int tx = i % tilesX;
            int ty = i / tilesX;
            for (unsigned int j = 0; j < TILE_SIZE; j++)
            {
                unsigned char pix = tile.pixels[j];
                const auto& c = palette.At(pix);
                wx.SetRGB(tx * 8 + j % 8, ty * 8 + j / 8, c.r << 3, c.g << 3, c.b << 3);
            }
        }
    }
}
void TransferToWx(const std::vector<PaletteBank>& banks, wxImage& wx)
{
    wx.Create(16, 16);
    for (unsigned int bank_id = 0; bank_id < banks.size(); bank_id++)
    {
        const PaletteBank& bank = banks[bank_id];
        int bx = (bank_id % 4) * 4;
        int by = (bank_id / 4) * 4;
        for (unsigned int i = 0; i < bank.Size(); i++)
        {
            const Color16& c = bank.At(i);
            wx.SetRGB(i % 4 + bx, i / 4 + by, c.r << 3, c.g << 3, c.b << 3);
        }
    }
}
void BM2CMP_FRAME::Binarize( double aThreshold )
{
    unsigned int  pixin;
    unsigned char pixout;
    int           h = m_Greyscale_Image.GetHeight();
    int           w = m_Greyscale_Image.GetWidth();
    unsigned int  threshold = (int)(aThreshold * 256);

    for( int y = 0; y < h; y++ )
        for( int x = 0; x < w; x++ )
        {
            pixin   = m_Greyscale_Image.GetGreen( x, y );

            if( pixin < threshold )
                pixout = 0;
            else
                pixout = 255;

            m_NB_Image.SetRGB( x, y, pixout, pixout, pixout );
        }

    m_BN_Bitmap = wxBitmap( m_NB_Image );
}
示例#9
0
wxImage ScaleImage(wxImage image, double source_scale_factor, double content_scale_factor,
                   wxSize output_size, wxRect usable_rect, LSIFlags flags,
                   const wxColour& fill_color)
{
  if (!image.IsOk())
  {
    wxFAIL_MSG("WxUtils::ScaleImage expects a valid image.");
    return image;
  }

  if (content_scale_factor != 1.0)
  {
    output_size *= content_scale_factor;
    usable_rect.SetPosition(usable_rect.GetPosition() * content_scale_factor);
    usable_rect.SetSize(usable_rect.GetSize() * content_scale_factor);
  }

  // Fix the output size if it's unset.
  wxSize img_size = image.GetSize();
  if (output_size.GetWidth() < 1)
    output_size.SetWidth(
        static_cast<int>(img_size.GetWidth() * (content_scale_factor / source_scale_factor)));
  if (output_size.GetHeight() < 1)
    output_size.SetHeight(
        static_cast<int>(img_size.GetHeight() * (content_scale_factor / source_scale_factor)));

  // Fix the usable rect. If it's empty then the whole canvas is usable.
  if (usable_rect.IsEmpty())
  {
    // Constructs a temp wxRect 0,0->output_size then move assigns it.
    usable_rect = output_size;
  }
  else if (!usable_rect.Intersects(output_size))
  {
    wxFAIL_MSG("Usable Zone Rectangle is not inside the canvas. Check the output size is correct.");
    image.Create(1, 1, false);
    image.SetRGB(0, 0, fill_color.Red(), fill_color.Green(), fill_color.Blue());
    if (fill_color.Alpha() == wxALPHA_TRANSPARENT)
      image.SetMaskColour(fill_color.Red(), fill_color.Green(), fill_color.Blue());
    usable_rect = output_size;
  }

  // Step 1: Scale the image
  if ((flags & LSI_SCALE) != LSI_SCALE_NONE)
  {
    if (flags & LSI_SCALE_NO_ASPECT)
    {
      // Stretch scale without preserving the aspect ratio.
      bool scale_width = (img_size.GetWidth() > usable_rect.GetWidth() && flags & LSI_SCALE_DOWN) ||
                         (img_size.GetWidth() < usable_rect.GetWidth() && flags & LSI_SCALE_UP);
      bool scale_height =
          (img_size.GetHeight() > usable_rect.GetHeight() && flags & LSI_SCALE_DOWN) ||
          (img_size.GetHeight() < usable_rect.GetHeight() && flags & LSI_SCALE_UP);
      if (scale_width || scale_height)
      {
        // NOTE: Using BICUBIC instead of HIGH because it's the same internally
        //   except that downscaling uses a box filter with awful obvious aliasing
        //   for non-integral scale factors.
        image.Rescale(scale_width ? usable_rect.GetWidth() : img_size.GetWidth(),
                      scale_height ? usable_rect.GetHeight() : img_size.GetHeight(),
                      wxIMAGE_QUALITY_BICUBIC);
      }
    }
    else
    {
      // Scale while preserving the aspect ratio.
      double scale = std::min(static_cast<double>(usable_rect.GetWidth()) / img_size.GetWidth(),
                              static_cast<double>(usable_rect.GetHeight()) / img_size.GetHeight());
      int target_width = static_cast<int>(img_size.GetWidth() * scale);
      int target_height = static_cast<int>(img_size.GetHeight() * scale);
      // Bilinear produces sharper images when upscaling, bicubic tends to smear/blur sharp edges.
      if (scale > 1.0 && flags & LSI_SCALE_UP)
        image.Rescale(target_width, target_height, wxIMAGE_QUALITY_BILINEAR);
      else if (scale < 1.0 && flags & LSI_SCALE_DOWN)
        image.Rescale(target_width, target_height, wxIMAGE_QUALITY_BICUBIC);
    }
    img_size = image.GetSize();
  }

  // Step 2: Resize the canvas to match the output size.
  // NOTE: If NOT using LSI_SCALE_DOWN then this will implicitly crop the image
  if (img_size != output_size || usable_rect.GetPosition() != wxPoint())
  {
    wxPoint base = usable_rect.GetPosition();
    if (flags & LSI_ALIGN_HCENTER)
      base.x += (usable_rect.GetWidth() - img_size.GetWidth()) / 2;
    else if (flags & LSI_ALIGN_RIGHT)
      base.x += usable_rect.GetWidth() - img_size.GetWidth();
    if (flags & LSI_ALIGN_VCENTER)
      base.y += (usable_rect.GetHeight() - img_size.GetHeight()) / 2;
    else if (flags & LSI_ALIGN_BOTTOM)
      base.y += usable_rect.GetHeight() - img_size.GetHeight();

    int r = -1, g = -1, b = -1;
    if (fill_color.Alpha() != wxALPHA_TRANSPARENT)
    {
      r = fill_color.Red();
      g = fill_color.Green();
      b = fill_color.Blue();
    }
    image.Resize(output_size, base, r, g, b);
  }

  return image;
}