PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Tutorial 2 - Hidden C test?

Just going through the tutorials on VC++ while waiting for eMbeddedwhatevertheheckmicrosoftnamedtheirsoftware to complete downloading and installing.  Had a couple of Q's:

1.  I noticed a comment in the tutorial that there are some difference between windows CE and Win 9x, and they'll be talked about later.  But there isn't anything mentioned in the tutorial?  I ask because..

2.  When I try to compile loadtga, I get errors on the CreateFile function that its first parameter should be a const char *, not const short *.  I assume this is somehow Unicode related?  I change the parameter to char, and the L"..." string to "..." and it will load files, so I guess that's ok.

3.  I have to use 24 bit Targa files (curses ThumbsPlus! I thought better of you...).  Wierd thing is that in order to load them properly, I have to reverse the position of red and blue.  Wierd, huh?  Here is the code I'm using to load 24 bit TIFF's:

Code:

// Function to load standard TARGA files.  Must be uncompressed, 24 bit format
void loadtga24( const char* file, int w, int h, PIXEL* dest )
{
     int red=0, green=0, blue=0;

     DWORD size = w * 3 * h + 20, bytesread;
     BYTE* tgabuff = new BYTE[size];
     HANDLE tga = CreateFile( file, GENERIC_READ, FILE_SHARE_READ,
           0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );

     ReadFile( tga, tgabuff, size, &bytesread, 0 );
     CloseHandle( tga );
     unsigned char* src = (unsigned char*)tgabuff + 18 + (h - 1) * w * 3;
     unsigned short* dst = (unsigned short*)dest;
     for ( int i = 0; i < h; i++ )
     {
           for ( int x = 0; x < w; x++ )
           {
                 //For some odd reason, Targa 24 bit files are red/blue reversed.
                 blue   = *(src + x*3 ) >> 3;
                 green = *(src + x*3 + 1) >> 2;
                 red  = *(src + x*3 + 2) >> 3;
                 *(dst + x) = (red << 11) + (green << 5) + blue;
           }
           dst += w;
           src -= w*3;
     }
     delete tgabuff;
}

Hey, I don't get it either, but WTF, it works!

4.  Couple of issues with the "average" example.  Like, it doesn't average!  The example does a / by 4, which isn't really going to average the colors because Green is given an extra bit.  So what I did is tack an extra bit onto red and blue, and divide by 6:
Code:

     int total = (red >> 10) + (green >> 5) + (blue << 1);
     int average = total / 6;

5.  Second, the "average" example uses the "total" number, not the "average" number it just calculated! D'oh!

6.  Third, the examples multiply y by 320.  Shouldn't that be 240, the width of one line?

Anyways, like the tutorial alot.  I'm hoping to, like probably alot of people, write games in my "spare" time.  Yeah, right. :)
Last modification: Richard Hendricks - 01/02/02 at 02:19:51
Last modification: Richard Hendricks - 01/02/02 at 02:20:16
ello, I will try to answer your questions:
1.Well, you should simple refer to EVC++ help if you get any problems. It explains very well everything.
2.Yep, W9x doesn't support UNICODE only w2k, wxp and wce supports UNICODE.
3.Yep, the same for me, that's how .tga files are stored
4.well it's hard to help you here because I haven't read it thru, but just few ideas:
a)Jacco might keep the buffer in 320x240 format rather then 240x320 so it works best with IPAQ devices.
b)it seems like you're right about the average/total numbers stuff looks like there's something for Jacco to fix

hope it helps a lil:)
Well, Mirek, thanks for answering most of the questions, safes (saves? When do you use an 'f' and when a 'v'?) me some typing. :)

About the averaging: I think you're talking about the greyscale stuff. Problem here is that you want it to be fast. Therefore, I divide by 4 rather than 3, because dividing by four can be achieved with a (very fast) shift operation. Of course, it shouldn't be called an average. :) I called it an 'average' because a real greyscale would be the average of red, green and blue. So this is the approximate average. :)

I can see some people thinking 'hey, greyscales are not calculated by averaging r g and b' so to shut them up and to show you how much I know about color stuff: :) To get a really good greyscale converter you need to multiply r, g and b by three constants that are not 1. :) That's because the eye is more sensitive to some color components.

About the multiply-by-240: Yep, you're right there. There are some major bugs in tutorial 2. Since I get so few questions about that, I'll leave it as an excercise to the reader. :)
my english is poor, but:
save - to save something(that sounds pretty dumb) like on disk, it saves you some typing etc.
safe - something is secure like my data is safe because I saved it. This network is safe etc.

Hope you can see the difference. You might want to check it in an dictionary to get a good idea about it.