|
Hello,
I am trying to adjust the color balance (CYMK) of an image like this is in photoshop or some other applications.
From examples, i tried changing the current RGB to CMYK (for each pixel) using the below code.
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
tmpbmp->GetPixel(i, j, &clr);
R = clr.GetR();
G = clr.GetG();
B = clr.GetB();
c = _cyanclr / 255.0;
m = _magentaclr / 255.0;
y = _yellowclr / 255.0;
k = _blackclr / 255.0;
rr = c * (1.0 - k) + k;
gg = m * (1.0 - k) + k;
bb = y * (1.0 - k) + k;
rr = (1.0 - rr) * 255.0 + 0.5;
gg = (1.0 - gg) * 255.0 + 0.5;
bb = (1.0 - bb) * 255.0 + 0.5;
R += rr;
G += gg;
B += bb;
R = R < 0 ? 0 : (R > 255) ? 255 : R;
G = G < 0 ? 0 : (G > 255) ? 255 : G;
B = B < 0 ? 0 : (B > 255) ? 255 : B;
output->SetPixel(i, j, Gdiplus::Color(R, G, B));
}
}
With this code, i am not getting the same result like in photoshop and other applications.
I am not sure whether I am doing in right method or not.
Please suggest.
I am using C++ and gdi+.
Regards,
Gopinath.
|
|
|
|
|
It is not clear exactly what your problem is or what adjustments you are trying to make to your colours. Maybe you could explain with some examples.
|
|
|
|
|
Whatever works is the right method. There's no "not". Unless all one does is repost code they dug up from elsewhere and hasn't the foggiest idea what they're doing THAT for.

