Thursday, December 17, 2009

Copying is not a theft !!!

Actually I don't know to what extent is that right :D 





I used to copy many things but with no intention of theft or abuse, it was always for benefit and learning sake.
Lately I keep in wondering is this a bad deed or immoral !!
I keep wondering and ... copying, except there's another way rather than copying to get what I need.


What do you think about this ?!!




Saturday, December 12, 2009

How to detect if bitmap is grayscale ?!

SA :)
Here we're again talking about images, it's all about image processing course :D
well I needed my package to do a specific action depending on whether the image is colored or grayscale.
So, how to achieve this ?!
I searched a lot but I reached nothing :S
so I had to loop the pixels of the image and make sure that the RGB components of each pixel are equal to detect it as grayscale.
How bad !!
But finally I discovered another simple method ;)
consider you have a Bitmap object called Myimage.
the condition of grayscale is( Myimage.Palette.Flags = = 2)
so if this condition is true Myimage is grayscale, else it's colored :D
Yes..That easy ;)
Enjoy it ;) :D

Friday, December 11, 2009

Adjusting controls in Forms - Efficient method ;)

SA :)
Finally there's an efficient way to adjust your controls in the form and to keep their relative positions and sizes.
review this and this :D


this method is very easy with perfect performance ;)


let's see a sample code for this:


    public partial class Form1 : Form
    {
        int oldHeight = 299, oldWidth = 436;
        int newHeight, newWidth;
        float verticalScale, horizontalScale;


        public Form1()
        {
            InitializeComponent();
        }


         private void Form1_Resize(object sender, EventArgs e)
        {


            newHeight = this.Height;
            newWidth = this.Width;
            verticalScale = (float)newHeight / oldHeight;
            horizontalScale = (float)newWidth / oldWidth;


            foreach (Control c in this.Controls)
            {
                c.Height = (int)(c.Height * verticalScale);
                c.Width = (int)(c.Width * horizontalScale);
                c.Top = (int)(c.Top * verticalScale);
                c.Left = (int)(c.Left * horizontalScale);
            }


            oldWidth = this.Width;
            oldHeight = this.Height;
        }
    }


all you have to do is to copy the parameters in your form, and set the oldHeight and oldWidth by the current Height and Width of your form in design mode.


        int oldHeight = 299, oldWidth = 436;
        int newHeight, newWidth;
        float verticalScale, horizontalScale;


and then copy the code in the Form1_Resize() in your form resize handler and enjoy the results ;)

Tuesday, December 1, 2009

Unsafetey is better sometimes :D

SA,


As the Image processing package which we develop in Image processing course gets bigger its performance gets slower, and the need to use unsafe code increases.
So I took the risk :D and started to modify all my functions in that package to convert it from safe to unsafe code, and since I’ve faced some problems and have learned some things about bitmaps I’ve never learned I decided to write them down for those who may face the same problems.


The PDF version is here.


So let’s start.


Safe VS Unsafe:
-Safecode is the code that runs under CLR. The memory management of safe code is handled by CLR.
-Unsafe code is direct memory access or file I/O or database operations. Here you need to manage memory yourself by calling dispose on objects.

For more about this :  http://www.dotnetspider.com/forum/155077-What-Safecode-unsafe-code.aspx


Before getting started:
We need to tell the C# project to allow unsafe code, R.click on the project and choose properties, then:









Also don’t forget to include this library.


using System.Drawing.Imaging;




What does this code look like?!


Let’s take the following function as an example, it’s used to convert image to grayscale.


private Bitmap GrayScale(Bitmap bmpimg)

        {
BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int remain = bmpData.Stride - bmpData.Width * 3;

            unsafe
            {
                byte* ptr = (byte*)bmpData.Scan0;

                for (int i = 0; i < bmpData.Height; i++)
                {
                    for (int j = 0; j < bmpData.Width; j++)
                    {

ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3);
                        ptr += 3;
                    }
                    ptr += remain;
                }
            }
            bmpimg.UnlockBits(bmpData);
            return bmpimg;
        }




To access the image pixels with pointers you’ve to lock its bits first, using the LockBits function



BitmapData bmpData = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);




 This function takes a rectangle as a parameter to determine the rectangle that represents the portion you need to lock in this image, also it takes the ImageLockMode and the PixelFormat and returns an object of BitmapData.




