Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:01:04+00:00 2026-06-12T13:01:04+00:00

Right, so let’s say I want to compare two BitmapDatas. One is an image

  • 0

Right, so let’s say I want to compare two BitmapDatas. One is an image of a background (not solid, it has varying pixels), and another is of something (like a sprite) on top of the exact same background. Now what I want to do is remove the background from the second image, by comparing the two images, and removing all the pixels from the background that are present in the second image.
For clarity, basically I want to do this in AS3.

Now I came up with two ways to do this, and they both work perfectly. One compares pixels directly, while the other uses the BitmapData.compare() method first, then copies the appropriate pixels into the result. What I want to know is which way is faster.

Here are my two ways of doing it:

Method 1

for (var j:int = 0; j < layer1.height; j++)
{
    for (var i:int = 0; i < layer1.width; i++)
    {
        if (layer1.getPixel32(i, j) != layer2.getPixel32(i, j))
        {
            result.setPixel32(i, j, layer2.getPixel32(i, j));
        }
    }
}

Method 2

result = layer1.compare(layer2) as BitmapData;

for (var j:int = 0; j < layer1.height; j++)
{
    for (var i:int = 0; i < layer1.width; i++)
    {
        if (result.getPixel32(i, j) != 0x00000000)
        {
            result.setPixel32(i, j, layer2.getPixel32(i, j));
        }
    }
}

layer1 is the background, layer2 is the image the background will be removed from, and result is just a BitmapData that the result will come out on.

These are very similar, and personally I haven’t noticed any difference in speed, but I was just wondering if anybody knows which would be faster. I’ll probably use Method 1 either way, since BitmapData.compare() doesn’t compare pixel alpha unless the colours are identical, but I still thought it wouldn’t hurt to ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:01:05+00:00Added an answer on June 12, 2026 at 1:01 pm

    This might not be 100% applicable to your situation, but FWIW I did some research into this a while back, here’s what I wrote back then:

    I’ve been meaning to try out grant skinners performance test thing for a while, so this was my opportunity.

    I tested the native compare, the iterative compare, a reverse iterative compare (because i know they’re a bit faster) and finally a compare using blendmode DIFFERENCE.

    The reverse iterative compare looks like this:

    for (var bx:int = _base.width - 1; bx >= 0; --bx) {
     for (var by:int = _base.height - 1; by >= 0; --by) {
      if (_base.getPixel32(bx, by) != compareTo.getPixel32(bx, by)) {
       return false;
      }
     }
    }
    return true;
    

    The blendmode compare looks like this:

    var test:BitmapData = _base.clone();
    test.draw(compareTo, null, null, BlendMode.DIFFERENCE);
    var rect:Rectangle = test.getColorBoundsRect(0xffffff, 0x000000, false);
    return (rect.toString() != _base.rect.toString());
    

    I’m not 100% sure this is completely reliable, but it seemed to work.

    I ran each test for 50 iterations on 500×500 images.

    Unfortunately my pixel bender toolkit is borked, so I couldn’t try that method.

    Each test was run in both the debug and release players for comparison, notice the massive differences!

    Complete code is here: http://webbfarbror.se/dump/bitmapdata-compare.zip

    graph

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me just say right off the bat that i'm not a programmer. I'm
I've got two tables (let's say left and right tables). The right table's got
Let's say I want to move a part of an array right by 1.
Let's say my app does something unrecoverable (this never happens, right? :P ). Should
Let's say I haven a Listbox, or as I do right now a Wrap
I'm new to JSF and am wondering if I got things right. Let's say
I'm writing some documentation and I just can't find the right word. Let say
Let's say I have a recursive data structure class Tree { private Tree right;
I am displaying two datatables (let's call them left and right) in two datagrids
So let me apologize right up front. I'm not a PHP programmer, I'm a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.