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.
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.
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?
Yes indeed it is, a JPEG compressor/decompressor. I implemented all the basic steps people can take on a matrix of 8 * 8, namely:Your job is not to implement a JPEG compressor?
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?
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; }
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:
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 ===|
Bookmarks