Most of time we use this value “Format24bppRgb” as PixelFormat, which means that each pixel contains 24 bits (3 bytes) and colored by RGB system. RGB color (24-bit) pixel values are stored with bytes as BGR (blue, green, red), byte 1 for Blue, byte 2 for Green and byte 3 for Red. (http://en.wikipedia.org/wiki/BMP_file_format)



A lot of formats are available in this enumerator, see more about them here


In my application I used the “Format24bppRgb” and “Format8bppIndexed” only which I’ll discuss later in this paper.


Now give me your attention in this part,
The basic layout of a locked bitmap array is






In brief let me say that the size of pixels row of an image must be a multiple of 4 and since it’s not the case in all images so padding is usually added at the end of each row.


Ex: an image of format 24bpp with width 17 pixels has a stride of 52 bytes, 51 bytes (3*17) and 1 bit as padding.



After understanding this concept let’s see the rest of the code.



int remain = bmpData.Stride - bmpData.Width * 3;




remain represents the padding magnitude.
Then to start using pointers you have to write your code in an unsafe block.




Unsafe
{……


Now we need a pointer to the first pixel of the image 




byte* ptr = (byte*)bmpData.Scan0;


Where scan0 is a property of BitmapData which gets or sets the address of the first pixel in the image, note that I said pixel not byte, this means if the image is of PixelFormat “Format24bppRgb” then ptr is a pointer to an array of 3 bytes.
Now we need to loop on the image pixels and change their values to grayscale, we use these nested for loops to do so



  for (int i = 0; i < bmpData.Height; i++)
  {
    for (int j = 0; j < bmpData.Width; j++)
     {
ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] + ptr[1] + ptr[2]) / 3);
      ptr += 3;
     }
    ptr += remain;
 }


Note this line



ptr += remain;




Where we add the padding value (remain) to go to the next line and get the next pixel correctly.
And finally




bmpimg.UnlockBits(bmpData);
return bmpimg;
}




We unlock the bits of the image using UnlockBits method which take the BitmapData object as a parameter and by then your image is converted to grayscale.
You can use this example to apply the unsafe code in many other functions of image processing package the concept is the same.


Take care of the pixel format:


 “Image is an array of pixels that carries the color intensities”, yes sometimes, but it’s not always the case.
There’s a format of images called indexed images where the image consists of 2 parts, pixel data and color model data. Each pixel gets its color equivalent by asking the color model for the color corresponding to the pixel’s index.
In Bitmap object the color map is stored in what’s called palette. To get the array of colors in palette, simply:


Color[] myPalette = Pic.Palette.Entries;




Where Entries is the color array in palette.


                                                                              http://en.wikipedia.org/wiki/Indexed_color




So if your image PixelFormat is “Format8bppIndexed”, don’t forget:


1- Lock the image by the same PixelFormat and if you don’t know the format of the input image you can use this




BitmapData orgImgData = Pic.LockBits(new Rectangle(Point.Empty, Pic.Size), ImageLockMode.ReadWrite, Pic.PixelFormat);


Where Pic is the image sent to the function.

2-The padding value will be as follows



int remain = bmpData.Stride - bmpData.Width;


as the pixel is represented by only one byte.


3-  To get the color components of the pixel, construct an array of Color from the Palette.Entries:



Color[] myPalette = Pic.Palette.Entries;


And to get the red component for example:




myPalette[orgImgPtr[0]].R







|--------------------------------------------------------|
  |             Hope this was useful : )               |
|--------------------------------------------------------|




References:
http://www.vcskicks.com/fast-image-processing.html
http://www.devasp.net/net/articles/display/453.html
http://www.bobpowell.net/lockingbits.htm
http://www.codersource.net/csharp_image_Processing.aspx
http://www.cylekx.net/help/indexed_images.htm
http://msdn.microsoft.com/en-us/library/ms229672.aspx


Friday, November 27, 2009

TableLayoutPanel control - is useful ;)

SA,
Do you remember this topic ?! Smart Layout !!!
I've found a good solution it's simple .. I wonder how I couldn't I notice this !!
it's using TableLayoutPanel control in your Form to adjust the position of your controls :D
yes!! that easy :D
I need to say a big Thanks to my friend Manal (she's a fresh graduate and working 
now) who advised me to use this control :)

To know more about this control and how to use it, check this :)
http://en.csharp-online.net/TableLayoutPanel

My first Seminar :)

I'm wondering how did I forget to post something about that significant day !!!!!!!!!!!
maybe just because of midterms that were less than a week from the seminar day.
anyway :D
It was a good day Thanks to Allah, except for the delay :S
we were supposed to start at about 11 am but we started at about 2 PM, How Great !! :D 
but it was good and there wasn't any of annoying questions from the doctors :D


This is our presentation
First seminar presentation

View more presentations from FatmaSamy.

and I was happy that my best friends attended it for my sake :$
I love them :$
May Allah bless them all (L)






From the left : Me, Ahd, Reham (The teamwork :D), Mariomty (My dear :) )
Love u all (K)(L)

Smart Layout !!!

SA,
As a little programmer :D when I try to develop a good looking program I suffer from adjusting controls within the form in a proper way such that they keep their relative positions while resizing. It's really annoying :S :D


I've searched for a program or blugin to help in this, I've found one here


http://www.smart-components.com/


It's cool,easy to use and suits me perfectly :D
but it has a little problem :D:D .. as it's a free copy, when you run your program a message appears which indicates that you haven't purchased yet LOL ..How great !! :D





and so I still have the same problem .. I found this too


http://www.viklele.com/info_formdnet.asp?ref=ggl&ad=fdnet6


but I hasn't discover how to use it yet 
anyway I wish I could find a tool to help it'll be great ;)

Friday, November 13, 2009

Back!!

