Pelles C forum

C language => User contributions => Topic started by: Grincheux on March 22, 2021, 07:59:39 PM

Title: Editor for convolutions filters
Post by: Grincheux on March 22, 2021, 07:59:39 PM
I did not know which name to give and I was trying to use Gimp, it is the reason.
The program works with Drag'n Drop only.

The editor can create filters from 3x3 to 19x19.
The resulting image can be saved.
1-File name currently editing : Img 0035.jpg
2-Saved file name (first saved) : Img 0035 - Result.jpg
3-File name when being saved an other time : Img 0035 - Result (0).jpg

Have fun using it.
Title: Re: Editor for convolutions filters
Post by: John Z on March 23, 2021, 10:14:49 AM
Thanks!

I'll play around with it.  Its not going to fill my HD with 1000's of files I hope  :)
A lot of work to do this kind of transformation.
3x3 means 3 pixels in X and 3 pixels in Y ?

John Z
Title: Re: Editor for convolutions filters
Post by: Grincheux on March 23, 2021, 02:15:35 PM
Hello John,

3x3 means 3 rows by 3 colums (3 * 3)

As you know a program never is finished and I have a lot of new ideas.
The biggest difficulty when starting with filters is what to put into each fields.
For example, you create a 19x19 filter.
That means there are 361 fileds.
When filling them the program puts a '1' into the edit fields.
That gives 361 when making the sum.
For having something to see just put -361 int the field 10 on line 10 and click 'Apply'.
Filters are very interesting because we can nice images.
I will store the filters into a database, will add an random option...

That's for the next season... It will not be like 'Dallas'... I hope.
Title: Re: Editor for convolutions filters
Post by: John Z on March 24, 2021, 05:04:21 PM
OK Thanks!

John Z
Title: Re: Editor for convolutions filters
Post by: Grincheux on March 25, 2021, 12:40:38 AM
Added two buttons : Inverse and Random
I have improved the border treatment in creatig a bigger images than the original. In fact I add 25 black pixels all around. This the reason why there is a whit border but all the orignal is modified that was not the case before. I have search how to do for borders, the answer was : "Every one does like he wants, one set black pixel, one compute new colors...'.
Title: Re: Editor for convolutions filters
Post by: John Z on March 25, 2021, 09:44:20 AM
Took a bit to figure out what to select and when but working  :)


John Z

Click on the picture for a better view ….
Title: Re: Editor for convolutions filters
Post by: Grincheux on March 25, 2021, 02:06:44 PM
I know that a minmum of documentation is to do...
It is coming ASAP.
Before I have to implement the histogrammes.
The function to compute them is written, just need a little courage for wrtting the display functions.
After I will write an help file.

When you start the program, it creates a filter of 3 rows and 3 colums.
All is at 0 except the central point which indicates the sum of numbers grether than 0.
If you want to fill all the filter with 0, click CLEAR
If you want to fill all the filter with 1, click SET

The RESET button, redraws original image on the two floatting windows.
RANDOMIZE insert randomized value int the filter, except for the central point.
INVERSE compute THE NOT value for all the fileds.
SAVE saves the current file.

Action menu:
View the result window : Creates or bring to foreground the result window. In the case you closed it.
View the Original Window : Creates or bring to foreground the original window. In the case you closed it.

For example, in a 3x3, if all the fields are equal to 1, the divisor is 9.
Using a divisor may result to add a blur on the new image.
For the instant I think I don't do it well.

The version I am correcting, will compute the divisor and it will be possible to force an other value.
At last, many programs add a "Bias" field to modify the luminosity. It seems to the Alpha channel on a image.
That will be done too.

When loading an image, I want to have an histigramme to see how the colors are place on the image. Is the image too dark? That kind of graph can give many answers.

Once the filter applied, an other histogram is computed to see what changed now.

It seems complicated but I will make a big work to explain that.

Thank You for your comments.

For example try
1 0 -2
0 0 0
1 0 -2

this applies the Sobel filter
Save the image.
Load the new image and apply the following filter :

