Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------------
- #include <windows.h>
- #include <commctrl.h>
- #include <stdio.h>
- #include "jsracka\jpge.h"
- HINSTANCE inst;
- HWND hwnd;
- HFONT font;
- HWND btn1;
- HWND btn2;
- HWND btn3;
- HWND btn4;
- HWND graf;
- HWND status;
- BITMAP bmp;
- HBITMAP hbmp;
- enum
- {
- ID_VYGENEROVAT,
- ID_OPTIMALIZOVAT,
- ID_POSTPROCESS,
- ID_ULOZIT,
- ID_GRAF,
- ID_STATUS
- };
- float teploty[1050] = {0};
- BYTE optimalizovano = 0;
- LRESULT CALLBACK SubProc(HANDLE _hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
- {
- switch(msg)
- {
- case WM_PAINT:
- if(_hwnd == graf)
- {
- RECT r;
- GetClientRect(graf, &r);
- WORD w = r.right;
- WORD h = r.bottom;
- PAINTSTRUCT ps;
- HDC dc = BeginPaint(graf, &ps);
- SelectObject(dc, GetStockObject(DC_BRUSH));
- SelectObject(dc, GetStockObject(DC_PEN));
- SetTextColor(dc, 0);
- SelectObject(dc, font);
- SetDCBrushColor(dc, 0xFFFFFF);
- SetDCPenColor(dc, 0xFFFFFF);
- Rectangle(dc, 0, 0, w, h);
- SetDCPenColor(dc, 0);
- Rectangle(dc, 30, 30, w - 29, h - 29);
- SetDCPenColor(dc, 0x0000FF);
- WORD pocethodnot = optimalizovano >= 3 ? 1050 : 1000;
- float gain = optimalizovano >= 3 ? 50 : 100;
- float off = optimalizovano >= 3 ? 325 : 225;
- for(DWORD i = 0; i < pocethodnot - 1; i ++)
- {
- MoveToEx(dc, i * (w - 60) / pocethodnot + 30, off - teploty[i] * gain, 0);
- LineTo(dc, (i + 1) * (w - 60) / pocethodnot + 30, off - teploty[i + 1] * gain);
- }
- SetDCPenColor(dc, 0);
- for(WORD i = 0; i <= pocethodnot / 50; i ++)
- {
- char txt[5]= {0};
- sprintf(txt, "%i", i * 50 + 1000);
- TextOut(dc, 20 + i * (w - 60) / (pocethodnot / 50), 460, txt, 4);
- MoveToEx(dc, 30 + i * (w - 60) / (pocethodnot / 50) , h - 30, 0);
- LineTo(dc, 30 + i * (w - 60) / (pocethodnot / 50) , h - 35);
- }
- TextOutW(dc, 250, 5, L"Graf vytvořený špičkovými vědci", sizeof("Graf vytvořený špičkovými vědci") - 1);
- RECT r1 = {10, 20, 30, 100};
- DrawText(dc, "T\r\nE\r\nP\r\nL\r\nO\r\nT\r\nA", sizeof("T\r\nE\r\nP\r\nL\r\nO\r\nT\r\nA") - 1, &r1, 0);
- EndPaint(graf, &ps);
- HDC hdc = GetDC(graf);
- BITMAPINFO bmi = {0}; // We set this up to grab what we want
- bmi.bmiHeader.biBitCount = 24; // rgb 8 bytes for each component(3)
- bmi.bmiHeader.biCompression = BI_RGB;// rgb = 3 components
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); // size of this struct
- bmi.bmiHeader.biWidth = r.right - r.left; // width of window
- bmi.bmiHeader.biHeight = r.bottom - r.top; // height of window
- HDC hdc1 = CreateCompatibleDC(hdc);
- BYTE* obrazek = 0;
- hbmp = CreateDIBSection(hdc1, &bmi, DIB_RGB_COLORS, (void**)&obrazek, 0, 0);
- SelectObject(hdc1, hbmp);
- BitBlt(hdc1, 0, 0, 640, 480, hdc, 0, 0, SRCCOPY);
- GetObject(hbmp, sizeof(BITMAP), &bmp);
- ReleaseDC(graf, hdc);
- DeleteDC(hdc1);
- }
- break;
- }
- return DefSubclassProc(_hwnd, msg, wParam, lParam);
- }
- LRESULT CALLBACK WndProc(HANDLE _hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_COMMAND:
- switch(LOWORD(wParam))
- {
- case ID_VYGENEROVAT:
- { DWORD seed = GetTickCount();
- srand(seed);
- for(WORD i = 0; i < 1050; i ++) teploty[i] = (float)(rand() % 32768) / 32768 * 0.8 - 0.4;
- float rnd;
- for(WORD i = 0; i < 1050; i ++)
- {
- if(!(i % 10)) rnd = (float)(rand() % 32768) / 32768;
- teploty[i] += rnd * 0.8 - 0.4;
- }
- for(WORD i = 0; i < 200; i ++) teploty[i] += (float)i / 200;
- for(WORD i = 200; i < 650; i ++) teploty[i] += (float)(650 - i) / 450 * 1.8 - 0.8;
- for(WORD i = 650; i < 1050; i ++) teploty[i] += (float)(i - 650) / 400 * 1 - 0.8;
- EnableWindow(btn1, 0);
- EnableWindow(btn2, 1);
- RedrawWindow(graf, 0, 0, RDW_INVALIDATE);
- } break;
- case ID_OPTIMALIZOVAT:
- switch(optimalizovano)
- {
- case 0:
- for(WORD i = 0; i < 200; i ++) teploty[i] -= (float)i / 200;
- for(WORD i = 200; i < 650; i ++) teploty[i] -= (float)(650 - i) / 450 * 1.8 - 0.8;
- for(WORD i = 650; i < 1050; i ++) teploty[i] -= (float)(i - 650) / 400 * 1 - 0.8;
- SendMessageW(status, WM_SETTEXT, 0, (LPARAM) L"Stav grafu: Optimální");
- break;
- case 1:
- for(WORD i = 900; i < 1050; i ++) teploty[i] += (float)(i - 900) / 150;
- EnableWindow(btn2, 0);
- EnableWindow(btn3, 1);
- SendMessageW(status, WM_SETTEXT, 0, (LPARAM) L"Stav grafu: Optimálnější");
- break;
- }
- optimalizovano ++;
- RedrawWindow(graf, 0, 0, RDW_INVALIDATE);
- break;
- case ID_POSTPROCESS:
- for(WORD i = 1000; i < 1050; i ++) teploty[i] += (float)(i - 1000) / 20 * 1.5;
- optimalizovano ++;
- RedrawWindow(graf, 0, 0, RDW_INVALIDATE);
- EnableWindow(btn3, 0);
- EnableWindow(btn4, 1);
- SendMessageW(status, WM_SETTEXT, 0, (LPARAM) L"Stav grafu: Nejoptimálnější");
- break;
- case ID_ULOZIT:
- { OPENFILENAME ofn;
- char filename[MAX_PATH] = {0};
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = 0;
- ofn.lpstrFilter = "Úžasný formát pro ukládání grafu (*.jpg)\0*.jpg\0";
- ofn.lpstrFile = filename;
- ofn.nMaxFile = MAX_PATH;
- ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
- ofn.lpstrDefExt = "jpg";
- GetSaveFileName(&ofn);
- BYTE *obrazek = (BYTE*)malloc(640*480*3);
- BYTE *_obrazek = obrazek;
- for(WORD y = 0; y < 480; y ++)
- for(WORD x = 0; x < 640; x ++)
- {
- *_obrazek++ = ((BYTE*)bmp.bmBits)[((479 - y) * 640 + x) * 3 + 2];
- *_obrazek++ = ((BYTE*)bmp.bmBits)[((479 - y) * 640 + x) * 3 + 1];
- *_obrazek++ = ((BYTE*)bmp.bmBits)[((479 - y) * 640 + x) * 3];
- }
- jpge::params params;
- params.m_quality = 30;
- params.m_subsampling = jpge::H2V2;
- // params.m_two_pass_flag = optimize_huffman_tables;
- jpge::compress_image_to_jpeg_file(filename, 640, 480, 3, obrazek, params);
- free(obrazek);
- } break;
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProcW(_hwnd, msg, wParam, lParam);
- }
- INT32 WINAPI WinMain(HINSTANCE _inst, HINSTANCE previnst, LPSTR cmd, INT32 cmdshow)
- {
- inst = _inst;
- INITCOMMONCONTROLSEX icex;
- icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
- icex.dwICC = ICC_STANDARD_CLASSES | ICC_BAR_CLASSES;
- InitCommonControlsEx(&icex);
- font = CreateFont(13, 0, 0, 0, FW_DONTCARE, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Tahoma");
- WNDCLASSW wc;
- ZeroMemory(&wc, sizeof(wc));
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.hInstance = inst;
- wc.hIcon = LoadIcon(inst, "RES_ICON");
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
- wc.lpszClassName = L"tridaokna";
- if(!RegisterClassW(&wc)) return 0;
- if(!(hwnd = CreateWindowExW(0, wc.lpszClassName, L"Výroba grafu", WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW, 0, 0, 666, 600, 0, 0, inst, 0))) return 1;
- btn1 = CreateWindowW(L"BUTTON", L"Obstarat \"vědecká\" data", WS_CHILD | WS_VISIBLE, 10, 10, 150, 25, hwnd, (HMENU)ID_VYGENEROVAT, inst, 0);
- btn2 = CreateWindowW(L"BUTTON", L"Optimalizovat graf", WS_CHILD | WS_VISIBLE | WS_DISABLED, 165, 10, 120, 25, hwnd, (HMENU)ID_OPTIMALIZOVAT, inst, 0);
- btn3 = CreateWindowW(L"BUTTON", L"Provést postprocessing", WS_CHILD | WS_VISIBLE | WS_DISABLED, 290, 10, 150, 25, hwnd, (HMENU)ID_POSTPROCESS, inst, 0);
- btn4 = CreateWindowW(L"BUTTON", L"Uložit elaborát", WS_CHILD | WS_VISIBLE | WS_DISABLED, 445, 10, 129, 25, hwnd, (HMENU)ID_ULOZIT, inst, 0);
- graf = CreateWindow("STATIC", 0, WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE, 10, 45, 640, 480, hwnd, (HMENU)ID_GRAF, inst, 0);
- SetWindowSubclass(graf, SubProc, 0, 0);
- status = CreateWindowW(L"msctls_statusbar32", L"Stav grafu: Neoptimální", WS_CHILD | WS_VISIBLE, 445, 10, 129, 25, hwnd, (HMENU)ID_STATUS, inst, 0);
- ShowWindow(hwnd, 1);
- MSG msg;
- while(GetMessage(&msg, 0, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (INT)msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement