NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Write a note in this area. It's really easy to share with others. Click here ...#include <windows.h>
#include <iostream>
#include <string>
#include <fstream>

using std::cin;
using std::cout;
using std::endl;
using std::cerr;
using std::ifstream;
using std::string;

const string INVALID_IN_FILE_NAME = "Не удалось открыть входной файл";
const string INVALID_OUT_FILE = "Не удалось создать выходной файл";

BYTE getIndexInPalette(RGBTRIPLE* color, RGBQUAD* Palette, int& numOfColors);

int main()
{
​try
​{
​​setlocale(LC_ALL, "rus");
​​const WCHAR* inFileName = L"in.bmp";
​​const WCHAR* outFileName = L"out.bmp";
​​BITMAPFILEHEADER bmpFileHeader;
​​BITMAPINFOHEADER bmpInfoHeader;
​​int Width, Height;
​​RGBQUAD Palette[256]; // 4 байта R G B Res
​​HANDLE hInputFile, hOutFile;
​​DWORD RW; // кол-во прочитанных или записанных символов

​​hInputFile = CreateFile(inFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
​​if (hInputFile == INVALID_HANDLE_VALUE)
​​{
​​​throw INVALID_IN_FILE_NAME;
​​}
​​hOutFile = CreateFile(outFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
​​if (hInputFile == INVALID_HANDLE_VALUE)
​​{
​​​CloseHandle(hInputFile);
​​​throw INVALID_OUT_FILE;
​​}


​​// считываем заголовки файла (file и info)
​​ReadFile(hInputFile, &bmpFileHeader, sizeof(bmpFileHeader), &RW, NULL);
​​ReadFile(hInputFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &RW, NULL);

​​// Установим указатель на начало данных о пиксеклях
​​SetFilePointer(hInputFile, bmpFileHeader.bfOffBits, NULL, FILE_BEGIN);

​​// считаем размеры изображения в пикселях
​​Width = bmpInfoHeader.biWidth;
​​Height = bmpInfoHeader.biHeight;

​​// вывод информации о входном файле
​​cout << "Разрешение изображения: ";
​​cout << Width << "x" << Height << endl;
​​cout << "Количество бит на пиксел: " << bmpInfoHeader.biBitCount << endl;
​​cout << "Размер изображения: " << bmpFileHeader.bfSize << " байт" << endl;

​​// Заполним заголовки
​​bmpFileHeader.bfOffBits = sizeof(bmpFileHeader) + sizeof(bmpInfoHeader) + 1024; // 1024 - Palette
​​bmpFileHeader.bfSize = bmpFileHeader.bfOffBits + Width * Height * 1 + Height * Width % 4;
​​bmpInfoHeader.biBitCount = 8;
​​
​​// Запишем заголовки в выходной файл
​​WriteFile(hOutFile, &bmpFileHeader, sizeof(bmpFileHeader), &RW, NULL);
​​WriteFile(hOutFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &RW, NULL);

​​// Выделим память
​​BYTE* outBuf = new BYTE[Width];
​​RGBTRIPLE* buf = new RGBTRIPLE;

​​// Начнем преобразовывать
​​SetFilePointer(hOutFile, bmpFileHeader.bfOffBits, NULL, FILE_BEGIN);
​​int numOfColors = 0;
​​for (int i = 0; i < Height; ++i)
​​{
​​​for (int j = 0; j < Width; ++j)
​​​{
​​​​ReadFile(hInputFile, buf, sizeof(RGBTRIPLE), &RW, NULL);
​​​​outBuf[j] = getIndexInPalette(buf, Palette, numOfColors);
​​​}
​​​WriteFile(hOutFile, outBuf, sizeof(BYTE) * Width, &RW, NULL);
​​​// Пишем мусор для выравнивания
​​​WriteFile(hOutFile, Palette, Width % 4, &RW, NULL);
​ ​SetFilePointer(hInputFile, Width % 4, NULL, FILE_CURRENT);
​​}
​​SetFilePointer(hOutFile, sizeof(bmpFileHeader) + sizeof(bmpInfoHeader), NULL, FILE_BEGIN);
​​WriteFile(hOutFile, Palette, 256 * sizeof(RGBQUAD), &RW, NULL);
​​cout << "Файл успешно преобразован";

​​delete[] outBuf;
​​delete buf;​
CloseHandle(hInputFile);
​​CloseHandle(hOutFile);
​​return 0;
​}
​catch (const string error)
​{
​​cerr << error;
​​return -1;
​}
}

BYTE getIndexInPalette(RGBTRIPLE* color, RGBQUAD* Palette, int& numOfColors)
{
​BYTE result = 0;
​for (int i = 0; i < numOfColors; ++i)
​{
​​if ((Palette[i].rgbRed + 21 >= color->rgbtRed && Palette[i].rgbRed - 21 <= color->rgbtRed) &&
​​​(Palette[i].rgbBlue + 20 >= color->rgbtBlue && Palette[i].rgbBlue - 20 <= color->rgbtBlue) &&
​​​(Palette[i].rgbGreen + 20 >= color->rgbtGreen && Palette[i].rgbGreen - 20 <= color->rgbtGreen))
​​{
​​​result = i;
​​​return result;
​​}
​}
​if (numOfColors < 256)
​{
​​Palette[numOfColors].rgbRed = color->rgbtRed;
​​Palette[numOfColors].rgbGreen = color->rgbtGreen;
​​Palette[numOfColors].rgbBlue = color->rgbtBlue;
​​result = numOfColors;
​​numOfColors++;
​}
​return result;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.