|
Both yours and Garth's answers have the array size specified in the declaration, which I always feel is prone to errors in the future. If you add or remove an element or elements and forget to amend the size then your program starts misbehaving. Stick with my solution and let the compiler do the work for you.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
It was more a joke than an answer, I edited it to a real answer.
cheers,
AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
Thanks I just tested this and it did not work. The size is always 1.
Have I implemented it correct?
#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer
char *OPTIONS1_ENUMS[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
char *OPTIONS2_ENUMS[2] = {"OPT_1", "OPT_4"};
char *OPTIONS3_ENUMS[1] = {"OPT_1234"};
char* GetOptions(short enumValue, char** szOptions)
{
int i, nOptions;
short size;
nOptions = 1;
size = ARRAY_SIZE(szOptions);
printf("Array Size = %d\n", size);
}
GetOptions(1, OPTIONS1_ENUMS);
GetOptions(2, OPTIONS2_ENUMS);
GetOptions(3, OPTIONS3_ENUMS);
|
|
|
|
|
Andy202 wrote: Thanks I just tested this and it did not work. The size is always 1.
Have I implemented it correct?
Nope
Use it as follows:
#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer
char *OPTIONS1_ENUMS[] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
char *OPTIONS2_ENUMS[] = {"OPT_1", "OPT_4"};
char *OPTIONS3_ENUMS[] = {"OPT_1234"};
void GetOptions(short enumValue, char** szOptions, int Size)
{
printf("Array Size = %d\n", Size);
}
int main()
{
GetOptions(1, OPTIONS1_ENUMS, ARRAY_SIZE(OPTIONS1_ENUMS));
GetOptions(2, OPTIONS2_ENUMS, ARRAY_SIZE(OPTIONS2_ENUMS));
GetOptions(3, OPTIONS3_ENUMS, ARRAY_SIZE(OPTIONS3_ENUMS));
}
BTW you should not call your arrays OPTIONSx_ENUMS :
- as capitalized names are conventionnally reserved for C macros, and
- they are not enums but C arrays of const char* .
cheers,
AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
Sorry for the late answer (went to bed, was night here), i agree with the others, use NULL or some special string (or a special pointer) to specify the end of the array.
Btw, you can also do this:
char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4", NULL};
this way you don't have to specify the array size, your array's size will be determined by the items you enumerated.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world.
Fry: But this is HDTV. It's got better resolution than the real world <
|
|
|
|
|
I might be really lazy but I'd be tempted to do one of two things:
- Pass another parameter into the function that says how big the array is
- Bundle the pointer into a structure with the number of entries and use that
That way you don't have to worry as much about pointers to pointers and other things that can make you go cross eyed. You're also half way to adding an abstract data type for your options handling as well.
From reading your code I'm not sure you're sure you know what you want to achive. The way you've written is it's "return the first array member that matches one of these strings." You've also not got a return value for when you can't find anything. It's been a while since I've programmed in C but I'd have thought your compiler would have a whinge about that!
Cheers,
Ash
|
|
|
|
|
hello
i am working on a project "Face Detection" in live video without OpenCv, EmguCV etc. so the first thing that I want to access the RGB values of each pixel and then set them back after some plus minus calculation. but the problem is that i can't use GetPixel and SetPixel methods because they are too slow. and i need heigh speed. in one second i have to process many frames. my question is that is there any other way to get the rgb values and set them back more faster than GetPixel and SetPixel, which full fill my requirements?
thaks
|
|
|
|
|
If you use CreateDIBSection[^] to create a DIB, you get a pointer at the pixel data in memory and reading/writing the memory directly is much much much much faster than SetPixel/GetPixel.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world.
Fry: But this is HDTV. It's got better resolution than the real world <
|
|
|
|
|
Just for my curiosity, I really don't want to be offensive: on a previous post (getting pixel color values by GetDiBit function[^]) you said that you are experienced in C# programming, but you have a very limited knowledge on C/C++.
Then, why are you searching for a solution on a C/C++/MFC forum? As I already answered, you can use the GetDIBits() and SetDIBits() from C# using the P-Invoke; is not that more confortable for you?
|
|
|
|
|
actually i got the solution of my problem in c#. and that worked. actually at first i was thinking that there is no way in c# to access pixels with high speed so therefore i thought to work in c++.
therefore i had posted my question here in C++ forum. but now i have the solution in c#.
thanks
|
|
|
|
|
thank you sauro viti i got the solution..
|
|
|
|
|
that's great!
|
|
|
|
|
hi
i want to get the pixel RGB values and then want to change them by simple plus or minus and then set again. i don't want to use getpixel and setpixel because they are too slow. i want to use GetDiBit and SetDiBit functions. but i don't know how to use..please someone help me to write a very simple code that uses GetDiBit and SetDiBit to access a bitmap pixel RGB values and change then Set,including with headerfiles..the code should be as to just copy and paste and run so that i will understand properly.
thanks
|
|
|
|
|
Don't ask for code! The right way starts from here:
|
|
|
|
|
let me tell you sir my actual problem. I am doing project of "Face detection" in live video in c#. and i know c#. but the problem is that i can't use GetPixel and SetPixel methods because they are too slow. then i googled and i came to know about GetDiBit and SetDiBit that they are very fast, but unfortunately i don't understand advance c++, i can just print hello hi etc.then i tried a lot to understand these GetDiBit functions but i don't understand at any way. i searched the code as well on google but didn't get.
so at last i thought that if i get proper code so i can use them and can play with RGB values. i will make DLLs of codes and i will call them in c#.
so this seems to me as solution.
plz help me
|
|
|
|
|
Here[^] you find a basic sample about using GetDIBits .
Pheraps you prefer to write your code using C#; you can starts from these links:
|
|
|
|
|
|
Hello Friends
I am Loading a bitmap using HBITMAP and then attaching it to CBitmap.
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,strFile, IMAGE_BITMAP,0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap bmp;
bmp.Attach(hBmp);
Then I am Creating CompatibleDC using CClientDC.then using Bitblt to show image in tht Window.
CClientDC dcbkImage(pDoc->m_pWndTissu->hbkimg);
CDC bmDC;
bmDC.CreateCompatibleDC(&dcbkImage);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
BITMAP bi;
bmp.GetBitmap(&bi);
dcbkImage.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
bmDC.SelectObject(pOldbmp);
After that I hv another functionality in my application is that I can create stripes vertically or Horizontally on tht window which contains my Image and that coloured stripes are getting draw pixel by pixel.
Now,the prob is tht stripes are getting draw but coming behind the image.
How can I make those stripes above the iamge?
Thanks In Advance.
Regards
Yogesh
|
|
|
|
|
Try this.
CDC *dc=NULL;
dc->Attach()
CDC bmDC;
bmDC.CreateCompatibleDC(dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
BITMAP bi;
bmp.GetBitmap(&bi);
dcbkImage.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
bmDC.SelectObject(pOldbmp);
|
|
|
|
|
GAJERA wrote: CDC *dc=NULL;
dc->Attach()
There are two big errors in this code:
- the
CDC::Attach() method wants an HDC as parameter, then this code doesn't compile. To which HDC do you want to attach? - you have initialized
dc to NULL and then you use that pointer to call one of its non-static members: this will give you a memory protection fault.
|
|
|
|
|
Get HDC from pDoc->m_pWndTissu->hbkimg.
|
|
|
|
|
Thanks Guys For Your reply.
I got the solution but in another way.I changed the last parameter of Bitblt to SRCAND which merges two colors.
Now,the prob is when I draw stripes OnPaint function is calling which hides my background image.So what can i do to keep my image there as background permanently?
Thanks In Advance.
Regards
Yogesh
|
|
|
|
|
OnPaint method always redraw window.
Does you load background image in OnPaint Method?
check with InvalidateRect API.
|
|
|
|
|
Yes,Background iMage is loading on OnPaint.
If I Load once on some other button then also it is coming but as i do some other Functionality like zoom,Draw stripes then tht Image gets hide bcoz on these functionalities OnPaint is calling.
Is there any other solution?
Thanks & Regards
Yogesh
|
|
|
|
|
show me your OnPaint code
|
|
|
|