Advertisement
BigETI

main.cpp - Color Gradient

May 21st, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdint.h>
  3. #include <string>
  4.  
  5. #define TGA_CMP_HEADER_LEN  (12)
  6.  
  7. static char tga_header[TGA_CMP_HEADER_LEN] = { 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
  8.  
  9. int main()
  10. {
  11.     unsigned short i, j;
  12.     wchar_t buf[512];
  13.     std::wstring output_file_name;
  14.     unsigned __int32 out_file_id = 0, progress = 0;
  15.     struct Pixel
  16.     {
  17.         unsigned char blue, green, red;
  18.     } pixel_data;
  19.     unsigned char color_buf;
  20.     FILE *w_file = NULL;
  21.     struct IHDR_chunk
  22.     {
  23.         unsigned short width, height;
  24.         unsigned char bpp, dummy;
  25.     } ihdr_chunk_buffer;
  26.     ihdr_chunk_buffer.width = 0;
  27.     ihdr_chunk_buffer.height = 0;
  28.     ihdr_chunk_buffer.bpp = sizeof(Pixel) * 8;
  29.     ihdr_chunk_buffer.dummy = 0;
  30.     while (!ihdr_chunk_buffer.width)
  31.     {
  32.         std::wcout << L"Please choose a width for the color gradient to generate: ";
  33.         memset(buf, 0, sizeof(wchar_t) * 512);
  34.         std::wcin >> buf;
  35.         ihdr_chunk_buffer.width = _wtoi(buf);
  36.         std::wcout << L"\n\n";
  37.     }
  38.     while (!ihdr_chunk_buffer.height)
  39.     {
  40.         std::wcout << L"Now choose a height for the color gradient to generate: ";
  41.         memset(buf, 0, sizeof(wchar_t) * 512);
  42.         std::wcin >> buf;
  43.         ihdr_chunk_buffer.height = _wtoi(buf);
  44.         std::wcout << L"\n\n";
  45.     }
  46.     std::wcout << L"Generating ";
  47.     while (true)
  48.     {
  49.         output_file_name = L"color_gradient_";
  50.         output_file_name += std::to_wstring(out_file_id);
  51.         output_file_name += L".tga";
  52.         _wfopen_s(&w_file, output_file_name.c_str(), L"rb");
  53.         if (w_file)
  54.         {
  55.             fclose(w_file);
  56.             out_file_id++;
  57.         }
  58.         else
  59.         {
  60.             _wfopen_s(&w_file, output_file_name.c_str(), L"wb");
  61.             break;
  62.         }
  63.     }
  64.     std::wcout << L'\"' << output_file_name << L"\"...";
  65.     fwrite(tga_header, sizeof(char), TGA_CMP_HEADER_LEN, w_file);
  66.     fwrite(&ihdr_chunk_buffer, sizeof(IHDR_chunk), 1, w_file);
  67.     for (i = 0; i != ihdr_chunk_buffer.height; i++)
  68.     {
  69.         for (j = 0; j != ihdr_chunk_buffer.width; j++)
  70.         {
  71.             // Human Eye
  72.             pixel_data.green = ((unsigned char)((~((j * 0xFF) / ihdr_chunk_buffer.width)) & 0xFF));
  73.             color_buf = ((unsigned char)(((i * 0xFF) / ihdr_chunk_buffer.height) & 0xFF));
  74.             pixel_data.green = (pixel_data.green <= color_buf) ? 0x0 : (pixel_data.green - color_buf);
  75.  
  76.             pixel_data.red = ((unsigned char)((~((j * 0xFF) / ihdr_chunk_buffer.width)) & 0xFF));
  77.             color_buf = ((unsigned char)((~((i * 0xFF) / ihdr_chunk_buffer.height)) & 0xFF));
  78.             pixel_data.red = (pixel_data.red <= color_buf) ? 0x0 : (pixel_data.red - color_buf);
  79.  
  80.             pixel_data.blue = ((unsigned char)(((j * 0xFF) / ihdr_chunk_buffer.width) & 0xFF));
  81.             color_buf = ((unsigned char)(((i * 0xFF) / ihdr_chunk_buffer.height) & 0xFF));
  82.             pixel_data.blue = (pixel_data.blue <= color_buf) ? 0x0 : (pixel_data.blue - color_buf);
  83.  
  84.             fwrite(&pixel_data, sizeof(Pixel), 1, w_file);
  85.         }
  86.     }
  87.     fclose(w_file);
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement