Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 17th, 2016, 3:46 AM   #1
colt
Programmer
 
Join Date: Mar 2005
Posts: 34
Rep Power: 0 colt is on a distinguished road
Thumbs up Working over only 1/3 of image pixels

Hello, I currently doing a program that read a image from a .tga file into memory, do some stuff over it and then write it back to a new .tga file.
I create an array to hold it with size being equal to width*height*bytesPerPixel.
Then I iterate over it doing some processing. The iterate process is done by two nested for loop

         int size = width*height*bpp;
         char imageArray [size];
         char * arrayPtr = &imageArray[0];
         for (int x=0; x<width; x++) {
            for (int y=0; y<height; y++) {
               imageArray [x*height + 3*y] = 255;
               imageArray [x*height + 3*y + 1] = 0;
               imageArray [x*height + 3*y + 2] = 0;
            }
         }
         fileWriter.write (arrayPtr, size);

the operations inside each loop iteration work over the three rgb values. Here I am making the image fully red just to try to make the code's purpose clear to anyone.
The problem is that only one third of the image is being modified, the other two thirds remains the same of the original image (just image a random image, the bottom third of it will be fully red, the rest will be unmodified)
Since I do three operation in each loop iteration, the number of operations being made is equal to the size of the image (there is three bytes per pixel, one for each color component)
So I don't know what I am doing wrong. Any suggestions will be appreciated.
colt is offline   Reply With Quote
Old Jun 17th, 2016, 7:15 PM   #2
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: Working over only 1/3 of image pixels

Man I read into this .tga stuff and it just sounds like a mess.

https://en.wikipedia.org/wiki/Truevision_TGA
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old Jun 20th, 2016, 10:55 AM   #3
FlakkenTime
Software Developer
 
FlakkenTime's Avatar
 
Join Date: Apr 2010
Location: Bay Area, CA
Posts: 268
Rep Power: 8 FlakkenTime is on a distinguished road
Re: Working over only 1/3 of image pixels

Quote:
Originally Posted by colt View Post
         int size = width*height*bpp;
         char imageArray [size];
         char * arrayPtr = &imageArray[0];
         for (int x=0; x<width; x++) {
            for (int y=0; y<height; y++) {
               imageArray [x*height + 3*y] = 255;
               imageArray [x*height + 3*y + 1] = 0;
               imageArray [x*height + 3*y + 2] = 0;
            }
         }
Fairly certain your issue is the 3*y. I don't know anything about this .tga format but I did an image processor project in college. Your loops seem wrong to me.

1. the *3 explains why you're only seeing a third of the image colored. Note your inner loop is y. This means you are coloring bottom up then left to right. (Just so you know).
2. If you want to do this line by line of pixles think of it like the cartesian coordinate system from math. Y is the row and X is the column.

Note I've never used ImageArray so I don't know if thats a library or something you built. I'm assuming ImageArray[x_coordinate, y_coordinate, index_for_color]
for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
        ImageArray[x, y, 0] = 255;
        ImageArray[x, y, 1] = 0;
        ImageArray[x, y, 2] = 0;
    }
}

Note I'm not a C++ developer so this is mostly from memories of being in school
FlakkenTime is offline   Reply With Quote
Old Jun 23rd, 2016, 8:13 PM   #4
colt
Programmer
 
Join Date: Mar 2005
Posts: 34
Rep Power: 0 colt is on a distinguished road
Re: Working over only 1/3 of image pixels

Quote:
Originally Posted by FlakkenTime View Post
Fairly certain your issue is the 3*y. I don't know anything about this .tga format but I did an image processor project in college. Your loops seem wrong to me.

1. the *3 explains why you're only seeing a third of the image colored. Note your inner loop is y. This means you are coloring bottom up then left to right. (Just so you know).
2. If you want to do this line by line of pixles think of it like the cartesian coordinate system from math. Y is the row and X is the column.

Note I've never used ImageArray so I don't know if thats a library or something you built. I'm assuming ImageArray[x_coordinate, y_coordinate, index_for_color]
for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
        ImageArray[x, y, 0] = 255;
        ImageArray[x, y, 1] = 0;
        ImageArray[x, y, 2] = 0;
    }
}

Note I'm not a C++ developer so this is mostly from memories of being in school
ImageArray was just an array, nothing to do with libraries. Anyway, the problem was that it should be
3*(x*height +y)
instead of
(x*height + 3*y)

The old way it write over several pixels more than once. Example: When it reaches the end of the first row, it will be writing the bytes: 0*768 + 3*767, 0*768 + 3*767 +1 and 0*768 + 3*767 +2. In the next iteration it will be writing the chars of numbers: 1*768 + 0*3, 1*768 + 0*3 +1 and 1*768 + 0*3* + 2, instead 3*768 + 3*0, 3*768 + 3*0 +1 and 3*768 + 3*0 +2.
colt is offline   Reply With Quote
Old Jun 23rd, 2016, 11:58 PM   #5
FlakkenTime
Software Developer
 
FlakkenTime's Avatar
 
Join Date: Apr 2010
Location: Bay Area, CA
Posts: 268
Rep Power: 8 FlakkenTime is on a distinguished road
Re: Working over only 1/3 of image pixels

Glad you figured it out! :-)
FlakkenTime is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookie Stuffing through an image + faking referrer ben7171 PHP 6 Jan 6th, 2011 5:30 PM
Checking source codes of image, audio and video files on_auc C++ 5 Jan 1st, 2010 10:14 AM
Manipulating the Pixels in an Image djm_2k1 Java 0 Jul 8th, 2005 10:08 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC