NO

Author Topic: ImageEdit  (Read 5802 times)

czerny

  • Guest
ImageEdit
« on: August 11, 2014, 02:35:49 PM »
This is a quick and dirty translation to Pelles C.

You can find the original here.

After loading a picture, the program creates a tempory bmp file. This should be done only once (controlled by a BOOL fLoad). But in the following example this is done multiple times. I have also tried a big file. It was done 2 times than.

Can anybody find the error?

Look at WM_PAINT in winproc.h and SaveBMPFile in functions.h

JohnF

  • Guest
Re: ImageEdit
« Reply #1 on: August 11, 2014, 04:20:56 PM »
This is a quick and dirty translation to Pelles C.

You can find the original here.

After loading a picture, the program creates a tempory bmp file. This should be done only once (controlled by a BOOL fLoad). But in the following example this is done multiple times. I have also tried a big file. It was done 2 times than.

Can anybody find the error?

Look at WM_PAINT in winproc.h and SaveBMPFile in functions.h

Put this in WM_PAINT

   case WM_PAINT:
      {
         char s[90];
         static int cnt = 0;
         cnt++;
         sprintf(s, "%d", cnt);
         SetWindowText(hwnd, s);

         HDC hdc = BeginPaint(hwnd, &ps);

You will notice that there is a count of 1 as the app starts, load an image and the count goes to three, so that's one for the loading of the image and one when the OpenDialog is closed maybe.

That code is a mess. I've cleaned it up a bit by making those header files, C files, and include various other things.

John
« Last Edit: August 11, 2014, 04:50:22 PM by JohnF »

czerny

  • Guest
Re: ImageEdit
« Reply #2 on: August 11, 2014, 05:44:52 PM »
Hi John!

The number of times the WM_PAINT is called must be greater than 1. But the
Code: [Select]
if (fLoad) {
  ...
  fLoad = FALSE;
}
part should only be executed once per file load.

How often are my MessageBoxes called?
« Last Edit: August 11, 2014, 06:02:05 PM by czerny »

JohnF

  • Guest
Re: ImageEdit
« Reply #3 on: August 11, 2014, 05:52:06 PM »
I've found it, the function PaintLoadBitmap() calls SetScrollInfo() twice - if you comment out those two SetScrollInfo calls you only get 1 when the app starts and 1 when the image is loaded.

John

czerny

  • Guest
Re: ImageEdit
« Reply #4 on: August 11, 2014, 06:09:21 PM »
John: Our postings have overlapped. Look at he fLoad flag in WM_PAINT.

JohnF

  • Guest
Re: ImageEdit
« Reply #5 on: August 11, 2014, 07:35:27 PM »
Oh right, well done.

EDIT: if you don't have  fLoad = TRUE the temp bmp is not saved and there can't be any UNDO.

Your MessageBox's are called numerous times because it interferes with WM_PAINT,

If you only use
Code: [Select]
case WM_PAINT:
{
char s[90];
static int cnt = 0;
cnt++;
sprintf(s, "%d", cnt);
SetWindowText(hwnd, s);
then all is ok.
 
Anyway a couple of W_PAINTs is not a problem.

John
« Last Edit: August 11, 2014, 07:44:44 PM by JohnF »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: ImageEdit
« Reply #6 on: August 11, 2014, 08:22:01 PM »
Just took a fast look.
As John said this code isn't a mess just because mess code is much more clean and tidy ...  ;D
First of all to insert long execution code in the screen painting generally is not a very good idea.
Anyway after some reordering to make code at least readable and with a fast debug I found that returning from the save to file function, or the MessageBox if I comment some other function call, the execution resumes at some point back and reexecutes the same code.
I suspect a stack corruption, but it is a real pain to find where up to now ...  :(
« Last Edit: August 11, 2014, 08:23:34 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

JohnF

  • Guest
Re: ImageEdit
« Reply #7 on: August 11, 2014, 09:06:15 PM »
With this cleaned up code and commented out //PaintLoadBitmap in WM_PAINT there is only 2 WM_PAINT's after loading the image.

Which is to be expected.

John

czerny

  • Guest
Re: ImageEdit
« Reply #8 on: August 11, 2014, 09:37:48 PM »
Your MessageBox's are called numerous times because it interferes with WM_PAINT,
Which sort of interference do you mean?

fLoad is a static variable.
There is only one code fragment which sets fLoad to TRUE.

What could it set to TRUE a second (or more) time?

czerny

  • Guest
Re: ImageEdit
« Reply #9 on: August 11, 2014, 10:01:35 PM »
With this cleaned up code and commented out //PaintLoadBitmap in WM_PAINT there is only 2 WM_PAINT's after loading the image.

Which is to be expected.

John
It also seems to be a good idea to use the following code
Code: [Select]
if (fLoad) {
  fLoad = FALSE;
  ...
}

and not

Code: [Select]
if (fLoad) {
  ...
  fLoad = FALSE;
}

you can uncomment PaintLoadBitmap.