File: BmpColorToGray.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 90,112 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 39
file content (94 lines) | stat: -rw-r--r-- 3,080 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.1.1 (2012/07/04)

#include <windows.h>
#include <cassert>
#include <cstdio>
#pragma warning(disable:4996)

//----------------------------------------------------------------------------
void LoadBMP (const char* name, int& width, int& height,
    unsigned char*& data)
{
    HBITMAP hImage = (HBITMAP) LoadImage(NULL, name, IMAGE_BITMAP, 0, 0,
        LR_LOADFROMFILE | LR_CREATEDIBSECTION);
    assert(hImage);

    DIBSECTION dibSection;
    GetObject(hImage, sizeof(DIBSECTION), &dibSection);

    width = dibSection.dsBm.bmWidth;
    height = dibSection.dsBm.bmHeight;
    assert(dibSection.dsBm.bmBitsPixel == 24);
    data = new unsigned char[3*width*height];
    memcpy(data, dibSection.dsBm.bmBits, 3*width*height);

    DeleteObject(hImage);
}
//----------------------------------------------------------------------------
void SaveBMP (const char* name, int width, int height, unsigned char* data)
{
    assert((width % 4) == 0);
    int quantity = width*height;
    int numBytes = 3*quantity;

    BITMAPFILEHEADER fileHeader;
    fileHeader.bfType = 0x4d42;  // "BM"
    fileHeader.bfSize =
        sizeof(BITMAPFILEHEADER) +
        sizeof(BITMAPINFOHEADER) +
        numBytes;
    fileHeader.bfReserved1 = 0;
    fileHeader.bfReserved2 = 0;
    fileHeader.bfOffBits =
        sizeof(BITMAPFILEHEADER) +
        sizeof(BITMAPINFOHEADER);

    BITMAPINFOHEADER infoHeader;
    infoHeader.biSize = sizeof(BITMAPINFOHEADER);
    infoHeader.biWidth = width;
    infoHeader.biHeight = height;
    infoHeader.biPlanes = 1;
    infoHeader.biBitCount = 24;
    infoHeader.biCompression = BI_RGB;
    infoHeader.biSizeImage = 0;
    infoHeader.biXPelsPerMeter = 0;
    infoHeader.biYPelsPerMeter = 0;
    infoHeader.biClrUsed = 0;
    infoHeader.biClrImportant = 0;

    FILE* outFile = fopen(name, "wb");
    fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, outFile);
    fwrite(&infoHeader, sizeof(BITMAPINFOHEADER), 1, outFile);
    fwrite(data, sizeof(unsigned char), numBytes, outFile);
    fclose(outFile);
}
//----------------------------------------------------------------------------
int main (int, char** arguments)
{
    int width, height;
    unsigned char* data;
    LoadBMP(arguments[1], width, height, data);

    unsigned char* color = data;
    for (int i = 0; i < width*height; ++i, color += 3)
    {
        float b = (float)color[0];
        float g = (float)color[1];
        float r = (float)color[2];
        unsigned char gray = (unsigned char)(0.30f*r + 0.59f*g + 0.11f*b);
        color[0] = gray;
        color[1] = gray;
        color[2] = gray;
    }

    SaveBMP(arguments[2], width, height, data);
    delete[] data;
    return 0;
}
//----------------------------------------------------------------------------