1 0 1
0 0 0
-2 0 -2

Following is a source code to write filters for Sobel, Kirsch and Robinson.
The filter given as a paramerter must be initialized with somle value.
The function initializes the others filters.

Code: [Select]
//   lpFiltre_1 must already be initialized

int Convolve_GetFilters_Sobel(LPCNV_FILTERS lpFilter_1,LPCNV_FILTERS lpFilter_2)
{
   lpFilter_2->Matrix[0] = lpFilter_1->Matrix[0] ;
   lpFilter_2->Matrix[1] = lpFilter_1->Matrix[3] ;
   lpFilter_2->Matrix[2] = lpFilter_1->Matrix[6] ;
   lpFilter_2->Matrix[3] = lpFilter_1->Matrix[1] ;
   lpFilter_2->Matrix[4] = lpFilter_1->Matrix[4] ;
   lpFilter_2->Matrix[5] = lpFilter_1->Matrix[7] ;
   lpFilter_2->Matrix[6] = lpFilter_1->Matrix[2] ;
   lpFilter_2->Matrix[7] = lpFilter_1->Matrix[5] ;
   lpFilter_2->Matrix[8] = lpFilter_1->Matrix[8] ;

   lstrcpy(lpFilter_1->szName,"# Sobel Horz #") ;
   lstrcpy(lpFilter_2->szName,"# Sobel Vert #") ;

   return (TRUE) ;
}


//   lpFilter_W must already be initialized

