Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdlib.h>
- #include <stdio.h>
- //typedef struct _BITMAPFILEHEADER
- //{
- // WORD bfType; //specifies the file type
- // DWORD bfSize; //specifies the size in bytes of the bitmap file
- // WORD bfReserved1; //reserved; must be 0
- // WORD bfReserved2; //reserved; must be 0
- // DWORD bfOffBits; //species the offset in bytes from the bitmapfileheader to the bitmap bits
- //}BITMAPFILEHEADER, *PBITMAPFILEHEADER;
- //
- //typedef struct _BITMAPINFOHEADER
- //{
- // DWORD biSize; //specifies the number of bytes required by the struct
- // LONG biWidth; //specifies width in pixels
- // LONG biHeight; //species height in pixels
- // WORD biPlanes; //specifies the number of color planes, must be 1
- // WORD biBitCount; //specifies the number of bit per pixel
- // DWORD biCompression;//spcifies the type of compression
- // DWORD biSizeImage; //size of image in bytes
- // LONG biXPelsPerMeter; //number of pixels per meter in x axis
- // LONG biYPelsPerMeter; //number of pixels per meter in y axis
- // DWORD biClrUsed; //number of colors used by th ebitmap
- // DWORD biClrImportant; //number of colors that are important
- //}BITMAPINFOHEADER, *PBITMAPINFOHEADER;
- typedef struct _RGB {
- BYTE Red;
- BYTE Green;
- BYTE Blue;
- } RGB, *PRGB;
- #define FILE_NAME "pr19.bmp"
- #define SIGNATURE 0x4D42
- HANDLE getDataPointer(
- PBYTE copyFileName,
- int *width,
- int *height,
- PRGB *startingPixel
- )
- {
- HANDLE fileHandle = INVALID_HANDLE_VALUE;
- HANDLE fileMappingHandle = INVALID_HANDLE_VALUE;
- PBITMAPFILEHEADER data = NULL;
- PBITMAPINFOHEADER bitmapInfoHeader = NULL;
- DWORD fileSize = 0;
- BOOL copied = CopyFileA(
- FILE_NAME,
- copyFileName,
- FALSE
- );
- if (copied == FALSE)
- {
- printf("Could not copy file %s", copyFileName);
- return INVALID_HANDLE_VALUE;
- }
- fileHandle = CreateFileA(
- copyFileName, //lpFileName
- GENERIC_READ | GENERIC_WRITE, //dwDesiredAccess
- 0, //dwShareMode
- NULL, //lpSecurityAttributes
- OPEN_EXISTING, //dwCreationDisposition
- FILE_ATTRIBUTE_NORMAL, //dwFlagsAndAttributes
- NULL //hTemplateFile
- );
- if (INVALID_HANDLE_VALUE == fileHandle)
- {
- printf("Error: could not open file %d", GetLastError());
- system("pause");
- exit(1);
- }
- fileMappingHandle = CreateFileMappingA(
- fileHandle, //hFile
- NULL, //lpFileMappingAttributes
- PAGE_READWRITE, //flProtect
- 0, //dwMaximumSizeHigh
- 0, //dwMaximumSizeLow
- NULL //lpName
- );
- if (NULL == fileMappingHandle)
- {
- printf("Error: mapping file %d", GetLastError());
- goto cleanup;
- }
- fileSize = GetFileSize(
- fileHandle,
- NULL
- );
- data = (PBITMAPFILEHEADER)MapViewOfFile(
- fileMappingHandle, //hFileMappingObject
- FILE_MAP_WRITE, //dwDesiredAccess
- 0, //dwFileOffsetHigh
- 0, //dwFileOffsetLow
- fileSize //dwNumberOfBytesToMap
- );
- if (NULL == data)
- {
- printf("Error: mapping file %d", GetLastError());
- goto cleanup;
- }
- if (SIGNATURE != data->bfType)
- {
- printf("Error: signature");
- goto cleanup;
- }
- bitmapInfoHeader = (PBITMAPINFOHEADER)((PBYTE)data + sizeof(BITMAPFILEHEADER));
- *width = bitmapInfoHeader->biWidth;
- *height = bitmapInfoHeader->biHeight;
- *startingPixel = (PBYTE)data + data->bfOffBits;
- return fileHandle;
- cleanup:
- if (NULL != data)
- {
- UnmapViewOfFile(data);
- }
- if (NULL != fileMappingHandle)
- {
- CloseHandle(fileMappingHandle);
- }
- return INVALID_HANDLE_VALUE;
- }
- void solveA()
- {
- int width, height;
- PRGB pixel;
- HANDLE h = getDataPointer("pr191.bmp", &width, &height, &pixel);
- int pad = (4 - width * 3 % 4) % 4;
- if (INVALID_HANDLE_VALUE != h)
- {
- for (int j = 0; j < height; ++j)
- {
- for (int i = 0; i < width; ++i)
- {
- pixel->Red = 255 - pixel->Red;
- pixel->Green = 255 - pixel->Green;
- pixel->Blue = 255 - pixel->Blue;
- pixel += 1;
- }
- pixel = (PBYTE)pixel + pad;
- }
- CloseHandle(h);
- }
- }
- void solveB()
- {
- int width, height;
- PRGB pixel;
- HANDLE h = getDataPointer("pr193.bmp", &width, &height, &pixel);
- int pad = (4 - width * 3 % 4) % 4;
- if (INVALID_HANDLE_VALUE != h)
- {
- for (int j = 0; j < height; ++j)
- {
- for (int i = 0; i < width; ++i)
- {
- int av = (pixel->Red + pixel->Green + pixel->Blue) / 3;
- pixel->Red = av;
- pixel->Green = av;
- pixel->Blue = av;
- pixel += 1;
- }
- pixel = (PBYTE)pixel + pad;
- }
- CloseHandle(h);
- }
- }
- void solveC()
- {
- int width, height;
- PRGB pixel;
- PRGB pixelAux;
- RGB aux;
- HANDLE h = getDataPointer("pr192.bmp", &width, &height, &pixel);
- int pad = (4 - width * 3 % 4) % 4;
- pixelAux = pixel + width * (height - 1);
- pixelAux = (PBYTE)pixelAux + (height - 1) * pad;
- if (INVALID_HANDLE_VALUE != h)
- {
- for (int i = 0; i < height / 2; ++i)
- {
- for (int j = 0; j < width; ++j)
- {
- //swap
- //////////////////////////////////
- aux.Red = pixel->Red;
- aux.Blue = pixel->Blue;
- aux.Green = pixel->Green;
- pixel->Red = pixelAux->Red;
- pixel->Green = pixelAux->Green;
- pixel->Blue = pixelAux->Blue;
- pixelAux->Red = aux.Red;
- pixelAux->Green = aux.Green;
- pixelAux->Blue = aux.Blue;
- //////////////////////////////////
- pixel += 1;
- pixelAux += 1;
- }
- pixel = (PBYTE)pixel + pad;
- pixelAux -= width * 2;
- pixelAux = (PBYTE)pixelAux - pad;
- }
- CloseHandle(h);
- }
- }
- int main()
- {
- solveA();
- solveB();
- solveC();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement