Skip to content

Commit 8d9cd38

Browse files
bastien-roucarieschearon
authored andcommitted
Fix endian of bmparser
forwarded: #2599
1 parent 021a4b3 commit 8d9cd38

2 files changed

Lines changed: 67 additions & 18 deletions

File tree

‎src/Image.cc‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,30 @@
1111
#include <node_buffer.h>
1212
#include <sys/stat.h>
1313

14+
#include <array>
15+
#include <bit>
16+
1417
/* Cairo limit:
1518
* https://lists.cairographics.org/archives/cairo/2010-December/021422.html
1619
*/
1720
static constexpr int canvas_max_side = (1 << 15) - 1;
1821

22+
template<std::endian E = std::endian::native>
23+
constexpr auto pixFmt() {
24+
std::array<uint8_t,5> fmt{};
25+
26+
if constexpr (E == std::endian::little) {
27+
// Cairo ARGB32 → BGRA bytes
28+
fmt = {2, 1, 0, 3, 1};
29+
} else {
30+
// Cairo ARGB32 → ARGB bytes
31+
fmt = {3, 0, 1, 2, 1};
32+
}
33+
34+
return fmt;
35+
}
36+
37+
1938
#ifdef HAVE_GIF
2039
typedef struct {
2140
uint8_t *buf;
@@ -1576,8 +1595,8 @@ cairo_status_t Image::loadBMPFromBuffer(uint8_t *buf, unsigned len){
15761595
BMPParser::Parser parser;
15771596

15781597
// Reversed ARGB32 with pre-multiplied alpha
1579-
uint8_t pixFmt[5] = {2, 1, 0, 3, 1};
1580-
parser.parse(buf, len, pixFmt);
1598+
auto fmt = pixFmt<>();
1599+
parser.parse(buf, len, fmt.data());
15811600

15821601
if (parser.getStatus() != BMPParser::Status::OK) {
15831602
errorInfo.reset();
@@ -1587,20 +1606,30 @@ cairo_status_t Image::loadBMPFromBuffer(uint8_t *buf, unsigned len){
15871606

15881607
width = naturalWidth = parser.getWidth();
15891608
height = naturalHeight = parser.getHeight();
1609+
uint32_t src_stride = width * sizeof(uint32_t);
15901610
uint8_t *data = parser.getImgd();
15911611

1592-
_surface = cairo_image_surface_create_for_data(
1593-
data,
1612+
int dst_stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
1613+
_surface = cairo_image_surface_create(
15941614
CAIRO_FORMAT_ARGB32,
15951615
width,
1596-
height,
1597-
cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width)
1616+
height
15981617
);
15991618

16001619
// No need to delete the data
16011620
cairo_status_t status = cairo_surface_status(_surface);
16021621
if (status) return status;
16031622

1623+
uint8_t *dst = cairo_image_surface_get_data(_surface);
1624+
for (int y = 0; y < height; y++) {
1625+
memcpy(
1626+
dst + y * dst_stride,
1627+
data + y * src_stride,
1628+
src_stride
1629+
);
1630+
}
1631+
1632+
cairo_surface_mark_dirty(_surface);
16041633
_data = data;
16051634
parser.clearImgd();
16061635

‎src/bmp/BMPParser.cc‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#include <cassert>
44
#include <cstring>
55

6+
#include <bit>
7+
#include <cstdint>
8+
#include <cstring>
9+
10+
11+
612
using namespace std;
713
using namespace BMPParser;
814

@@ -382,22 +388,36 @@ string Parser::getErrMsg() const{
382388
return "Error while processing " + getOp() + " - " + err;
383389
}
384390

385-
template <typename T, bool check> inline T Parser::get(){
386-
if(check)
387-
CHECK_OVERRUN(ptr, sizeof(T), T);
388-
T val;
389-
std::memcpy(&val, ptr, sizeof(T));
390-
ptr += sizeof(T);
391-
return val;
391+
392+
template <typename T, bool check>
393+
inline T Parser::get() {
394+
if (check)
395+
CHECK_OVERRUN(ptr, sizeof(T), T);
396+
397+
T val;
398+
std::memcpy(&val, ptr, sizeof(T));
399+
ptr += sizeof(T);
400+
401+
if constexpr (sizeof(T) > 1 && std::endian::native == std::endian::big)
402+
val = std::byteswap(val);
403+
return val;
392404
}
393405

394-
template <typename T, bool check> inline T Parser::get(uint8_t* pointer){
395-
if(check)
396-
CHECK_OVERRUN(pointer, sizeof(T), T);
397-
T val = *(T*)pointer;
398-
return val;
406+
template <typename T, bool check>
407+
inline T Parser::get(uint8_t* pointer) {
408+
if (check)
409+
CHECK_OVERRUN(pointer, sizeof(T), T);
410+
411+
T val;
412+
std::memcpy(&val, pointer, sizeof(T));
413+
414+
if constexpr (sizeof(T) > 1 && std::endian::native == std::endian::big)
415+
val = std::byteswap(val);
416+
417+
return val;
399418
}
400419

420+
401421
string Parser::getStr(int size, bool reverse){
402422
CHECK_OVERRUN(ptr, size, string);
403423
string val = "";

0 commit comments

Comments
 (0)