fix a bug that incorrect BMP header processing(#179)

This commit is contained in:
Jinhao 2017-01-15 20:45:14 +08:00
parent ac40ee41ce
commit 880a91b717
2 changed files with 136 additions and 249 deletions

View File

@ -845,7 +845,7 @@ namespace nana { namespace experimental { namespace filesystem
GetFileSizeEx_fptr_t get_file_size_ex = reinterpret_cast<GetFileSizeEx_fptr_t>(::GetProcAddress(::GetModuleHandleA("Kernel32.DLL"), "GetFileSizeEx"));
if (get_file_size_ex)
{
HANDLE handle = ::CreateFile(p.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
HANDLE handle = ::CreateFile(p.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE != handle)
{
LARGE_INTEGER li;

View File

@ -1,7 +1,7 @@
/*
* Bitmap Format Graphics Implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com)
* Copyright(C) 2003-2017 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
@ -30,6 +30,15 @@ namespace nana{ namespace paint
unsigned bfOffBits;
} __attribute__((packed));
struct bitmap_core_header
{
unsigned biSize;
unsigned short biWidth;
unsigned short biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
} __attribute__((packed));
struct bitmap_info_header {
unsigned biSize;
int biWidth;
@ -51,15 +60,10 @@ namespace nana{ namespace paint
unsigned char rgbRed;
unsigned char rgbReserved;
};
struct bitmap_info
{
bitmap_info_header bmiHeader;
rgb_quad bmiColors[1];
}__attribute__((packed));
#else
typedef BITMAPFILEHEADER bitmap_file_header;
typedef BITMAPINFO bitmap_info;
typedef BITMAPFILEHEADER bitmap_file_header;
typedef BITMAPCOREHEADER bitmap_core_header;
typedef BITMAPINFOHEADER bitmap_info_header;
typedef RGBQUAD rgb_quad;
#endif
@ -67,244 +71,42 @@ namespace nana{ namespace paint
:public basic_image_pixbuf
{
public:
image_bmp(){}
~image_bmp()
{
this->close();
}
bool open(const void* data, std::size_t bytes) override
bool open(const void* file_data, std::size_t bytes) override
{
auto bmp_data = reinterpret_cast<const char*>(data);
auto header = reinterpret_cast<const bitmap_file_header*>(bmp_data);
if ((header->bfType != 0x4D42) || (header->bfSize != bytes))
auto bmp_file = reinterpret_cast<const bitmap_file_header*>(file_data);
if ((bmp_file->bfType != 0x4D42) || (bmp_file->bfSize != bytes))
return false;
auto bits = reinterpret_cast<const unsigned char*>(bmp_data + header->bfOffBits);
auto info = reinterpret_cast<const bitmap_info *>(header + 1);
auto const header_bytes = *reinterpret_cast<const unsigned long*>(bmp_file + 1);
//There are two kind of base headers. Determinate it by size of header(The first ulong of header).
//Only Windows Bitmap(BITMAPINFOHEADER) is supported.
if (sizeof(bitmap_core_header) == header_bytes)
{
//The OS/2 BITMAPCOREHEADER is not supported.
throw std::invalid_argument("BMP with OS/2 BITMAPCOREHEADER is not supported now.");
}
auto header = reinterpret_cast<const bitmap_info_header *>(bmp_file + 1);
const std::size_t bmp_height = std::abs(header->biHeight);
//Bitmap file is 4byte-aligned for each line.
std::size_t bytes_per_line;
const std::size_t height_pixels = std::abs(info->bmiHeader.biHeight);
if (0 == info->bmiHeader.biSizeImage)
bytes_per_line = (((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) & ~31) >> 3);
auto bytes_per_line = (((header->biWidth * header->biBitCount + 31) & ~31) >> 3);
pixbuf_.open(header->biWidth, bmp_height);
auto bits = reinterpret_cast<const unsigned char*>(reinterpret_cast<const char*>(file_data) + bmp_file->bfOffBits);
if (16 <= header->biBitCount)
pixbuf_.put(bits, header->biWidth, bmp_height, header->biBitCount, bytes_per_line, (header->biHeight < 0));
else
bytes_per_line = info->bmiHeader.biSizeImage / height_pixels;
pixbuf_.open(info->bmiHeader.biWidth, height_pixels);
auto d = pixbuf_.raw_ptr(0);
if (16 <= info->bmiHeader.biBitCount)
{
pixbuf_.put(bits, info->bmiHeader.biWidth, height_pixels, info->bmiHeader.biBitCount, bytes_per_line, (info->bmiHeader.biHeight < 0));
}
else if (8 == info->bmiHeader.biBitCount)
{
const auto lend = d + info->bmiHeader.biWidth * height_pixels;
if (info->bmiHeader.biHeight < 0)
{
auto s = bits;
while (d < lend)
{
auto d_p = d;
auto dpend = d_p + info->bmiHeader.biWidth;
auto s_p = s;
while (d_p != dpend)
{
auto & rgb = info->bmiColors[*s_p++];
d_p->element.red = rgb.rgbRed;
d_p->element.green = rgb.rgbGreen;
d_p->element.blue = rgb.rgbBlue;
d_p->element.alpha_channel = rgb.rgbReserved;
++d_p;
}
d = dpend;
s += bytes_per_line;
}
}
else
{
const auto* s = bits + bytes_per_line * (height_pixels - 1);
while (d < lend)
{
auto d_p = d;
auto* const dpend = d_p + info->bmiHeader.biWidth;
const auto * s_p = s;
while (d_p != dpend)
{
auto & rgb = info->bmiColors[*s_p++];
d_p->element.red = rgb.rgbRed;
d_p->element.green = rgb.rgbGreen;
d_p->element.blue = rgb.rgbBlue;
d_p->element.alpha_channel = rgb.rgbReserved;
++d_p;
}
d = dpend;
s -= bytes_per_line;
}
}
}
else if (4 == info->bmiHeader.biBitCount)
{
const auto * const lend = d + info->bmiHeader.biWidth * height_pixels;
if (info->bmiHeader.biHeight < 0)
{
const unsigned char* s = bits;
while (d < lend)
{
auto d_p = d;
auto * const dpend = d_p + info->bmiHeader.biWidth;
unsigned index = 0;
while (d_p != dpend)
{
auto & rgb = info->bmiColors[(index & 1) ? (s[index >> 1] & 0xF) : (s[index >> 1] & 0xF0) >> 4];
d_p->element.red = rgb.rgbRed;
d_p->element.green = rgb.rgbGreen;
d_p->element.blue = rgb.rgbBlue;
d_p->element.alpha_channel = rgb.rgbReserved;
++d_p;
++index;
}
d = dpend;
s += bytes_per_line;
}
}
else
{
const auto* s = bits + bytes_per_line * (height_pixels - 1);
while (d < lend)
{
auto d_p = d;
auto * const dpend = d_p + info->bmiHeader.biWidth;
unsigned index = 0;
while (d_p != dpend)
{
auto & rgb = info->bmiColors[(index & 1) ? (s[index >> 1] & 0xF) : (s[index >> 1] & 0xF0) >> 4];
d_p->element.red = rgb.rgbRed;
d_p->element.green = rgb.rgbGreen;
d_p->element.blue = rgb.rgbBlue;
d_p->element.alpha_channel = rgb.rgbReserved;
++d_p;
++index;
}
d = dpend;
s -= bytes_per_line;
}
}
}
else if (2 == info->bmiHeader.biBitCount)
{
const auto * const lend = d + info->bmiHeader.biWidth * height_pixels;
if (info->bmiHeader.biHeight < 0)
{
const unsigned char* s = bits;
while (d < lend)
{
auto d_p = d;
auto * const dpend = d_p + info->bmiHeader.biWidth;
unsigned index = 0;
while (d_p != dpend)
{
unsigned shift = (3 - (index & 0x3)) << 1; // (index % 4) * 2
auto& rgb = info->bmiColors[(s[index >> 2] & (0x3 << shift)) >> shift];
d_p->element.red = rgb.rgbRed;
d_p->element.green = rgb.rgbGreen;
d_p->element.blue = rgb.rgbBlue;
d_p->element.alpha_channel = rgb.rgbReserved;
++d_p;
++index;
}
d = dpend;
s += bytes_per_line;
}
}
else
{
const auto* s = bits + bytes_per_line * (height_pixels - 1);
while (d < lend)
{
auto d_p = d;
auto * const dpend = d_p + info->bmiHeader.biWidth;
unsigned index = 0;
while (d_p != dpend)
{
unsigned shift = (3 - (index & 0x3)) << 1; // (index % 4) * 2
auto& rgb = info->bmiColors[(s[index >> 2] & (0x3 << shift)) >> shift];
d_p->element.red = rgb.rgbRed;
d_p->element.green = rgb.rgbGreen;
d_p->element.blue = rgb.rgbBlue;
d_p->element.alpha_channel = rgb.rgbReserved;
++d_p;
++index;
}
d = dpend;
s -= bytes_per_line;
}
}
}
else if (1 == info->bmiHeader.biBitCount)
{
const auto * const lend = d + info->bmiHeader.biWidth * height_pixels;
if (info->bmiHeader.biHeight < 0)
{
const auto* s = bits;
while (d < lend)
{
auto d_p = d;
auto * const dpend = d_p + info->bmiHeader.biWidth;
unsigned index = 0;
while (d_p != dpend)
{
unsigned bi = (7 - (index & 7)); //(index % 8)
auto & rgb = info->bmiColors[(s[index >> 3] & (1 << bi)) >> bi];
d_p->element.red = rgb.rgbRed;
d_p->element.green = rgb.rgbGreen;
d_p->element.blue = rgb.rgbBlue;
d_p->element.alpha_channel = rgb.rgbReserved;
++d_p;
++index;
}
d = dpend;
s += bytes_per_line;
}
}
else
{
const auto* s = bits + bytes_per_line * (height_pixels - 1);
while (d < lend)
{
auto d_p = d;
auto * const dpend = d_p + info->bmiHeader.biWidth;
unsigned index = 0;
while (d_p != dpend)
{
unsigned bi = (7 - (index & 7));
auto & rgb = info->bmiColors[(s[index >> 3] & (1 << bi)) >> bi];
d_p->element.red = rgb.rgbRed;
d_p->element.green = rgb.rgbGreen;
d_p->element.blue = rgb.rgbBlue;
d_p->element.alpha_channel = rgb.rgbReserved;
++d_p;
++index;
}
d = dpend;
s -= bytes_per_line;
}
}
}
_m_put_with_palette(header, bits, bytes_per_line);
return true;
}
@ -312,20 +114,15 @@ namespace nana{ namespace paint
bool open(const std::experimental::filesystem::path& filename) override
{
std::ifstream ifs(filename.string(), std::ios::binary);
if(ifs)
auto const bytes = static_cast<unsigned>(std::experimental::filesystem::file_size(filename));
if (ifs && (bytes > static_cast<int>(sizeof(bitmap_file_header))))
{
ifs.seekg(0, std::ios::end);
auto size = static_cast<std::size_t>(ifs.tellg());
ifs.seekg(0, std::ios::beg);
std::unique_ptr<char[]> buffer{ new char[bytes] };
if(size <= static_cast<int>(sizeof(bitmap_file_header)))
return false;
std::unique_ptr<char[]> buffer(new char[static_cast<int>(size)]);
ifs.read(buffer.get(), size);
if (size == static_cast<std::size_t>(ifs.gcount()))
return open(buffer.get(), size);
ifs.read(buffer.get(), bytes);
if (bytes == static_cast<std::size_t>(ifs.gcount()))
return open(buffer.get(), bytes);
}
return false;
}
@ -334,6 +131,96 @@ namespace nana{ namespace paint
{
return false;
}
private:
void _m_put_with_palette(const bitmap_info_header* header, const unsigned char* pixel_indexes, unsigned line_bytes)
{
auto const image_height = std::abs(header->biHeight);
const std::size_t total_pixels = header->biWidth * static_cast<std::size_t>(image_height);
auto const color_table = reinterpret_cast<const rgb_quad*>(reinterpret_cast<const unsigned char*>(header) + header->biSize);
auto dst_px = pixbuf_.raw_ptr(0);
auto const end_dst_px = dst_px + total_pixels;
int line_pos = image_height - 1;
int delta = -1;
if (header->biHeight < 0)
{
line_pos = 0;
delta = 1;
}
if (8 == header->biBitCount)
{
while (dst_px < end_dst_px)
{
auto px_indexes = pixel_indexes + line_bytes * line_pos;
auto const line_end_dst_px = dst_px + header->biWidth;
while (dst_px != line_end_dst_px)
{
auto & rgb = color_table[*px_indexes++];
dst_px->element.red = rgb.rgbRed;
dst_px->element.green = rgb.rgbGreen;
dst_px->element.blue = rgb.rgbBlue;
dst_px->element.alpha_channel = rgb.rgbReserved;
++dst_px;
}
line_pos += delta;
}
}
else
{
while (dst_px < end_dst_px)
{
auto px_indexes = pixel_indexes + line_bytes * line_pos;
auto const line_end_dst_px = dst_px + header->biWidth;
std::size_t pos = 0;
switch (header->biBitCount)
{
case 4:
while (dst_px != line_end_dst_px)
{
auto & rgb = color_table[((pos & 1) ? px_indexes[pos >> 1] : (px_indexes[pos >> 1] >> 4)) & 0xF];
dst_px->element.red = rgb.rgbRed;
dst_px->element.green = rgb.rgbGreen;
dst_px->element.blue = rgb.rgbBlue;
dst_px->element.alpha_channel = rgb.rgbReserved;
++dst_px;
++pos;
}
break;
case 2:
while (dst_px != line_end_dst_px)
{
//auto const shift = ((3 - (pos & 0x3)) << 1); // (index % 4) * 2
auto& rgb = color_table[(px_indexes[pos >> 2] >> ((3 - (pos & 0x3)) << 1)) & 0x3];
dst_px->element.red = rgb.rgbRed;
dst_px->element.green = rgb.rgbGreen;
dst_px->element.blue = rgb.rgbBlue;
dst_px->element.alpha_channel = rgb.rgbReserved;
++dst_px;
++pos;
}
break;
case 1:
while (dst_px != line_end_dst_px)
{
auto & rgb = color_table[(px_indexes[pos >> 3] >> (7 - (pos & 7))) & 1];
dst_px->element.red = rgb.rgbRed;
dst_px->element.green = rgb.rgbGreen;
dst_px->element.blue = rgb.rgbBlue;
dst_px->element.alpha_channel = rgb.rgbReserved;
++dst_px;
++pos;
}
break;
}
line_pos += delta;
}
}
}
};//end class bmpfile
}//end namespace detail
}//end namespace paint