int Convolve_GetFilters_Robinson(LPCNV_FILTERS lpFilter_W,LPCNV_FILTERS lpFilter_NO,LPCNV_FILTERS lpFilter_N,LPCNV_FILTERS lpFilter_NE,LPCNV_FILTERS lpFilter_E,LPCNV_FILTERS lpFilter_SE,LPCNV_FILTERS lpFilter_S,LPCNV_FILTERS lpFilter_SO)
{
   lstrcpy(lpFilter_W->szName, "# Robinson West #") ;
   lstrcpy(lpFilter_NO->szName,"# Robinson North West #") ;
   lstrcpy(lpFilter_N->szName, "# Robinson North #") ;
   lstrcpy(lpFilter_NE->szName,"# Robinson North East #") ;
   lstrcpy(lpFilter_E->szName, "# Robinson East #") ;
   lstrcpy(lpFilter_SE->szName,"# Robinson South East #") ;
   lstrcpy(lpFilter_S->szName, "# Robinson South #") ;
   lstrcpy(lpFilter_SO->szName,"# Robinson South West #") ;

   lpFilter_NO->Matrix[0] = lpFilter_W->Matrix[3] ;
   lpFilter_NO->Matrix[1] = lpFilter_W->Matrix[0] ;
   lpFilter_NO->Matrix[2] = lpFilter_W->Matrix[1] ;
   lpFilter_NO->Matrix[3] = lpFilter_W->Matrix[6] ;
   lpFilter_NO->Matrix[4] = lpFilter_W->Matrix[4] ;
   lpFilter_NO->Matrix[5] = lpFilter_W->Matrix[2] ;
   lpFilter_NO->Matrix[6] = lpFilter_W->Matrix[7] ;
   lpFilter_NO->Matrix[7] = lpFilter_W->Matrix[8] ;
   lpFilter_NO->Matrix[8] = lpFilter_W->Matrix[5] ;

   lpFilter_N->Matrix[0] = lpFilter_NO->Matrix[3] ;
   lpFilter_N->Matrix[1] = lpFilter_NO->Matrix[0] ;
   lpFilter_N->Matrix[2] = lpFilter_NO->Matrix[1] ;
   lpFilter_N->Matrix[3] = lpFilter_NO->Matrix[6] ;
   lpFilter_N->Matrix[4] = lpFilter_NO->Matrix[4] ;
   lpFilter_N->Matrix[5] = lpFilter_NO->Matrix[2] ;
   lpFilter_N->Matrix[6] = lpFilter_NO->Matrix[7] ;
   lpFilter_N->Matrix[7] = lpFilter_NO->Matrix[8] ;
   lpFilter_N->Matrix[8] = lpFilter_NO->Matrix[5] ;

   lpFilter_NE->Matrix[0] = lpFilter_N->Matrix[3] ;
   lpFilter_NE->Matrix[1] = lpFilter_N->Matrix[0] ;
   lpFilter_NE->Matrix[2] = lpFilter_N->Matrix[1] ;
   lpFilter_NE->Matrix[3] = lpFilter_N->Matrix[6] ;
   lpFilter_NE->Matrix[4] = lpFilter_N->Matrix[4] ;
   lpFilter_NE->Matrix[5] = lpFilter_N->Matrix[2] ;
   lpFilter_NE->Matrix[6] = lpFilter_N->Matrix[7] ;
   lpFilter_NE->Matrix[7] = lpFilter_N->Matrix[8] ;
   lpFilter_NE->Matrix[8] = lpFilter_N->Matrix[5] ;

   lpFilter_E->Matrix[0] = lpFilter_NE->Matrix[3] ;
   lpFilter_E->Matrix[1] = lpFilter_NE->Matrix[0] ;
   lpFilter_E->Matrix[2] = lpFilter_NE->Matrix[1] ;
   lpFilter_E->Matrix[3] = lpFilter_NE->Matrix[6] ;
   lpFilter_E->Matrix[4] = lpFilter_NE->Matrix[4] ;
   lpFilter_E->Matrix[5] = lpFilter_NE->Matrix[2] ;
   lpFilter_E->Matrix[6] = lpFilter_NE->Matrix[7] ;
   lpFilter_E->Matrix[7] = lpFilter_NE->Matrix[8] ;
   lpFilter_E->Matrix[8] = lpFilter_NE->Matrix[5] ;

   lpFilter_SE->Matrix[0] = lpFilter_E->Matrix[3] ;
   lpFilter_SE->Matrix[1] = lpFilter_E->Matrix[0] ;
   lpFilter_SE->Matrix[2] = lpFilter_E->Matrix[1] ;
   lpFilter_SE->Matrix[3] = lpFilter_E->Matrix[6] ;
   lpFilter_SE->Matrix[4] = lpFilter_E->Matrix[4] ;
   lpFilter_SE->Matrix[5] = lpFilter_E->Matrix[2] ;
   lpFilter_SE->Matrix[6] = lpFilter_E->Matrix[7] ;
   lpFilter_SE->Matrix[7] = lpFilter_E->Matrix[8] ;
   lpFilter_SE->Matrix[8] = lpFilter_E->Matrix[5] ;

   lpFilter_S->Matrix[0] = lpFilter_SE->Matrix[3] ;
   lpFilter_S->Matrix[1] = lpFilter_SE->Matrix[0] ;
   lpFilter_S->Matrix[2] = lpFilter_SE->Matrix[1] ;
   lpFilter_S->Matrix[3] = lpFilter_SE->Matrix[6] ;
   lpFilter_S->Matrix[4] = lpFilter_SE->Matrix[4] ;
   lpFilter_S->Matrix[5] = lpFilter_SE->Matrix[2] ;
   lpFilter_S->Matrix[6] = lpFilter_SE->Matrix[7] ;
   lpFilter_S->Matrix[7] = lpFilter_SE->Matrix[8] ;
   lpFilter_S->Matrix[8] = lpFilter_SE->Matrix[5] ;

   lpFilter_SO->Matrix[0] = lpFilter_S->Matrix[3] ;
   lpFilter_SO->Matrix[1] = lpFilter_S->Matrix[0] ;
   lpFilter_SO->Matrix[2] = lpFilter_S->Matrix[1] ;
   lpFilter_SO->Matrix[3] = lpFilter_S->Matrix[6] ;
   lpFilter_SO->Matrix[4] = lpFilter_S->Matrix[4] ;
   lpFilter_SO->Matrix[5] = lpFilter_S->Matrix[2] ;
   lpFilter_SO->Matrix[6] = lpFilter_S->Matrix[7] ;
   lpFilter_SO->Matrix[7] = lpFilter_S->Matrix[8] ;
   lpFilter_SO->Matrix[8] = lpFilter_S->Matrix[5] ;

   return (TRUE) ;
}