Here I'm again :D
It has been a while since the last time I have written here .. 
The internet connection was broken :(
But finally and after about a month I've an internet connection now ;)
a wireless one too :D:D:D LOL
about My graduation project there's a lot of news and nowadays I'm preparing the blog and I'll put its link here very soon isA ;)
For now let me tell you that my first seminar will be on next Sunday at 11a.m. isA :D
pray for me plz :)
and about myself ... mmmmm
well :D let me tell you that I'm trying a new method to organize my time 
it's a board divided to 7 parts each represents a day of the week and some sticky notes to assign tasks for each day :D



Hope it'll be useful :D:D
I wish this seminar will be a good and successful one isA
Pray for me :)

Saturday, October 24, 2009

Drawing.Graphics class ;)

Time passes fast, the 4th week in college is starting and a lot of tasks are required .. Image processing, simulation and patterns.
well, while working I face some problems and for sure I turn to google to help me :D
one of the good tutorial I've found is this


http://www.dreamincode.net/forums/showtopic67275.htm

which is about the Drawing.Graphics class
we need this class in many tasks, hope this will be useful :)

Monday, October 5, 2009

First day on CS :)

Today was my first day in this year,my first day in my department - CS -.
Thanks to Allah it was a good day .. I went to college and met my dear friend Fatma and she gave my a gift for my birthday :$:$:$ .. I loved it, really it was amazing one .. it's a pillow with Mickey and Minnie drawn on .. and I really love this couple .. it's one of the most lovely gift's I've ever recieved .. Thanks "Battety" (K)(K)(K).
and this is the photo of the lovely pillow (L)(L)(L)





Then I entered the lectures hall to attend my first lecture in this department .. it was "Image processing" lectured by Dr.Haitham El Mesery .. actually it was a good lecture I loved it .. the introduction was great .. and the examples introduced were funny and encourage us to think and participate in the lecture rather than just keep listening to the lecturer and fall asleep :D:D
About the project ..mmm.. well The CBIR field attracted us specially me :D .. but we hadn't determine the application we'd work on yet .. I wish we can do good job in this field and choose a good, applicable and feasible idea .. I hope so deeply :)
I'm so happy today .. Thanks to Allah .
I hope this year will be a good one isA :):)

Tuesday, September 29, 2009

First day to record :)

Dear blog,
let's record this day (Tuesday 29/9/2009) :)
well it was a good day .. Thanks to ALLAH .. I began this day by going to MODLI to ask about english courses .. the next course will begin on 3/10/2009 .. the first day in the new academic year .. the intensive course takes 1 month and a week :D .. it costs L.E. 350 .. any way I preferd to postpone this after graduation isA .. as this term in my department (computer science) is not easy plus my grad project .. OH !! I think it'll be challenging .. so it'll be tough for me to attend an extra course.
Then I went to my college on foot :D .. MODLI is  near to my college at Ain Shams Univ. I,Ahd and Reham (my teammates) were supposed to meet Dr.Safwat head of scientific computing department to help us find an idea to our Graduation project .. he listed 5 topics for us and we've to choose one and think about an idea in this topic to work an as a GP .. now let's see what are they :

- Trajectory data mining.
- Content based image retrieval.
- DNA info hiding.
- video surveillance.
- HPC solutions for antenna designs.

well the last topic has it's advantages as the idea is ready and we'll get a good support from Dr.Safwat and 2 others .. but we still have to search and to determine if we are really interested in this topic or not .. also we've to search in the other topics we may find a good alternative :)
we're running out of time :(
we've few days to decide which topic and which idea we'll work on :(
may ALLAH be with us .. Pray for us plz :)

whenever we decide a trend I'll write a post for this isA :)


Monday, September 28, 2009

بسـم الله الرحمن الرحيم

I preferred to make my first post's title "In the name of ALLAH", as we always ask the help of ALLAH in every little thing in our lives and we always bless our actions by mentioning his glory name.
Well !! .. why this blog ?! and why this title "On My Way"?!
OK, first I'm a senior in faculty of computer science Ain shams university, Cairo, Egypt.
I'm about to graduate .. but actually when I think about the last 3 years I spent in the college I feel desperate somehow !!! I hadn't achieved much, 3 years and I feel that I'm still very young to get a job and responsibility of work, maybe it's because I didn't tried hard to enrich my abilities and work on myself well, thanks to ALLAH my grade is Very good for the last 3 years but I was just studying the college subjects with no extras with no good awareness of latest technologies.
SO !! .. I actually need to change .. I know I have good abilities, but I'm just in need of hard work .. this blog to help me record my thoughts, progress and to share anything I have learned or will learn isA :) .. and from here the name came :) .. On My Way .. on my way to change, on my way to be a graduate :) .. on my way to me who I've dreamed of  :).. and on my way I'll try to record my steps here :)


I'll put here a list of things I need to achieve and learn to persuade myself to be responsible enough this time in front of myself .
and every time I achieve one I'll strike it out and write a post for this :)  


Just Pray for me :)


Note: I wrote this in English coz I need to improve my language .. and sry for any grammatical or semantical errors :)