|
|
|
|
|
I'm looking for a way to catch exceptions due to software or hardware errors and prevent my solution from breaking brutally, but I can't find anything complete.
I have read the two main ways of handling exceptions, C ++ and SEH, but it seems to me that both do not include all cases that can happen.
Can anyone help me ?
|
|
|
|
|
|
Drugodrf wrote: include all cases that can happen. There is no way to handle all cases. For example, what it the power cord is pulled? Or the OS crashes?
Follow best practices and you'll get most all of the likely happenings.
|
|
|
|
|
Greg Utas, thanks for article: i read it as soon as possible.
DevParty: ok, all cases is too much, but most of the cases?
I've tried both ways (exception C++ and SEH), but, for example, they catch division by zero, but not a string copy error.
|
|
|
|
|
Drugodrf wrote: they catch division by zero, but not a string copy error.
What is a "string copy error"?
|
|
|
|
|
In C/C++ you can copy a buffer to another, often a char array but not always, and unintentionally write past the end of the target. Less often but still possible you can write past the beginning as well.
So for example if you have a char[5] and you write 6 bytes to that you have a problem.
The impact of this depends on where the buffer actually lives in the memory space and often what processing has been happening up to that point.
|
|
|
|
|
|
jschell wrote: you write 6 bytes to that you have a problem. Of course, but not an exception. Which is why you are always reminded to use the strxxx_s versions which include bounds checking. But you have to work quite hard to write past the beginning of a buffer.
|
|
|
|
|
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#define MAX_RESPONSE_LENGTH 5
using namespace std;
int main()
{
const float ENGLISH_MIDTERM_PERCENTAGE = .25;
const float ENGLISH_FINALEXAM_PERCENTAGE = .25;
const float ENGLISH_RESEARCH_PERCENTAGE = .30;
const float ENGLISH_PRESENTATION_PERCENTAGE = .20;
const float SCIENCE_MIDTERM_PERCENTAGE = .40;
const float SCIENCE_FINALEXAM_PERCENTAGE = .40;
const float SCIENCE_RESEARCH_PERCENTAGE = .20;
const float MATHS_MIDTERM_PERCENTAGE = .50;
const float MATHS_FINALEXAM_PERCENTAGE = .50;
float finalNumericGrade = 0;
char finalLetteredGrade;
char response [MAX_RESPONSE_LENGTH];
string moreGradesToCalculate;
short midtermGrade = 0;
short finalExamGrade = 0;
short researchGrade = 0;
short presentationGrade = 0;
cout << "Do you have a grade to calculate? \n";
cin >> moreGradesToCalculate;
for(int x = 0; x < moreGradesToCalculate.length();x++){
moreGradesToCalculate[x] = toupper(moreGradesToCalculate[x]);
}
while(moreGradesToCalculate == "YES" ){
cout << "Designate a student type to calculate.\n\n";
cout << "1.English 2.Science\n\n"
<< "3.Maths \n";
cin >> response;
if(strlen(response) == 0){
cout << "You must select a student type.\n" << endl;
return 0;
}
if((atoi(response) < 1) || (atoi(response) > 3)) {
cout << response << " - is not a valid student type.";
return 1;
}
switch (atoi(response))
{
case 1:
cout << "Midterm Grade : ";
cin >> response;
midtermGrade = atoi(response);
cout << "Final Exam Grade : ";
cin >> response;
finalExamGrade = atoi(response);
cout << "Research Grade : ";
cin >> response;
researchGrade = atoi(response);
cout << "Presentation Grade : ";
cin >> response;
presentationGrade = atoi(response);
finalNumericGrade = ((midtermGrade * ENGLISH_MIDTERM_PERCENTAGE) + (finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) +
(researchGrade * ENGLISH_RESEARCH_PERCENTAGE) + (presentationGrade * ENGLISH_PRESENTATION_PERCENTAGE));
cout << endl << "**** ENGLISH STUDENT ****\n" << endl;
cout << "Midterm Grade : " << midtermGrade << endl;
cout << "Final Exam Grade : " << finalExamGrade << endl;
cout << "Research Grade : " << researchGrade << endl;
cout << "Presentation Grade : " << presentationGrade << endl << endl;
if((finalNumericGrade >= 92) && (finalNumericGrade <= 100)) {
finalLetteredGrade = 'A';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n" ;
}
else
if((finalNumericGrade < 92) && (finalNumericGrade >= 85)) {
finalLetteredGrade = 'B';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
}
else
if((finalNumericGrade < 85) && (finalNumericGrade >= 78)) {
finalLetteredGrade = 'C';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
}
else
if((finalNumericGrade < 78) && (finalNumericGrade >= 70)) {
finalLetteredGrade = 'D';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
}
else
if((finalNumericGrade < 70) && (finalNumericGrade >= 0)) {
finalLetteredGrade = 'F';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
}
break;
case 2:
cout << "Midterm Grade :";
cin >> response;
midtermGrade = atoi(response);
cout << "Final Exam Grade :";
cin >> response;
finalExamGrade = atoi(response);
cout << "Research Grade :";
cin >> response;
researchGrade = atoi(response);
finalNumericGrade = ((midtermGrade * SCIENCE_MIDTERM_PERCENTAGE) + (finalExamGrade* SCIENCE_FINALEXAM_PERCENTAGE)
+ (researchGrade * SCIENCE_RESEARCH_PERCENTAGE));
if((finalNumericGrade >= 90) &&(finalNumericGrade <= 100))
finalLetteredGrade = 'A';
else
if((finalNumericGrade < 90) &&(finalNumericGrade >= 80))
finalLetteredGrade = 'B';
else
if((finalNumericGrade < 80) &&(finalNumericGrade >= 70))
finalLetteredGrade = 'c';
else
if((finalNumericGrade < 70) &&(finalNumericGrade >= 60))
finalLetteredGrade = 'D';
else
if((finalNumericGrade < 60) && (finalNumericGrade >= 0))
finalLetteredGrade = 'F';
cout << endl << "**** SCIENCE STUDENT ****\n\n";
cout << "Midterm Grade : " << midtermGrade << endl;
cout << "Final Exam Grade : " << finalExamGrade << endl;
cout << "Research Grade : " << researchGrade << endl << endl;
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
break;
case 3:
cout << "Midterm Grade :";
cin >> response;
midtermGrade = atoi(response);
cout << "Final Exam Grade :";
cin >> response;
finalExamGrade = atoi(response);
finalNumericGrade = (midtermGrade * MATHS_MIDTERM_PERCENTAGE) + (finalExamGrade* MATHS_FINALEXAM_PERCENTAGE);
if((finalNumericGrade >= 90) & (finalNumericGrade <= 100))
finalLetteredGrade = 'A';
else
if((finalNumericGrade >= 83 ) & (finalNumericGrade < 90 ))
finalLetteredGrade = 'B';
else
if((finalNumericGrade >= 76 ) & (finalNumericGrade < 83 ))
finalLetteredGrade = 'C';
else
if((finalNumericGrade >= 65) & (finalNumericGrade < 76))
finalLetteredGrade = 'D';
else
if((finalNumericGrade < 65) & (finalNumericGrade >= 0))
finalLetteredGrade = 'F';
cout << endl << "**** MATHS STUDENT ****\n\n";
cout << "Midterm Grade : " << midtermGrade << endl;
cout << "Final Exam Grade : " << finalExamGrade << endl << endl;
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
break;
default:
cout << "Is not a valid student type:)\n" << endl;
return 1;
}
cout << "Do you have more grades to calculate?\n";
cin >> moreGradesToCalculate;
cout << endl;
for(int x = 0; x < moreGradesToCalculate.length(); x++){
moreGradesToCalculate [x] = toupper(moreGradesToCalculate[x]);
}
}
cout << endl << "Thanks for using my grade calculator.";
return 0;
}
PLEASE HELP ME EXAMINE THE LOGICAL ERROR IN THIS CODE....
1. THE STRLEN() FUNCTION THAT CHECK IF THE USER PRESS THE ENTER KEY WITHOUT PROVING A RESPONSE TO THE QUESTION.
2. THE DEFAULT KEYWORD IN THE SWITCH STATEMENT THAT CHECKS A NON-INTEGER RESPONSE FROM THE USER.
|
|
|
|
|
Compiling does not mean your code is right!
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.
So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.
Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input Expected output Actual output
1 2 1
2 4 4
3 6 9
4 8 16 Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
{
return value * value;
}
Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Member 15281733 wrote: PLEASE HELP ME EXAMINE THE LOGICAL ERROR IN THIS CODE....
1. THE STRLEN() FUNCTION THAT CHECK IF THE USER PRESS THE ENTER KEY WITHOUT PROVING A RESPONSE TO THE QUESTION.
2. THE DEFAULT KEYWORD IN THE SWITCH STATEMENT THAT CHECKS A NON-INTEGER RESPONSE FROM THE USER. You've failed to explain exactly what the problem is. How are we, therefore, to know what your program is supposed to do?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
if((finalNumericGrade >= 83 ) & (finalNumericGrade < 90 ))
If you reach this statement the grade must be less than 90, because you already checked that in the previous statement. So all you need is
if(finalNumericGrade >= 83)
And similar for the following tests.
But putting all your code into main like that makes it so much more difficult to debug. Split the different parts out into functions to make it easier to see where your errors may be.
|
|
|
|
|
Yeah, that might go through a C++ compiler, but it's not really C++. I just didn't want to pile on.
OP - debugger is your friend.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
#include<iostream>
using namespace std;
int main()
{
const int b=14;
const float f=0.20;
cout<<"----------------"<<endl;
cout<<"BOOK LOAN SYSTEM"<<endl;
cout<<"----------------"<<endl;
cout<<"Enter the number of books : ";
int n;
cin>>n;
cout<<"\nEnter the days of the loan : ";
int l;
cin>>l;
cout<<"\n-------------------------------------"<<endl;
cout<<"Days overdue : ";
if(l>b){
cout<<l-b;
cout<<"\nFine : RM "<<(l-b)*(f)*n;
}
}
|
|
|
|
|
Do you mean in this snippet was used some other than C++ language? 
|
|
|
|
|
Did you mean to say "Help me convert this code I found on the internet to C so I can turn it in as my own work?"
|
|
|
|
|
it's already C++.
What are you wanting ? classes ?
I suggest using meaningful variable names instead of b or f or n.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
If you mean to replace std::cout and std::cin with stdio.h functions, then look up the usage of printf() and scanf(), e. g. at C Library - <stdio.h> - Tutorialspoint[^]
If you mean to convert everything to C, you also may need to remove the const qualifiers depending on the exact C compiler (and version) you are using.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application.
Here's the critical portion of my code: (Unnecessary detail has been removed.)
Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun.
It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written."
I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.
m_MemberCount = 7;
CGsUCharType* m_s_b1 = new CGsUCharType(); CGsUCharType* m_s_b2 = new CGsUCharType();
CGsUCharType* m_s_b3 = new CGsUCharType();
CGsUCharType* m_s_b4 = new CGsUCharType();
CGsUShortType* m_s_w1 = new CGsUShortType();
CGsUShortType* m_s_w2 = new CGsUShortType();
CGsULongType* m_S_addr = new CGsULongType();
CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount];
m_Members[0] = m_s_b1;
m_Members[1] = m_s_b2; m_Members[2] = m_s_b3;
m_Members[3] = m_s_b4;
m_Members[4] = m_s_w1;
m_Members[5] = m_s_w2;
m_Members[6] = m_S_addr;
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Your code should work (Intellisense is a little confused here), but may I suggest using std::vector instead:
#include <vector>
...
std::vector<CGsTypeBase*> m_Members(m_MemberCount);
This will ensure that your array is properly constructed and is destructed when it goes out of scope.
You may also wish to look into storing smart pointers (rather than raw pointers) in the vector. This can help ensure that they are destructed at the right time, as well.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Thank you for taking the time to respond. I will consider using vector instead.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
What are the definitions of CGsTypeBase and CGsUCharType ?
[edit]
I built this short program and it compiled and linked successfully:
struct CGsTypeBase
{
char foo[8];
};
struct CGsUCharType : CGsTypeBase
{
wchar_t foo[8];
};
struct CGsUShortType : CGsTypeBase
{
uint16_t foo[8];
};
struct CGsULongType : CGsTypeBase
{
long foo[8];
};
int main(
int argc,
char* argv[]
)
{
int m_MemberCount = 7;
CGsUCharType* m_s_b1 = new CGsUCharType(); CGsUCharType* m_s_b2 = new CGsUCharType();
CGsUCharType* m_s_b3 = new CGsUCharType();
CGsUCharType* m_s_b4 = new CGsUCharType();
CGsUShortType* m_s_w1 = new CGsUShortType();
CGsUShortType* m_s_w2 = new CGsUShortType();
CGsULongType* m_S_addr = new CGsULongType();
CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount];
m_Members[0] = m_s_b1;
m_Members[1] = m_s_b2; m_Members[2] = m_s_b3;
m_Members[3] = m_s_b4;
m_Members[4] = m_s_w1;
m_Members[5] = m_s_w2;
m_Members[6] = m_S_addr;
return 0;
}
So how does that differ from your code?
[/edit]
modified 6-Jul-21 3:54am.
|
|
|
|