Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* W2CIF = w2ch.org image format *
- * Credits: w2ch Anon *
- * File:main.h */
- #ifndef MAIN_H
- #define MAIN_H
- #ifndef WINVER // Allow use of features specific to Windows 7 or later.
- #define WINVER 0x0700 // Change this to the appropriate value to target other versions of Windows.
- #endif
- #ifndef _WIN32_WINNT // Allow use of features specific to Windows 7 or later.
- #define _WIN32_WINNT 0x0700 // Change this to the appropriate value to target other versions of Windows.
- #endif
- //#ifndef UNICODE
- //#define UNICODE
- //#endif
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <cstdio>
- #define BITMAP_ID 0x4d42 // BM
- #endif /* MAIN_H */
- /* W2CIF = world 2 c image format *
- * Credits: w2ch Anon *
- * File:main.cpp */
- #ifndef MAIN_H
- #include "main.h"
- #endif
- void Help(char *szExeName);
- int Printf(HANDLE hFileToWriteTo, char *szFormat, ...);
- int WriteW2CIFHead(HANDLE hFileToWriteTo);
- int WriteW2CIFImageCreation(HANDLE hFileToWriteTo, char *szBitmapName, int nBitmapNumber);
- int WriteW2CIFEnd(HANDLE hFileToWriteTo, int nImageSpeed, int NumberOfImages);
- void Help(char *szExeName)
- {
- fprintf(stdout, "%s FileToCreate -s Bitmap1 Bitmap2 Bitmap3...\n-s TimeInSecondsToChangeCurrentBitmapToNextBitmap\nNOTE:There is no limit on how many bitmaps you can have\n", szExeName);
- }
- int Printf(HANDLE hFileToWriteTo, char *szFormat, ...)
- {
- int nReturnValue = 0;
- DWORD nLenghtOfString = 0,
- nNumberOfCharWriten = 0;
- char szBuffer[1024];
- va_list pArgList;
- va_start(pArgList, szFormat);
- wvsprintf(szBuffer, szFormat, pArgList);
- va_end(pArgList);
- for (; szBuffer[nLenghtOfString] != '\0'; ++nLenghtOfString);
- nReturnValue = WriteFile(hFileToWriteTo,
- szBuffer, nLenghtOfString, &nNumberOfCharWriten, NULL);
- if (nReturnValue) nReturnValue = 0;
- else nReturnValue = GetLastError();
- if (nLenghtOfString != nNumberOfCharWriten) nReturnValue = 1;
- return nReturnValue;
- }
- int WriteW2CIFHead(HANDLE hFileToWriteTo)
- {
- if (Printf(hFileToWriteTo, "/* File:main.h */\r\n\r\n#ifndef MAIN_H\r\n#define MAIN_H\r\n\r\n#ifndef UNICODE\r\n#define UNICODE\r\n#endif\r\n\r\n#define WIN32_LEAN_AND_MEAN\r\n\r\n#include <windows.h>\r\n\r\n#endif /* MAIN_H */\r\n\r\n\r\n")) return 0;
- if (Printf(hFileToWriteTo, "/* File: main.c */\r\n\r\n#ifndef MAIN_H\r\n#include \"main.h\"\r\n#endif\r\n\r\n\r\nLRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);\r\nTCHAR szAppName[] = TEXT(\"W2CHimage\");\r\n\r\n\r\n")) return 0;
- if(Printf(hFileToWriteTo, "int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)\r\n{\r\n\tHWND hwnd;\r\n\tMSG msg;\r\n\tWNDCLASS wndclass;\r\n\t\r\n\twndclass.style = CS_HREDRAW | CS_VREDRAW;\r\n\twndclass.lpfnWndProc = WndProc;\r\n\twndclass.cbClsExtra = 0;\r\n\twndclass.cbWndExtra = 0;\r\n\twndclass.hInstance = hInstance;\r\n\twndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);\r\n\twndclass.hCursor = LoadCursor(NULL, IDC_ARROW);\r\n\twndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);\r\n\twndclass.lpszMenuName = szAppName;\r\n\twndclass.lpszClassName = szAppName;\r\n\t\r\n")) return 0;
- if(Printf(hFileToWriteTo, "\r\n\tif (!RegisterClass(&wndclass))\r\n\t{\r\n\t\tMessageBox(NULL, TEXT(\"This program requires Windows NT!\"),\r\n\t\t\tszAppName, MB_ICONERROR);\r\n\t\treturn 0;\r\n\t}\r\n\t\r\n\thwnd = CreateWindow(szAppName, TEXT(\"DIB Section Display\"),\r\n\t\tWS_OVERLAPPEDWINDOW,\r\n\t\tCW_USEDEFAULT, CW_USEDEFAULT,\r\n\t\tCW_USEDEFAULT, CW_USEDEFAULT,\r\n\t\tNULL, NULL, hInstance, NULL);\r\n\t\r\n\tShowWindow(hwnd, iCmdShow);\r\n\tUpdateWindow(hwnd);\r\n\twhile(GetMessage(&msg, NULL, 0, 0))\r\n\t{\r\n\t\tTranslateMessage(&msg);\r\n\t\tDispatchMessage(&msg);\r\n\t}\r\n\t\r\n\treturn msg.wParam;\r\n}\r\n\r\n\r\n")) return 0;
- return 1;
- }
- int WriteW2CIFImageCreation(HANDLE hFileToWriteTo, char *szBitmapName, int nBitmapNumber)
- {
- HANDLE file_handle; // the file handle to the bitmap to create function to
- int nReturnValue;
- DWORD nNumberOfBytesRead;
- // 0. Open the file.
- if ((file_handle = CreateFile(szBitmapName, GENERIC_READ, FILE_SHARE_READ/* what if 0? */,
- NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
- return 1;
- // 1. Is the file a bitmap?
- BITMAPFILEHEADER bitmapFileHeader;
- nReturnValue = ReadFile(file_handle, &bitmapFileHeader, sizeof(BITMAPFILEHEADER), &nNumberOfBytesRead, NULL);
- if (!nReturnValue || sizeof(BITMAPFILEHEADER) != nNumberOfBytesRead || bitmapFileHeader.bfType != BITMAP_ID)
- {
- CloseHandle(file_handle);
- return 1;
- }
- // 2. Read the bitmap infoheader:
- BITMAPINFOHEADER bih;
- nReturnValue = ReadFile(file_handle, &bih, sizeof(BITMAPINFOHEADER), &nNumberOfBytesRead, NULL);
- if (!nReturnValue || sizeof(BITMAPINFOHEADER) != nNumberOfBytesRead)
- {
- CloseHandle(file_handle);
- return 1;
- }
- // 3. Load the color palette(if there is one)
- int oldBitCount = bih.biBitCount;
- switch (bih.biBitCount)
- {
- default:
- case 0:
- bih.biBitCount = 0;
- break;
- case 1:
- bih.biBitCount = 2;
- break;
- case 4:
- bih.biBitCount = 16;
- break;
- case 8:
- bih.biBitCount = 256;
- break;
- }
- /* (1.5) Print The First Things in the Bitmap Function--- */
- if (Printf(hFileToWriteTo, "HBITMAP CreateBitmap%d(void)\r\n{\r\n\tBITMAPINFO * pbmi;\r\n\tBYTE * pBits;\r\n\tHBITMAP hBitmap;\r\n\t\r\n\t// Allocate memory for the BITMAPINFO structure & read it in\r\n\tpbmi = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)+%d * sizeof(RGBQUAD)];\r\n\t\r\n", nBitmapNumber, bih.biBitCount))
- {
- CloseHandle(file_handle);
- return 1;
- }
- if (Printf(hFileToWriteTo, "\tpbmi->bmiHeader.biSize = %d; // size of this header\r\n\tpbmi->bmiHeader.biWidth = %d; // image width in pixels\r\n\tpbmi->bmiHeader.biHeight = %d; // image height in pixels\r\n\tpbmi->bmiHeader.biPlanes = %d; // number of planes (must be set to 1)\r\n\tpbmi->bmiHeader.biBitCount = %d; // bits per pixel\r\n\tpbmi->bmiHeader.biCompression = ", bih.biSize, bih.biWidth, bih.biHeight, bih.biPlanes, oldBitCount))
- {
- CloseHandle(file_handle);
- return 1;
- }
- switch (bih.biCompression)
- {
- default:
- case BI_RGB:
- if (Printf(hFileToWriteTo, "BI_RGB"))
- {
- CloseHandle(file_handle);
- return 1;
- }
- break;
- case BI_RLE8:
- if (Printf(hFileToWriteTo, "BI_RLE8"))
- {
- CloseHandle(file_handle);
- return 1;
- }
- break;
- case BI_RLE4:
- if (Printf(hFileToWriteTo, "BI_RLE4"))
- {
- CloseHandle(file_handle);
- return 1;
- }
- break;
- case BI_BITFIELDS:
- if (Printf(hFileToWriteTo, "BI_BITFIELDS"))
- {
- CloseHandle(file_handle);
- return 1;
- }
- break;
- case BI_JPEG:
- if (Printf(hFileToWriteTo, "BI_JPEG"))
- {
- CloseHandle(file_handle);
- return 1;
- }
- break;
- case BI_PNG:
- if (Printf(hFileToWriteTo, "BI_PNG"))
- {
- CloseHandle(file_handle);
- return 1;
- }
- break;
- }
- if (Printf(hFileToWriteTo, "; // compression method\r\n\tpbmi->bmiHeader.biSizeImage = %d; // number of bytes in bitmap\r\n\tpbmi->bmiHeader.biXPelsPerMeter = %d; // horizontal resolution\r\n\tpbmi->bmiHeader.biYPelsPerMeter = %d; // vertical resolution\r\n\tpbmi->bmiHeader.biClrUsed = %d; // colors used in the image\r\n\tpbmi->bmiHeader.biClrImportant = %d; // important colors\r\n\r\n", bih.biSizeImage, bih.biXPelsPerMeter, bih.biYPelsPerMeter, bih.biClrUsed, bih.biClrImportant))
- {
- CloseHandle(file_handle);
- return 1;
- }
- /* ------------------------------------------------ */
- if (bih.biBitCount)
- {
- RGBQUAD *pBitmapColorPalette = (RGBQUAD *)new char[bih.biBitCount * sizeof(RGBQUAD)];
- for (int i = 0; i < bih.biBitCount; i++)
- {
- nReturnValue = ReadFile(file_handle, &(pBitmapColorPalette[i]), sizeof(RGBQUAD), &nNumberOfBytesRead, NULL);
- if (!nReturnValue || sizeof(RGBQUAD) != nNumberOfBytesRead)
- {
- delete[] pBitmapColorPalette;
- CloseHandle(file_handle);
- return 1;
- }
- }
- // 3.5 Print the color pallet:
- for (int i = 0; i < bih.biBitCount; i++)
- {
- if (Printf(hFileToWriteTo, "\tpbmi->bmiColors[%d].rgbBlue = %#02x;\r\n\tpbmi->bmiColors[%d].rgbGreen = %#02x;\r\n\tpbmi->bmiColors[%d].rgbRed = %#02x;\r\n", i, (unsigned char)pBitmapColorPalette[i].rgbBlue, i, (unsigned char)pBitmapColorPalette[i].rgbGreen, i, (unsigned char)pBitmapColorPalette[i].rgbRed))
- {
- delete[] pBitmapColorPalette;
- CloseHandle(file_handle);
- return 1;
- }
- }
- if (Printf(hFileToWriteTo, "\r\n\tfor (int n = 0; n < %d; n++) pbmi->bmiColors[n].rgbReserved = 0;\r\n\r\n\t// Create the DIB Section\r\n\thBitmap = CreateDIBSection(NULL, pbmi, ", bih.biBitCount))
- {
- delete[] pBitmapColorPalette;
- CloseHandle(file_handle);
- return 1;
- }
- if (oldBitCount > 8 || !bih.biBitCount)
- {
- if (Printf(hFileToWriteTo, "DIB_PAL_COLORS"))
- {
- delete[] pBitmapColorPalette;
- CloseHandle(file_handle);
- return 1;
- }
- }
- else
- if (Printf(hFileToWriteTo, "DIB_RGB_COLORS"))
- {
- delete[] pBitmapColorPalette;
- CloseHandle(file_handle);
- return 1;
- }
- delete[] pBitmapColorPalette;
- if (Printf(hFileToWriteTo, ", (VOID **)&pBits, NULL, 0);\r\n\t\r\n\tif (hBitmap == NULL)\r\n\t{\r\n\t\tdelete[] pbmi;\r\n\t\treturn NULL;\r\n\t}\r\n\t\r\n\t// Read in the bitmap bits\r\n"))
- {
- CloseHandle(file_handle);
- return 1;
- }
- }
- // 4. Get the bitmap data.
- int nBitmapDataSize = bitmapFileHeader.bfSize - bitmapFileHeader.bfOffBits;
- char *pcBitmapData = new CHAR[nBitmapDataSize];
- for (int i = 0; i < nBitmapDataSize; i++)
- {
- nReturnValue = ReadFile(file_handle, &(pcBitmapData[i]), sizeof(CHAR), &nNumberOfBytesRead, NULL);
- if (!nReturnValue || sizeof(CHAR) != nNumberOfBytesRead)
- {
- delete[] pcBitmapData;
- CloseHandle(file_handle);
- return 1;
- }
- }
- CloseHandle(file_handle); // No need for the handel any more.
- // 4.5 Print the data of the bitmap:
- for (int i = 0; i < nBitmapDataSize; i++)
- {
- if (Printf(hFileToWriteTo, "\tpBits[%d] = %#02x;\r\n", i, (unsigned char)pcBitmapData[i]))
- {
- delete[] pcBitmapData;
- return 1;
- }
- }
- delete[] pcBitmapData;
- if (Printf(hFileToWriteTo, "\t\r\n\t\r\n\tdelete[] pbmi;\r\n\treturn hBitmap;\r\n}\r\n\r\n")) return 1;
- return 0;
- }
- int WriteW2CIFEnd(HANDLE hFileToWriteTo, int nImageSpeed, int NumberOfImages)
- {
- if (Printf(hFileToWriteTo, "\r\nLRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)\r\n{\r\n\tstatic int nImageToDisplay = 0;\r\n\t\r\n\tstatic int nClientWidth = 0;\r\n\tstatic int nWindowHeight = 0;\r\n\t\r\n")) return 1;
- for (int i = 0; i < NumberOfImages; ++i)
- {
- if (Printf(hFileToWriteTo, "\tstatic HBITMAP hBitmap%d = NULL;\r\n", i)) return 1;
- }
- if (Printf(hFileToWriteTo, "\t\r\n\tswitch(message)\r\n\t{\r\n\t\tcase WM_CREATE:\r\n\t\t\tif (!SetTimer (hwnd, 1, %d, NULL))\r\n\t\t\t{\r\n\t\t\t\tMessageBox (hwnd, TEXT(\"SetTimer failed!\"), TEXT(\"ERROR\"), MB_OK);\r\n\t\t\t\treturn -1;\r\n\t\t\t}\r\n\t\t\t\r\n", nImageSpeed)) return 1;
- for (int i = 0; i < NumberOfImages; ++i)
- {
- if (Printf(hFileToWriteTo, "\t\t\tif (!(hBitmap%d = CreateBitmap%d()))\r\n\t\t\t{\r\n\t\t\t\tMessageBox(NULL, TEXT(\"failed CreateBitmap%d!\"), TEXT(\"ERROR\"), MB_OK);\r\n\t\t\t\treturn -1;\r\n\t\t\t}\r\n", i, i, i)) return 1;
- }
- if (Printf(hFileToWriteTo, "\t\t\treturn 0;\r\n\t\t\t\r\n\t\tcase WM_SIZE:\r\n\t\t\tnClientWidth = LOWORD(lParam);\r\n\t\t\tnWindowHeight = HIWORD(lParam);\r\n\t\t\treturn 0;\r\n\t\t\t\r\n\t\tcase WM_TIMER:\r\n\t\t\tnImageToDisplay = (nImageToDisplay < 2) * (nImageToDisplay + 1);\r\n\t\t\tInvalidateRect(hwnd, NULL, TRUE);\t\t\t// Update the client to show next/new image\r\n\t\t\treturn 0;\r\n\t\t\t\r\n", NumberOfImages)) return 1;
- /* !WM_PAINT! */
- if (Printf(hFileToWriteTo, "\t\tcase WM_PAINT:\r\n\t\t\t{\r\n\t\t\t\tHBITMAP hCurrentBitmapToDisplay;\r\n\t\t\t\tHGDIOBJ oldHandelObject;\r\n\t\t\t\tHDC hdc, hdcMem;\r\n\t\t\t\tPAINTSTRUCT ps;\r\n\t\t\t\t\r\n\t\t\t\tswitch(nImageToDisplay)\r\n\t\t\t\t{\r\n\t\t\t\t\tdefault:\r\n")) return 1;
- for (int i = 0; i < NumberOfImages; ++i)
- {
- if (Printf(hFileToWriteTo, "\t\t\t\t\tcase %d:\r\n\t\t\t\t\t\thCurrentBitmapToDisplay = hBitmap%d;\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\t\r\n", i, i)) return 1;
- }
- if (Printf(hFileToWriteTo, "\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\thdc = BeginPaint(hwnd, &ps);\r\n\t\t\t\tif (hCurrentBitmapToDisplay)\r\n\t\t\t\t{\r\n\t\t\t\t\thdcMem = CreateCompatibleDC(hdc);\r\n\t\t\t\t\t/* -------------------------------------- */\r\n\t\t\t\t\tBITMAPINFO bmiTemp;\r\n\t\t\t\t\tZeroMemory(&bmiTemp, sizeof(BITMAPINFO));\r\n\t\t\t\t\tbmiTemp.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);\r\n\t\t\t\t\tif (!GetDIBits(hdc, hCurrentBitmapToDisplay, 0, 0, NULL, &bmiTemp, DIB_RGB_COLORS))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tMessageBox(NULL, TEXT(\"GetDIBits failed!\"), TEXT(\"ERROR\"), MB_OK);\r\n\t\t\t\t\t\tPostMessage(hwnd, WM_DESTROY, 0, 0);\r\n\t\t\t\t\t}\r\n\t\t\t\t\t/* -------------------------------------- */\r\n\t\t\t\t\telse\r\n\t\t\t\t\t{\r\n")) return 1;
- if (Printf(hFileToWriteTo, "\t\t\t\t\tif (NULL == (oldHandelObject = SelectObject(hdcMem, hCurrentBitmapToDisplay)))\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tMessageBox(NULL, TEXT(\"SelectObject failed!\"), TEXT(\"ERROR\"), MB_OK);\r\n\t\t\t\t\t\t\tPostMessage(hwnd, WM_DESTROY, 0, 0);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tStretchBlt(hdc, 0, 0, nClientWidth, nWindowHeight, hdcMem, 0, 0, bmiTemp.bmiHeader.biWidth, bmiTemp.bmiHeader.biHeight, SRCCOPY);\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\tSelectObject(hdcMem, oldHandelObject);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tDeleteDC(hdcMem);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tEndPaint(hwnd, &ps);\r\n\t\t\t}\r\n\t\t\treturn 0;\r\n\t\t\t\r\n")) return 1;
- /* !WM_PAINT! */
- if (Printf(hFileToWriteTo, "\t\tcase WM_DESTROY: \r\n\t\t\tKillTimer(hwnd, 1);\r\n\t\t\t\r\n")) return 1;
- for (int i = 0; i < NumberOfImages; ++i)
- {
- if (Printf(hFileToWriteTo, "\t\t\tif (hBitmap%d) DeleteObject(hBitmap%d);\r\n", i, i)) return 1;
- }
- if (Printf(hFileToWriteTo, "\t\t\t\r\n\t\t\tPostQuitMessage(0);\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\r\n\treturn DefWindowProc(hwnd, message, wParam, lParam);\r\n}\r\n")) return 1;
- return 0;
- }
- int main(int argc, char *argv[])
- {
- if (argc > 1)
- {
- int nNextImageSpeed = 0;
- int nNumberOfImages = 0;
- HANDLE hNewFile;
- //argv[1] "test.txt"
- if ((hNewFile = CreateFile(argv[1], GENERIC_WRITE,
- FILE_SHARE_READ/*0*/, NULL, CREATE_NEW,
- FILE_ATTRIBUTE_NORMAL, NULL)
- ) == INVALID_HANDLE_VALUE)
- {
- if (ERROR_FILE_EXISTS == GetLastError()) MessageBox(NULL, "Error CreateFile, file exist that you want to create! Please delete the file to create a new one", "ERROR", MB_OK);
- }
- else
- {
- if (WriteW2CIFHead(hNewFile))
- {
- int i;
- for (i = 2; i < argc; ++i)
- {
- if (*(argv[i]) == '-')
- if (*((argv[i]) + 1) == 's')
- if (*((argv[i]) + 2) == '\0')
- {
- ++i;
- for (int n = 0; *((argv[i]) + n) != '\0'; nNextImageSpeed = (nNextImageSpeed * 10) | ((*((argv[i]) + n)) & 0xF), ++n);
- continue;
- }
- if (WriteW2CIFImageCreation(hNewFile, argv[i], nNumberOfImages))
- {
- MessageBox(NULL, "WriteW2CIFImageCreation failed!", "ERROR", MB_OK);
- i = argc + 1;
- }
- else ++nNumberOfImages;
- }
- if (i != argc + 1)
- if (WriteW2CIFEnd(hNewFile, nNextImageSpeed, nNumberOfImages)) MessageBox(NULL, "WriteW2CIFEnd failed!", "ERROR", MB_OK);
- }
- CloseHandle(hNewFile);
- }
- }
- else Help(argv[0]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement