|
-
May 20th, 2001, 04:04 PM
#1
Thread Starter
Frenzied Member
Really simple question... regarding arrays
Hi!
I've been programming in VB for about 2 years now, but I only started C++ 3 hours ago with this article I found 
It basically explains how to make a DLL in C++ to handle arrays. But it only allows me to use 1 dimension arrays, and since this is for special effects in images, it needs to be 2d 
Here's the code, in VB. I tried to translate but have no idea of where to start to get the values from the arrays at the positions I want.
Code:
Public Sub SpecialBltArray(SrcArray() As Byte, DestArray() As Byte, ByVal dX As Long, ByVal dY As Long, SrcX As Long, SrcY As Long, SrcWid As Long, SrcHgt As Long, LookupTable() As Byte)
Dim cX As Long, cY As Long 'Loop counters
Dim AbsX As Long, AbsY As Long 'X/Y values in the destination. This is used twice, so I calculate it only once to speed up :)
'Loop trough all pixels...
For cX = 0 To SrcWid
For cY = 0 To SrcHgt
'See the resulting value of crossing the effect's R/G/B value with the
'destination's R/G/B value, in the lookup table.
AbsX = dX + cX
AbsY = dY + cY
DestArray(AbsX, AbsY) = LookupTable(DestArray(AbsX, AbsY), SrcArray(SrcX + cX, SrcY + cY))
Next cY
Next cX
End Sub
That's all. There's no need for array bound checks, because it's all handled in VB, as well as the lookup table. I just need C++ for that little extra speed, VB is kinda slow in this kind of stuff
-
May 20th, 2001, 11:44 PM
#2
Frenzied Member
You can easily use a 1d array almost just the same as if it were 2d. All any array is, is a block of memory split into elements. If you have a 1d array of 30 elements, that's the same amount of memory as a 2d array with dimensions [3][10]. You can translate the x and y coordinates you would use with a 2d array to an element number in a 1d array very easily, so long as you know the width of one line. So to get coord [x][y] you use [x + y*width].
Anyway, your C++ function will look something like this:
Code:
typedef unsigned short BYTE;
void SpecialBltArray(BYTE SrcArray[], BYTE DestArray[], long dX, long dY, long SrcX, long SrcY, long SrcWid, long SrcHgt, BYTE LookupTable[])
{
long cX, cY; // loop counters
long AbsX, AbsY; // X/Y values in the destination. This is usedtwice, so I calculate it only once to speed it up
// loop through all pixels...
for (cX=0; cX<=SrcWid; cX++)
for (cY=0; cY<=SrcHgt; cY++)
{
// see the resulting value of crossing the effect's R/G/B value with the
// destination's R/G/B value, in the lookup table.
AbsX = dX + cX;
AbsY = dY + cY;
DestArray[Absx + AbsY*lDestPitch] = LookupTable[DestArray[BasX + AbsY*lDestPitch] + SrcArray[SrcX+cX + (SrcY+cY)*lSrcPitch]*lLookupPitch];
}
}
The variables lDestPitch, lSrcPitch and lLookupPitch are the widths of one line of their respective arrays. You need to pass them in as parameters.
Also note that there is a lot of scope for optimisation with those array accesses. If any of the widths is constant, you can use some bitshifts to get rid of some multiplications too.
Harry.
"From one thing, know ten thousand things."
-
May 21st, 2001, 03:08 AM
#3
Thread Starter
Frenzied Member
-
May 21st, 2001, 03:33 AM
#4
Frenzied Member
Well the BYTE type was one that I defined. Notice the typedef statement - that's just makeing "BYTE" an alias for "unsigned short" which is the C/C++ native type for a single byte with a value between 0 and 255.
Who's the 'they' you're talking about?
The for loops in the C function will iterate through exactly the same values of cX and cY as the for loops in your VB version. Your for loops start at 0, so do the for loops in the C version. They end at SrcWid and SrcHgt, and so do the loops in the C version. If you used < instead of <= then the last interation would be missed.
Harry.
"From one thing, know ten thousand things."
-
May 21st, 2001, 08:46 AM
#5
Thread Starter
Frenzied Member
Heh, ok thanks, now I understand 
(Sorry but VB World ate my other reply so this one is kinda short)
"They" are actually "he". The guy that wrote the article: http://64.23.12.52
-
May 21st, 2001, 05:43 PM
#6
Thread Starter
Frenzied Member
Sorry, but I tested your code and it doesn't work 
Here it is (with some modifications):
Code:
//#include "Windows.h"
typedef unsigned short BYTE;
//Create the function
void _stdcall SpecialBltArray(BYTE SrcArray[], long TotalSrcWidth, BYTE DestArray[], long TotalDestWidth, long dX, long dY, long SrcX, long SrcY, long SrcWid, long SrcHgt, BYTE LookupTable[])
{
long cX, cY; // loop counters
//long AbsX, AbsY; // X/Y values in the destination. This is used twice, so I calculate it only once to speed it up
long DestPos, SrcPos;
// loop through all pixels...
for (cX=0; cX<=SrcWid; cX++)
for (cY=0; cY<=SrcHgt; cY++)
{
// see the resulting value of crossing the effect's R/G/B value with the
// destination's R/G/B value, in the lookup table.
//AbsX = dX + cX;
//AbsY = dY + cY;
DestPos = (dX + cX) + (dY + cY)*TotalDestWidth;
SrcPos = (SrcX + cX) + (SrcY + cY)*TotalSrcWidth;
//DestArray[AbsX + AbsY*TotalDestWidth] = LookupTable[DestArray[AbsX + AbsY*TotalDestWidth] + SrcArray[SrcX+cX + (SrcY+cY)*TotalSourceWidth]*255];
DestArray[DestPos] = LookupTable[DestArray[DestPos] + SrcArray[SrcPos]*255];
}
}
The definitions are like this:
LIBRARY SpecialEffects
EXPORTS
SpecialBltArray
What's wrong? If you need, I can send you the whole C++ and VB projects...
-
May 22nd, 2001, 03:23 AM
#7
Frenzied Member
I don't really know, I've never made a DLL before... I guess you mean you're getting compile errors and not runtime errors? *Shrug*
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|