gettingstarted
[Top] [All Lists]

Re: Faster image manipulation?

To: Getting Started <gettingstarted at lists dot realsoftware dot com>
Subject: Re: Faster image manipulation?
From: Phil M <phil at mobleybros dot com>
Date: Fri, 28 Jan 2005 12:49:02 -0800
Delivered-to: gettingstarted at lists dot realsoftware dot com
References: <B34727EE-716B-11D9-BC13-000A95E636AC at utkux dot utcc dot utk dot edu>

On Jan 28, 2005, at 12:32 PM, Jeff Borckardt wrote:

I have a program that takes several full-color images, and wherever each image displays a predetermined color (for example blue), the program turns it green and coverts all other sections of the picture to greyscale. Here is the code that is placed in a thread in the program.

cor(c) is an array of the original pictures, cor2(c) is an array of the manipulated pictures, temp is a color, q is an integer

  w=cor(c).width
  h=cor(c).height
  for k=1 to 217

why 217? Are you looping through each color of a web-based index color mode? If yes, you could probably do a lot better here.

if c2done(k)=false then //this is a marker to prevent duplication of the process below if stopthread=true then exit //this global variable is true if a cancel button is clicked
      for i=0 to w-1
        for j=0 to h-1
          temp=cor(k).rgbSurface.pixel(i,j) //gets the color of a pixel
          q=temp.red //stores the integer value of 'red' for later
if temp=c1 then //sees if the color is one that gets turned green or grayscale
            cor2(k).rgbsurface.pixel(i,j)=rgb(0,255,0) //green
          else
            cor2(k).rgbsurface.pixel(i,j)=rgb(q,q,q) //greyscale
          end
        next
      next
      c2done(k)=true //mark this picture as converted
    end
  next

This takes a long time... is there a faster way?

Three optimizations for the inner parts:

1. Do not use (w-1) and (h-1) but precalculate them -- a small improvement, but it will help. Right now, every loop iteration is calculating these (just in case w or h has changed). Since they cannot change in this context, you should use something like:

   wMax = w - 1
   hMax = h - 1

2. Set the Height to be the outer loop. By looping as you are right now, you are jumping around a lot in memory -- a small improvement, but it will help:

   for i=0 to hMax
      for j=0 to wMax

3. Everytime through a loop costs CPU. If you can do more tasks in a single loop run, then it will be faster. For example, getting the values of 8 pixels in a row. There is a pretty good article on this in the REALbasic Developer magazine which compares the speed of processing 64 pixels in one loop pass compared to 1 pixel.

4. Pragmas. I honestly do not recommend using the #DisableBackgroundTasks and other Pragmas until you are positive your code is *rock-solid*.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>