Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-05-2009
Member
 
Join Date: Feb 2008
Posts: 624
Break down an image into 8 x 8 blocks

I want to know if anyone know a lib or something in C that allows me to break down an image into bmp with matrices (blocks) of 8 x 8 in order to apply changes to each block.
Reply With Quote
  #2  
Old 04-05-2009
Member
 
Join Date: May 2008
Posts: 945
Re: Break down an image into 8 x 8 blocks

SDL allows to address sub-surface of arbitrary size, eg 8x8. A "surface" SDL is a generalization of the concept of image/bitmap, you can upload one .bmp and do what you want inside.

By against I do not think there is any lib that specializes in 8x8 blocks, although this size corresponds to the macroblocks of JPEG. Your job is not to implement a JPEG compressor?
Reply With Quote
  #3  
Old 04-05-2009
Member
 
Join Date: Feb 2008
Posts: 624
Re: Break down an image into 8 x 8 blocks

Quote:
Your job is not to implement a JPEG compressor?
Yes indeed it is, a JPEG compressor/decompressor. I implemented all the basic steps people can take on a matrix of 8 * 8, namely:
A 8 * 8 block pixels ---> matrix DCT --->Matrix quantified ---> huffman coding ---> huffman decoding ---> Quantification opposite ---> inverse DCT ---> 8 * 8 block pixels

And I want to test the work on images, so it should be broken down into 8 * 8 blocks to implement the steps on each block.

So it's feasible with the SDL?
Reply With Quote
  #4  
Old 04-05-2009
XSI XSI is offline
Member
 
Join Date: May 2008
Posts: 271
Re: Break down an image into 8 x 8 blocks

There are several types of BMP images (I suppose that is the Windows BMP format). There may be black and white, or with a palette of colors, or 4 bytes per pixel, which has become the most common today.

As my day of goodness, here is the code tested and approved permits to load a BMP image in memory and get the color of each pixel and much faster than the API GetPixel () which also works:

Code:
#include <windows.h>
BITMAP bmp1;
HBITMAP hbmp1;
BYTE *DIBits;
HGLOBAL hDIBits;
UINT picture_width, picture_height;
/* ============================================================== */
int init_bitmap(void)
{
   hbmp1 = NULL;
   hDIBits = NULL;
   DIBits = NULL;
   picture_width = 0;
   picture_height = 0;
   return TRUE;
}
/* ============================================================== */
int release_bitmaps(void)
{
   if (hbmp1 != NULL) {
      DeleteObject(hbmp1);
      hbmp1 = NULL;
   }
   if (hDIBits != NULL) {
      GlobalUnlock(hDIBits);
      GlobalFree(hDIBits);
      hDIBits = NULL;
      DIBits = NULL;
   }
   return TRUE;
}
/* ============================================================== 
   Load a BMP file into a DIB bitmap. 
   ============================================================== */