//   lpFilter_W must already be initialized

int Convolve_GetFilters_Kirsch(LPCNV_FILTERS lpFilter_W,LPCNV_FILTERS lpFilter_N,LPCNV_FILTERS lpFilter_E,LPCNV_FILTERS lpFilter_S)
{
   lstrcpy(lpFilter_W->szName,"# Kirsch West #") ;
   lstrcpy(lpFilter_N->szName,"# Kirsch North #") ;
   lstrcpy(lpFilter_E->szName,"# Kirsch East #") ;
   lstrcpy(lpFilter_S->szName,"# Kirsch South #") ;

   lpFilter_N->Matrix[0] = lpFilter_W->Matrix[7] ;
   lpFilter_N->Matrix[1] = lpFilter_W->Matrix[3] ;
   lpFilter_N->Matrix[2] = lpFilter_W->Matrix[0] ;
   lpFilter_N->Matrix[3] = lpFilter_W->Matrix[6] ;
   lpFilter_N->Matrix[4] = lpFilter_W->Matrix[4] ;
   lpFilter_N->Matrix[5] = lpFilter_W->Matrix[1] ;
   lpFilter_N->Matrix[6] = lpFilter_W->Matrix[8] ;
   lpFilter_N->Matrix[7] = lpFilter_W->Matrix[5] ;
   lpFilter_N->Matrix[8] = lpFilter_W->Matrix[2] ;

   lpFilter_E->Matrix[0] = lpFilter_N->Matrix[2] ;
   lpFilter_E->Matrix[1] = lpFilter_N->Matrix[1] ;
   lpFilter_E->Matrix[2] = lpFilter_N->Matrix[0] ;
   lpFilter_E->Matrix[3] = lpFilter_N->Matrix[7] ;
   lpFilter_E->Matrix[4] = lpFilter_N->Matrix[4] ;
   lpFilter_E->Matrix[5] = lpFilter_N->Matrix[1] ;
   lpFilter_E->Matrix[6] = lpFilter_N->Matrix[8] ;
   lpFilter_E->Matrix[7] = lpFilter_N->Matrix[7] ;
   lpFilter_E->Matrix[8] = lpFilter_N->Matrix[2] ;

   lpFilter_S->Matrix[0] = lpFilter_E->Matrix[6] ;
   lpFilter_S->Matrix[1] = lpFilter_E->Matrix[7] ;
   lpFilter_S->Matrix[2] = lpFilter_E->Matrix[8] ;
   lpFilter_S->Matrix[3] = lpFilter_E->Matrix[3] ;
   lpFilter_S->Matrix[4] = lpFilter_E->Matrix[4] ;
   lpFilter_S->Matrix[5] = lpFilter_E->Matrix[5] ;
   lpFilter_S->Matrix[6] = lpFilter_E->Matrix[0] ;
   lpFilter_S->Matrix[7] = lpFilter_E->Matrix[1] ;
   lpFilter_S->Matrix[8] = lpFilter_E->Matrix[2] ;

   return (TRUE) ;
}

Some help :

https://en.wikipedia.org/wiki/Sobel_operator (https://en.wikipedia.org/wiki/Sobel_operator)
https://en.wikipedia.org/wiki/Laplace_operator (https://en.wikipedia.org/wiki/Laplace_operator)
https://en.wikipedia.org/wiki/Prewitt_operator (https://en.wikipedia.org/wiki/Prewitt_operator)
https://en.wikipedia.org/wiki/Roberts_cross (https://en.wikipedia.org/wiki/Roberts_cross)
https://en.wikipedia.org/wiki/Robinson_compass_mask (https://en.wikipedia.org/wiki/Robinson_compass_mask)

You can search Nagoa too.
An example of what you can do

