Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I frequent other newsgroups, too, and am MOST IMPRESSED with the lack of smart a-- and presence of genuine desire to help anyone of any skill level..."

Geography

Where in the world do Tek-Tips members come from?

How to create greyscale pixel value array

HuntsvilleRob (Programmer)
3 Nov 10 18:06
Hello.
Thought I'd ask; having a hard time figuring this out on my own. I am using Borland C++ Builder 6 on a PC running Windows XP.

I have a black and white image; I can put it in any format, bmp, jpg, tif, whatever. The image has pixel dimensions 500 x 500. Each pixel is a greyscale value from 0 (black)to 2^16 (white). I want to write C++ code that will read the image file and create an array, call it DataArray[500][500] where DataArray[i][j] is the greyscale value for the pixel in the ith row, the jth column. How can I create such an array? This would allow me, for example, to create a graph of the greyscale values along any column (for all the rows) or along any row (for all the column values in that row). Any help would be greatly appreciated. Thanks.

-Rob
smays (Programmer)
4 Nov 10 14:00
Have you tried Graphics::TBitmap?  The members of the TCanvas of TBitmap include the Pixels property where you can access each pixel individually and the members of TBitmap include a ScanLine method where one can access either a horizontal or a vertical line of pixels via index.

If you don't mind me being nosy, HuntsvilleRob as in Huntsville, AL?

Steve.
HuntsvilleRob (Programmer)
4 Nov 10 14:26
Do you have any example code you could recommend?

Yes, Huntsville, AL!

Thanks.  -Rob
smays (Programmer)
4 Nov 10 17:49
...from memory:

CODE

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Graphics::TBitmap *bmpImage = new Graphics::TBitmap;
    AnsiString asImagePathNFilename = edtImagePathNFilename->Text;
    int iImageWidth, iImageHeight;

    bmpImage->LoadFromFile(asImagePathNFilename);
    iImageWidth = bmpImage->Width;
    iImageHeight = bmpImage->Height;
    for (iWhichXPixel = 0; iWhichXPixel < iImageWidth; iWhichXPixel++)
    {
        for (iWhichYPixel = 0; iWhichYPixel < iImageHeight; iWhichYPixel++)
        {
            bmpImage->Canvas->Pixels[iWhichXPixel][iWhichYPixel] = 0xFFFFFF - bmpImage->Canvas->Pixels[iWhichXPixel][iWhichYPixel];
        }
    }
    delete bmpImage;
}

You can explore TBitmap to squeeze out of it what you need.  I have not used the ScanLine member before.  Below is some example code from Builder help (by the way, TCanvas has a EASY TO USE StretchDraw method that will allow you to easily copy and double the size of an image):

CODE

/*
This example shows how to draw directly to a Bitmap.  It
loads a bitmap from a file and then copies it to another
bitmap twice it's size.  Then the two bitmaps are
displayed on the form canvas.
*/

#include <memory>       //for STL auto_ptr class

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::auto_ptr<Graphics::TBitmap> Bitmap(new Graphics::TBitmap);
  std::auto_ptr<Graphics::TBitmap> BigBitmap(new Graphics::TBitmap);
  TRGBTriple *ptr, *bigPtr;   // use a (byte *) for pf8bit color
  TPixelFormat pixForm, bigpixForm;
  try
  {
    Bitmap->LoadFromFile("../littlefac.bmp");
    pixForm = Bitmap->PixelFormat;
    bigpixForm  = BigBitmap->PixelFormat;
    Bitmap->PixelFormat = pf24bit;
    BigBitmap->PixelFormat = pf24bit;
    BigBitmap->Height = Bitmap->Height * 2;
    BigBitmap->Width = Bitmap->Width * 2;
    for (int y = 0; y < Bitmap->Height; y++)
    {
      ptr = reinterpret_cast<TRGBTriple *>(Bitmap->ScanLine[y]);
      for (int x = 0; x < Bitmap->Width; x++)
      {
        int bx = x * 2;
        int by = y * 2;
        bigPtr = reinterpret_cast<TRGBTriple *>(BigBitmap->ScanLine[by]);
        bigPtr[bx] = ptr[x];
        bigPtr[bx + 1] = ptr[x];
        bigPtr = reinterpret_cast<TRGBTriple *>(BigBitmap->ScanLine[by + 1]);
        bigPtr[bx] = ptr[x];
        bigPtr[bx + 1] = ptr[x];
      }
    }
    Canvas->Draw(0, 0, Bitmap.get());
    Canvas->Draw(200, 200, BigBitmap.get());
  }
  catch (...)
  {
    ShowMessage("Could not load or alter bitmap");
  }
}

I use the Pixels member of TCanvas, which is a member class of TBitmap, extensively.  Pixels returns a TColor, which is four byte value (the upper-most byte is reserved by Builder, leaving the lower three bytes accessible as the color of the pixel).  TBitmap supports practically all bitmap formats, including gray scale images and reduced color depth images.

CODE

    __property TColor Pixels[int X][int Y] = {read=GetPixel, write=SetPixel};

There are corresponding classes for JPEGs (jpeg.hpp) and PNGs (pngimage.hpp), which I have used in several programs.

Ultimately, if you can't get these to work, you will have to study the format of the image preambles so you can:
1.)  Access the actual pixel data.  The first 10's to 100's of bytes of image formats are preambles describing the image file.  The classes handle all that for you.
2.)  Decompress the image as necessary.

Hello from Madison, AL!  Hope this helps!
Steve.
HuntsvilleRob (Programmer)
4 Nov 10 19:28
Hello, Steve!  Thanks very much for the example code; this is just what I was looking for!  I can now proceed.

Funny, looking for help in a world-wide forum leads me to the answer from a guy 10 minutes down 565 from me.  So strange!

-Rob
smays (Programmer)
4 Nov 10 20:25
With all the LabView crammed down my throat for engineering automation here in town, it is refreshing to know there is at least one other person here in town who uses Builder!

Let me know if you need anything else!

Steve.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close