Resize a monochrome bitmap without using any class
-
Dear Friends. I want to resize a monochrome bitmap (every pixel can only contains 0 or 1) to another size (like 32*32 to 30*50). I can do it with GDI+ but I want to do it WITHOUT USING ANY CLASS LIKE GDI+. (only loop , for , if , etc.) How can I do it?
Regards. Mehdi Ghiasi
-
Dear Friends. I want to resize a monochrome bitmap (every pixel can only contains 0 or 1) to another size (like 32*32 to 30*50). I can do it with GDI+ but I want to do it WITHOUT USING ANY CLASS LIKE GDI+. (only loop , for , if , etc.) How can I do it?
Regards. Mehdi Ghiasi
You can't run anything under .NET without using some .NET classes, so I'll assume the classes you want to avoid are the graphical ones only. Then this is the recipe:
1. study the specification of your input image format, maybe it is a BMP file;
2. read the input, probably into a byte array;
3. now turn it into some in-memory representation that you feel comfortable with;
4. perform the resize, i.e. create a new bitmap in memory
5. study the specification of your output image format, maybe it is a PNG file;
6. generate your output image by converting it into the output format.You probably could combine some of the steps. You will need a lot of bit manipulations, so make sure you are fluent in masking, shifting, and the like. For the resizing itself, you may choose a nearest-neighbor approach, or alternatively you may want to study different interpolation algorithms, bi-cubic is popular. If you need an extra challenge, you may implement compression, e.g. the one used in the JPEG file format. The alternative to all of the above is less than 10 lines of code, based on the Bitmap class. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
You can't run anything under .NET without using some .NET classes, so I'll assume the classes you want to avoid are the graphical ones only. Then this is the recipe:
1. study the specification of your input image format, maybe it is a BMP file;
2. read the input, probably into a byte array;
3. now turn it into some in-memory representation that you feel comfortable with;
4. perform the resize, i.e. create a new bitmap in memory
5. study the specification of your output image format, maybe it is a PNG file;
6. generate your output image by converting it into the output format.You probably could combine some of the steps. You will need a lot of bit manipulations, so make sure you are fluent in masking, shifting, and the like. For the resizing itself, you may choose a nearest-neighbor approach, or alternatively you may want to study different interpolation algorithms, bi-cubic is popular. If you need an extra challenge, you may implement compression, e.g. the one used in the JPEG file format. The alternative to all of the above is less than 10 lines of code, based on the Bitmap class. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
No! I have a monochrome bitmap in an integer array table and I want to resize it into a new array WITHOUT GDI+ or any Class that Resizes the image For me. I want to know How It Works!
Regards. Mehdi Ghiasi
-
No! I have a monochrome bitmap in an integer array table and I want to resize it into a new array WITHOUT GDI+ or any Class that Resizes the image For me. I want to know How It Works!
Regards. Mehdi Ghiasi
The result probably won't look very good under these conditions; here are two examples (data in binary): 1. an image with a width of 8 pixels, one row containing
00101110
scaled up by a factor of 2, it becomes
0000110011111100
0000110011111100So the result is a "pixelated" image, bits just got replicated, as there is no extra information available. 2. the reverse operation: an image with a width of 16 pixels, two rows containing
0x0x1x0x1x1x1x0x
xxxxxxxxxxxxxxxx(you can put a 0 or 1 at any of the 'x' positions) scaled down by a factor of 2 it could become
00101110
which again does not look anywhere near the same as the original as 75% of the information got thrown away. And that was only the very simple case of a scale by 1/2 or 2/1. As I said before, you will need a lot of bit manipulations, so make sure you are fluent in masking, shifting, and the like. For the resizing itself, you may choose a nearest-neighbor approach. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
The result probably won't look very good under these conditions; here are two examples (data in binary): 1. an image with a width of 8 pixels, one row containing
00101110
scaled up by a factor of 2, it becomes
0000110011111100
0000110011111100So the result is a "pixelated" image, bits just got replicated, as there is no extra information available. 2. the reverse operation: an image with a width of 16 pixels, two rows containing
0x0x1x0x1x1x1x0x
xxxxxxxxxxxxxxxx(you can put a 0 or 1 at any of the 'x' positions) scaled down by a factor of 2 it could become
00101110
which again does not look anywhere near the same as the original as 75% of the information got thrown away. And that was only the very simple case of a scale by 1/2 or 2/1. As I said before, you will need a lot of bit manipulations, so make sure you are fluent in masking, shifting, and the like. For the resizing itself, you may choose a nearest-neighbor approach. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
So how can I scale a 100x100 image into 150x150 ?
Regards. Mehdi Ghiasi
-
So how can I scale a 100x100 image into 150x150 ?
Regards. Mehdi Ghiasi
only double even OR odd bits ...
-
Dear Friends. I want to resize a monochrome bitmap (every pixel can only contains 0 or 1) to another size (like 32*32 to 30*50). I can do it with GDI+ but I want to do it WITHOUT USING ANY CLASS LIKE GDI+. (only loop , for , if , etc.) How can I do it?
Regards. Mehdi Ghiasi