(https://upload.wikimedia.org/wikipedia/commons/9/95/SunLou2.jpg)
and its histogram
(https://upload.wikimedia.org/wikipedia/commons/2/2f/SunHistacp.jpg)

That's all for now.
I'm going to take a nap! I am too old...
Title: Re: Editor for convolutions filters
Post by: Grincheux on March 26, 2021, 08:15:26 AM
Now John, Frankie and the Ghost. A good name for a music group...
I make a pause because I want to test all in depth and would like to speed up the program.
I have an help file to do, it is not easy to do.
I would like to do as wells as John does.
Title: Re: Editor for convolutions filters
Post by: Grincheux on March 28, 2021, 03:41:45 PM
Here is the last update.
Now I am doing the doc and try to profile the program
Title: Re: Editor for convolutions filters
Post by: John Z on April 01, 2021, 10:42:06 AM
Got it.

Thanks!
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 02, 2021, 11:22:22 AM

Here is the last version waiting for doc again.
You can compile/run it but it needs sqlite3:


sqlite3.h
sqlite3.lib
sqlite3.dll
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 02, 2021, 11:28:30 AM
Here is sqlite3
Title: Re: Editor for convolutions filters
Post by: John Z on April 02, 2021, 12:57:51 PM
Have you considered compiling SQLite directly into the sources?  This way an end user does not need to install an additional package to run your program.   I got it (SQLite) to run within a program I had already written using Pelles C.  The only downside I could see was the final project exe was 2x larger in my test case.


John Z
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 02, 2021, 06:48:44 PM
You found why, too big
(https://www.mediafire.com/convkey/acbb/pxc8ms4ghc9boeo4g.jpg) (https://www.mediafire.com/view/pxc8ms4ghc9boeo/me.jpg/file)

Here is the last version with a readme file.
Last SQLite 64 bits version https://www.filehorse.com/download-sqlite-64/download/ (https://www.filehorse.com/download-sqlite-64/download/)
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 03, 2021, 10:12:34 AM
For JohnZ

Quote
The program only accept file thru the Drag'n Drop process.
It accepts many image formats. Not only JPEG.
It uses the "Tahoma" font.

Buttons

Result      Shows the RESULT window. If it is closed it is created.
Original   Shows the ORIGINAL window. If it is closed it is created.
Histogram   Shows the HISTOGRAM window. If it is closed it is created.

Compute Divisor   Just an indication for the sum of the filter.
            Attention the value willbe used the next time you click on a button.
            Click the Clear button to set it to 0.

Clear         Sets all the edit fields to 0. Clears Divisor and Bias.
Set            Sets all the edit fields to 1.
Reset         Sets the content of the Original image into the Result image without applying any filter.
Previous 1      Restore the last random filter
Previous 2      Restore the second random filter

Inverse         Inverse (Not) the edit fields content.
Random         Insert random values into the edit fields.
            Copie "Previous 1" to "Previous 2" and copie the current filter into "Previous 1".
            All this is made before anything.

Negative      Negate the output values, not the entire image.
            During the filter operation the RED, GREEN and BLUE values are set to -Red / -Green / -Blue

Save         Saves the filter into the database and creates the output image file.
            If the filter is a model, it is not saved.

Apply         Apply the filter to the orginal image and store the resulting operation into the Result image.

Combos

Sizes         Allow the user to select between different filter sizes.
            All the sizes are Odd, because there is no middle if the size is even.
            For a 5x5 filter the middle is 2. AAMAA

Models         Different models of filters with different sizes allowing to get a quick result.

Divisor         The value stored into this field is divided by the number of columns * numbers of rows.
            The value got will be multiplied to each component once the filter is applied.

Bias         This value is added to each component once the filter is applied.
            To be exact, the program would have to have three bias fields, one for Red, one for Green and one for blue.

Grey         The check boxes are managed as follow
            Grey Input                        Ok
            Grey Input + Grey Output            Ok
            Grey Input + Grey None               KO   The program unchecks the grey input field.
            Grey Output                        OK
            Grey Output + Grey None               KO   The program unchecks the grey output field.
            Grey Input + Grey Output + Grey None   KO   The program unchecks the grey input and output fields.

            if none of the check boxes are checked, the program check the Grey None.

Random         Numbers are generated between -841...+841 (29x29) limit filter size for the program.

Database organisation

The database is named "User Filters.db3" and is created into the program folder.
It is a sqlite database. The VACUUM command is never executed. Take care when the database grows.
It is possible to store twice the same filter!
The database is created if it does not exists, when the program starts.
It is not important if it exists or not.
If it exists the filters will be append. The index used in that case is the biggest plus 1.
The same index "[RecordNumber]" cannot be inserted twice.
The field "[Name]" is filled by the program, but you can change it if you want. Max 63 characters.
The field "[Grey]" must have the values 0, 1, 2 or 3.
The values are stored into the fields "[V001]" to "[V361]". 19x19 filter.

                           "   [NumberOfColumns]   INTEGER DEFAULT 0 COLLATE BINARY,\n"
                           "   [ColsNumber]      INTEGER DEFAULT 0 COLLATE BINARY,\n"

They must have the same value. For a 9x9 filter, the value 9 is stored in the two fields!

Mathenay, le 2 Avril 2021
Philippe RIO / Grincheux
51966@protonmail.ch
France
-----------------------------------------------------
2021-04-03   Added a primary key composed by a CRC64
         The RecordNumber is just an index.
         When saving filter, if that fails, the image is saved.
         Included SQLite 3.35.3 in 64 bits.
Title: Re: Editor for convolutions filters
Post by: John Z on April 03, 2021, 11:30:28 AM
Très bien.
J'espère que plus que moi en profiterons.

Merci beaucoup mon ami.

John Z
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 04, 2021, 09:35:44 AM
Here is the las version.
Improved speed and corrected bugs.

Nom: myGimp.7z
Taille: 720138 octets (703 KiB)
CRC32: 751FF826
CRC64: EA209118B8E05293
SHA256: 1D08CC8ECD05742366A45614712096D7F84D72A9D9BA37BA9FD2FB09773429E5
SHA1: C6B628A0E4D8D949725D8C35C1C3A00D309AAD0E
BLAKE2sp: 54A86B585E39463362A24EAD4FB9587997D161C5F9AE5653E1BA547BDF1CBEC6
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 04, 2021, 09:39:28 AM
And the missing Sqlite3

Nom: sqlite3.7z
Taille: 678152 octets (662 KiB)
CRC32: 9D4966E7
CRC64: E5C887B0205A29F5
SHA256: 633FD9732A44662C804B1E81F717C4309E945526DC44D0EEC3FE27AD0FF16A66
SHA1: DB5C34039C6DC14080A4DE1454269CED7F907682
BLAKE2sp: 6B73618D93B6898388A3CB1D23B66C21EF0254FD160DFB30961FD8DF8EF517FD
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 04, 2021, 06:44:13 PM
Original
(https://www.mediafire.com/convkey/2776/vlh7fvk3wa6rd8e6g.jpg)


Once the filter is applied:
(https://www.mediafire.com/convkey/7bdc/tyghyedl4ls8gov6g.jpg)

Last Version (https://www.mediafire.com/file/y4f75chow8b9jhp/myGimp_%2528Binaries%2529.7z/file)


(https://www.mediafire.com/convkey/225f/h25kqppgmn6josj6g.jpg)
Quote
Datas :
Name : Flt 0017
Record Number 17
Size : 3x3
Divisor : -50
Bias : 0
Grey : 0
Datas :
   745,  -646,  -279
   371,  -378,   652
   140,  -624,   -32
Title: Re: Editor for convolutions filters
Post by: Stefan Pendl on April 06, 2021, 03:17:33 PM
The resulting image is really scary ;-)
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 06, 2021, 03:22:42 PM
I am testing Median filter, Min and Max filters, Distance and Variance too.
After I have no idea.
I re-read my code to try to improve it.
In a few days I will create a page on google blogger for downloading the program and try to understand how it works.


Thank you for your comment. ;D
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 09, 2021, 05:41:23 PM
Final update ?


(https://www.mediafire.com/convkey/7fb8/h25kqppgmn6josj6g.jpg)

I have added pdf files in French and in english.
Thank You Google Translate

https://www.mediafire.com/file/h7ydmq0u2bbhn0v/Cfe-en.pdf/file (https://www.mediafire.com/file/h7ydmq0u2bbhn0v/Cfe-en.pdf/file)
Title: Re: Editor for convolutions filters
Post by: John Z on April 11, 2021, 04:20:46 PM
 :)
I note the name change....

Thanks, the pdf is helpful.  Should it mention the need for SQLite? 

John Z
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 11, 2021, 04:22:35 PM
SQLite is joint into the package.
Title: Re: Editor for convolutions filters
Post by: John Z on April 11, 2021, 04:40:01 PM
 :) Was just going to say I see the DLL  :)

John Z
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 11, 2021, 07:25:51 PM
I have added a file name Cfe.ini
It contains:
Quote
[Cfe]
Database=0
Inf File=0

When "Database" is equal to 0, that means that no filter record will be added into to file User Filters.db3
When "Inf File" is equal to 0, no image description file will be created.
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 15, 2021, 09:57:42 AM
I do not publish any changes from now within two months.
I made a lot of changes and corrected a mountain of bugs:
Everyday I work with the program during many hours to find what is wrong.
For now it seems clean.

Source code = Binaries

Play with it you will be very surprised if you think you where pretty... I test on me and did not recognized myself.

There is an other domain that I would like to include, it is the CNN seems not too complicated...

Telle me what is wrong or what could be improved, that would help me.
Title: Re: Editor for convolutions filters
Post by: John Z on April 17, 2021, 02:49:34 AM
Ran across this and remembered reading you were interested in optimization as well as assembly code.

Software optimization resources
https://www.agner.org/optimize/

John Z
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 17, 2021, 05:57:44 PM
I know this and read it it many times.

Agner has a good code but don't forget you can't always use it for all the programs.
For example, his function StrCpy is good for large strings, but for small strings, we don't need it.
There are very good ideas because he does not speak about asm only, he introduces optimizations for many languages.
I apply his rules for unrolling loops, when I have a loop like this:

for(i = 0 i < 256 ; i=i+4)
{
    DoSomething(i) ;
    DoSomething(i + 1) ;
    DoSomething(i + 2) ;
    DoSomething(i + 3) ;
}

In my program I need to transform the image from RGB to Grey.
I make the transformation using the average of Red, Green and Blue.

Rather than making Average = (Red + Green = Blue) / 3;
I have a table fo 256 entries nto which I already have computed the average.
I get it doing _r = _g = _b = TabDiv3[_r + _g + _b] ;
If I would repect the standard, it is not for today, I would have three tables, one for Red, one for Green and an other for Blue.
_r = TabStdRed[_r] ;
_g = TabStdGreen[_g] ;
_b = TabStdBlue[_b] ;
Grey = _r + _g + _b ;

I think it is quicker. An other idea is to replace the division by 3 using magic numbers, difficult in C.

I would finish repeating what Mr Pelles told me, having a good algorithm is the best thing.
I think he is TRUE.

But for finishing, Mr Pelle or Frankie must have good ideas on the subject that would be very interesting if they would...

Title: Re: Editor for convolutions filters
Post by: Pelle on April 18, 2021, 02:46:46 PM
To avoid writing a whole book, I just have a few random comments:

1) The compiler will use magic numbers for division and multiplication when it can, but sometimes C language promotion rules can confuse things.

2) Running the profiler on an interactive program like this is only partially helpful, since most of the time is recorded in some Windows function waiting for the next message (so application times are often seen as 0.nn% and easy to miss). What it reveals is pretty obvious: a function like Convolve() takes some time (but still pretty fast on my machine, even for a large image). Doing the traditional "start clock" + "stop clock" in a function like this may be more helpful.

3) I just looked briefly at Convolve(), and not sure how much it matters in practice (without changing code and measuring time), but more variables could be "unsigned" and some conditions inside the loops seems to apply to the whole image, so perhaps use the condition to choose between two different (tighter) loops. This will complicate the code, but if you really want those last percent of performance, it may be worth investigating... (or not).

4) Not sure it applies, but have you considered using SIMD instructions/intrinsics (SSE/AVX/...)? Processing many pixels quickly is one thing they should be good at (in theory). In practice it may be too complicated and not worth it...
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 18, 2021, 02:51:02 PM
Thank You for your comments.
I am pleased.  :D
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 18, 2021, 06:27:59 PM
After the Pelle's comment, I have the following question.