static int loadDIBfromBMPfile(HWND hwnd, char *bmp_filename)
{
   BITMAPINFO bi;
   UINT width, height, components_nb;
   HDC hdc;
   HDC hdcmem; HBITMAP hbmpmem;
   int DIB_x0, DIB_y0, DIB_w;
   HBITMAP h_tmp_bmp;
   BITMAP  tmp_bm;
   if (hwnd == NULL)
      return -99;
   if (hbmp1 != NULL) {
      release_bitmaps();
      init_bitmap();
   }
   DIB_x0 = 0; DIB_y0 = 0; DIB_w = 0;
   memset(&bi, 0, sizeof(BITMAPINFO));
   bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
   bi.bmiHeader.biPlanes = 1;
   bi.bmiHeader.biBitCount = 32; // 32 bit so no padding of the horizontal lines is required 
   bi.bmiHeader.biCompression = BI_RGB;
   bi.bmiHeader.biClrUsed = 0;
   bi.bmiHeader.biClrImportant = 0;
   // Load file 
   h_tmp_bmp = (HBITMAP)LoadImage(NULL, bmp_filename, IMAGE_BITMAP, 0, 0,
               LR_LOADFROMFILE);
//               LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE ); 
   GetObject(h_tmp_bmp, sizeof(BITMAP), &tmp_bm);
   // N.B.: The GetObject function returns only the width, height, and color format information of the bitmap 
   DIB_w = DIB_x0 + tmp_bm.bmWidth;
   height = DIB_y0 + tmp_bm.bmHeight;
   bi.bmiHeader.biWidth = DIB_w;
   bi.bmiHeader.biHeight = -height;
   bi.bmiHeader.biSizeImage = DIB_w * 4 * height * sizeof(BYTE);
   picture_width = DIB_w;
   picture_height = height;
   // Allocate a buffer for the pixels 
   hDIBits = GlobalAlloc(GHND, bi.bmiHeader.biSizeImage);
   if (hDIBits == NULL)
      return(-3);
   DIBits = (BYTE *)GlobalLock(hDIBits);
   memset(DIBits, 255, bi.bmiHeader.biSizeImage); // Initiailize with white pixels 
   // Create a memory DC, with a bitmap 
   hdc = GetDC(hwnd);
   if (!hdc) {
      GlobalUnlock(hDIBits); GlobalFree(hDIBits);
      hDIBits = NULL; DIBits = NULL;
      return -5;
   }
   GetDIBits(hdc, h_tmp_bmp, 0, tmp_bm.bmHeight, DIBits, &bi, DIB_RGB_COLORS);
   hbmp1 = CreateDIBitmap(hdc, &bi.bmiHeader, CBM_INIT,
                          DIBits, &bi, DIB_RGB_COLORS);
   DeleteObject(h_tmp_bmp);
   ReleaseDC(hwnd, hdc);
   if (hbmp1 == NULL) {
      MessageBox(hwnd, "Image too big and/or not enough video ram.",
                 "Error", MB_OK|MB_ICONERROR);
      return FALSE;
   }
   InvalidateRect(hwnd, NULL, TRUE); // Refresh screen 
   return(0);
}
/* ================================================ 
   Get the color of pixel at x, y 
   ================================================ */
int get_pixel_color(int x, int y, int &blue, int &green, int &red)
{
   long int k;
   k = (y * picture_width + x) * 4;
   *blue = DIBits[k];
   *green = DIBits[k + 1];
   *red = DIBits[k + 2];
   // The fourth integer is not used or is the alpha value 
   return TRUE;
}
Reply With Quote
  #5  
Old 04-05-2009
Member
 
Join Date: Feb 2008
Posts: 624
Re: Break down an image into 8 x 8 blocks

In fact bmp images that I created are grayscale. Otherwise I have the following errors in the compilation in C with GCC with CodeBocks in Windows:

Quote:
main.c||In function `init_bitmap':|
main.c|16|error: `picture_x0' undeclared (first use in this function)|
main.c|16|error: (Each undeclared identifier is reported only once|
C:\Documents and Settings\TheShNaYkHs\Bureau\project\tpR\main.c|16|error: for each function it appears in.)|
main.c|17|error: `picture_y0' undeclared (first use in this function)|
main.c||In function `loadDIBfromBMPfile':|
main.c|41|warning: unused variable `width'|
main.c|41|warning: unused variable `components_nb'|
main.c|43|warning: unused variable `hdcmem'|
main.c|43|warning: unused variable `hbmpmem'|
main.c|103|error: syntax error before "pixel_color"|
main.c|103|error: syntax error before '&' token|
main.c|104|warning: return type defaults to `int'|
main.c||In function `pixel_color':|
main.c|106|error: `y' undeclared (first use in this function)|
main.c|106|error: `x' undeclared (first use in this function)|
main.c|107|error: `blue' undeclared (first use in this function)|
main.c|108|error: `green' undeclared (first use in this function)|
|109|error: `red' undeclared (first use in this function)|
|39|warning: 'loadDIBfromBMPfile' defined but not used|
||=== Build finished: 11 errors, 6 warnings ===|
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Break down an image into 8 x 8 blocks"
Thread Thread Starter Forum Replies Last Post
Page Break lines are blocked under Page Break Preview in Microsoft Excel connoisseur MS Office Support 2 17-02-2012 02:09 PM
Difference between Titan Break 1 and Titan Break 2 in Infinity Blade 2 TeesMaar Video Games 5 14-12-2011 06:09 PM
Is it possible to break into wii 4.3 Ryder Allen Portable Devices 5 04-03-2011 10:06 AM
Is it possible to break AES easily aahisTA Networking & Security 3 23-12-2010 07:37 AM
What is Break Even Analysis Logan.B Off Topic Chat 5 19-11-2010 08:45 PM


All times are GMT +5.5. The time now is 09:06 PM.