1 #include "lua/bitmap.hpp"
2 #include "library/zip.hpp"
7 inline uint64_t tocolor(unsigned r
, unsigned g
, unsigned b
, unsigned a
)
12 return (static_cast<int64_t>(256 - a
) << 24) | (static_cast<int64_t>(r
) << 16) |
13 (static_cast<int64_t>(g
) << 8) | static_cast<int64_t>(b
);
17 struct lua_loaded_bitmap
lua_loaded_bitmap::load(std::istream
& file
)
19 struct lua_loaded_bitmap b
;
27 if(magic
!= "LSNES-BITMAP")
28 throw std::runtime_error("Bitmap load: Wrong magic");
31 if(b
.h
>= std::numeric_limits
<size_t>::max() / b
.w
)
32 throw std::runtime_error("Bitmap load: Bitmap too large");
33 b
.bitmap
.resize(b
.w
* b
.h
);
36 throw std::runtime_error("Bitmap load: Palette too big");
40 b
.palette
.resize(pcolors
);
41 for(size_t i
= 0; i
< pcolors
; i
++) {
46 if(R
> 255 || G
> 255 || B
> 255 || A
> 256) //Yes, a can be 256.
47 throw std::runtime_error("Bitmap load: Palette entry out of range");
48 b
.palette
[i
] = tocolor(R
, G
, B
, A
);
50 for(size_t i
= 0; i
< b
.w
* b
.h
; i
++) {
53 throw std::runtime_error("Bitmap load: color index out of range");
59 for(size_t i
= 0; i
< b
.w
* b
.h
; i
++) {
64 if(R
> 255 || G
> 255 || B
> 255 || A
> 256) //Yes, a can be 256.
65 throw std::runtime_error("Bitmap load: Color out of range");
66 b
.bitmap
[i
] = tocolor(R
, G
, B
, A
);
70 throw std::runtime_error("Bitmap load: Error reading bitmap");
74 struct lua_loaded_bitmap
lua_loaded_bitmap::load(const std::string
& name
)
76 struct lua_loaded_bitmap b
;
77 std::istream
& file
= zip::openrel(name
, "");
79 b
= lua_loaded_bitmap::load(file
);