In your post you speak about signed/unsigned variables.

Quote
3) I just looked briefly at Convolve(), and not sure how much it matters in practice (without changing code and measuring time), but more variables could be "unsigned" and some conditions inside the loops seems to apply to the whole image, so perhaps use the condition to choose between two different (tighter) loops. This will complicate the code, but if you really want those last percent of performance, it may be worth investigating... (or not).

I know what that means but in terms of performance what do I win changing signed by unsigned? I don't understand.

The only 128 bits instruction I use is when computing the histogram for setting a table to 0.
Title: Re: Editor for convolutions filters
Post by: Pelle on April 18, 2021, 07:59:40 PM
I know what that means but in terms of performance what do I win changing signed by unsigned? I don't understand.
For example in address calculations, when you use 'int' (32 bits) as an index in a 64-bit (array) address. By default the index needs to be sign-extended to 64 bits (movsxd) so it can be added to the address base. This is normally a little slower than zero-extending, or not extending at all. The compiler will try to minimize the use of movsxd, but depending on the code it may not always be possible. The only reason I mentioned this is because we are talking about images. Even a small picture like 100x100 pixels means 10000 pixels to process, so small things may matter. At least in the past sign-extending vs zero-extending mattered, but modern processors are smarter so maybe less so today. Only way to know fore sure is to measure it.

The only 128 bits instruction I use is when computing the histogram for setting a table to 0.
OK.
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 19, 2021, 09:47:09 AM
I am sad, very sad.
All the Pelle's change have been done and I must say it is finished.
Play with the program and have as much fun as I had writing it and playing with it too.
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 20, 2021, 11:14:46 AM
I have some changes to win more speed.
I have made a test with the profiler and the result is very good.
Last time I made de test I had 9.3% in the "Inclusive" column.
I suppose the program is faster.

I replaced memcpy by __movsq
Made change into loops that use the edit fields
Removed some parameters in some functions
Replaced call to lstrlen with a loop.
I replaced the comparison function in C with one in asembler.

For being a "Pro", I would have to subclass the edit fields or to use the message when a field is left: WM_KILLFOCUS.
In the same style I would have to display a dialog box when an operation is time consuming, a modeless (modless?) box.

I have made a test with cppcheck, but I did not understand. Not very clear.
That is the reason I wrote the "Memory Leaks" module.
(https://www.mediafire.com/convkey/90b5/iwnrz452g42ytrz6g.jpg)
Title: Re: Editor for convolutions filters
Post by: John Z on April 20, 2021, 12:58:58 PM
Living up to your motto!

For long running ops I've used a DialogBox in a thread asking the user to wait but also allowing them to terminate the long running process gracefully if they decide not to wait....

John Z
Title: Re: Editor for convolutions filters
Post by: Grincheux on April 20, 2021, 06:01:51 PM
I didn't get very tired. On this site (https://devblogs.microsoft.com/oldnewthing/20190221-00/?p=101062), I found the work already done by Raymond Chen.
I have added a wait dialog box, in fact the suggest you to have a coffee!

Source code (https://www.mediafire.com/file/ze0sudmcox78wbl/Cfe-Setup-Sources.exe/file)
Binaries (https://www.mediafire.com/file/1jvz4aculnm8kr1/Cfe-Setup.exe/file)