bsnes/nall/image/core.hpp

174 lines
4.3 KiB
C++
Raw Normal View History

#pragma once
namespace nall {
image::image(const image& source) {
operator=(source);
Update to v094r09 release. byuu says: This will easily be the biggest diff in the history of higan. And not in a good way. * target-higan and target-loki have been blown away completely * nall and ruby massively updated * phoenix replaced with hiro (pretty near a total rewrite) * target-higan restarted using hiro (just a window for now) * all emulation cores updated to compile again * installation changed to not require root privileges (installs locally) For the foreseeable future (maybe even permanently?), the new higan UI will only build under Linux/BSD with GTK+ 2.20+. Probably the most likely route for Windows/OS X will be to try and figure out how to build hiro/GTK on those platforms, as awful as that would be. The other alternative would be to produce new UIs for those platforms ... which would actually be a good opportunity to make something much more user friendly. Being that I just started on this a few hours ago, that means that for at least a few weeks, don't expect to be able to actually play any games. Right now, you can pretty much just compile the binary and that's it. It's quite possible that some nall changes didn't produce compilation errors, but will produce runtime errors. So until the UI can actually load games, we won't know if anything is broken. But we should mostly be okay. It was mostly just trim<1> -> trim changes, moving to Hash::SHA256 (much cleaner), and patching some reckless memory copy functions enough to compile. Progress isn't going to be like it was before: I'm now dividing my time much thinner between studying and other hobbies. My aim this time is not to produce a binary for everyone to play games on. Rather, it's to keep the emulator alive. I want to be able to apply critical patches again. And I would also like the base of the emulator to live on, for use in other emulator frontends that utilize higan.
2015-02-26 10:10:46 +00:00
}
image::image(image&& source) {
operator=(forward<image>(source));
}
image::image(bool endian, unsigned depth, uint64_t alphaMask, uint64_t redMask, uint64_t greenMask, uint64_t blueMask) {
_endian = endian;
_depth = depth;
_alpha = {alphaMask, bitDepth(alphaMask), bitShift(alphaMask)};
_red = {redMask, bitDepth(redMask), bitShift(redMask )};
_green = {greenMask, bitDepth(greenMask), bitShift(greenMask)};
_blue = {blueMask, bitDepth(blueMask), bitShift(blueMask )};
}
image::image(const string& filename) {
load(filename);
}
image::image(const void* data_, uint size) {
auto data = (const uint8_t*)data_;
if(size < 4);
else if(data[0] == 'B' && data[1] == 'M') loadBMP(data, size);
else if(data[1] == 'P' && data[2] == 'N' && data[3] == 'G') loadPNG(data, size);
}
image::image(const vector<uint8_t>& buffer) : image(buffer.data(), buffer.size()) {
}
template<uint Size> image::image(const uint8_t (&Name)[Size]) : image(Name, Size) {
}
image::image() {
}
image::~image() {
free();
}
auto image::operator=(const image& source) -> image& {
if(this == &source) return *this;
free();
_width = source._width;
_height = source._height;
_endian = source._endian;
_depth = source._depth;
_alpha = source._alpha;
_red = source._red;
_green = source._green;
_blue = source._blue;
_data = allocate(_width, _height, stride());
memory::copy(_data, source._data, source.size());
return *this;
}
auto image::operator=(image&& source) -> image& {
if(this == &source) return *this;
free();
_width = source._width;
_height = source._height;
_endian = source._endian;
_depth = source._depth;
_alpha = source._alpha;
_red = source._red;
_green = source._green;
_blue = source._blue;
_data = source._data;
source._data = nullptr;
return *this;
}
image::operator bool() const {
return _data && _width && _height;
}
auto image::operator==(const image& source) const -> bool {
if(_width != source._width) return false;
if(_height != source._height) return false;
if(_endian != source._endian) return false;
if(_depth != source._depth) return false;
Update to v094r09 release. byuu says: This will easily be the biggest diff in the history of higan. And not in a good way. * target-higan and target-loki have been blown away completely * nall and ruby massively updated * phoenix replaced with hiro (pretty near a total rewrite) * target-higan restarted using hiro (just a window for now) * all emulation cores updated to compile again * installation changed to not require root privileges (installs locally) For the foreseeable future (maybe even permanently?), the new higan UI will only build under Linux/BSD with GTK+ 2.20+. Probably the most likely route for Windows/OS X will be to try and figure out how to build hiro/GTK on those platforms, as awful as that would be. The other alternative would be to produce new UIs for those platforms ... which would actually be a good opportunity to make something much more user friendly. Being that I just started on this a few hours ago, that means that for at least a few weeks, don't expect to be able to actually play any games. Right now, you can pretty much just compile the binary and that's it. It's quite possible that some nall changes didn't produce compilation errors, but will produce runtime errors. So until the UI can actually load games, we won't know if anything is broken. But we should mostly be okay. It was mostly just trim<1> -> trim changes, moving to Hash::SHA256 (much cleaner), and patching some reckless memory copy functions enough to compile. Progress isn't going to be like it was before: I'm now dividing my time much thinner between studying and other hobbies. My aim this time is not to produce a binary for everyone to play games on. Rather, it's to keep the emulator alive. I want to be able to apply critical patches again. And I would also like the base of the emulator to live on, for use in other emulator frontends that utilize higan.
2015-02-26 10:10:46 +00:00
if(_alpha != source._alpha) return false;
if(_red != source._red) return false;
if(_green != source._green) return false;
if(_blue != source._blue) return false;
return memory::compare(_data, source._data, size()) == 0;
}
auto image::operator!=(const image& source) const -> bool {
return !operator==(source);
}
auto image::read(const uint8_t* data) const -> uint64_t {
uint64_t result = 0;
if(_endian == 0) {
for(signed n = stride() - 1; n >= 0; n--) result = (result << 8) | data[n];
} else {
for(signed n = 0; n < stride(); n++) result = (result << 8) | data[n];
}
return result;
}
auto image::write(uint8_t* data, uint64_t value) const -> void {
if(_endian == 0) {
for(signed n = 0; n < stride(); n++) {
data[n] = value;
value >>= 8;
}
} else {
for(signed n = stride() - 1; n >= 0; n--) {
data[n] = value;
value >>= 8;
}
}
}
auto image::free() -> void {
if(_data) delete[] _data;
_data = nullptr;
}
auto image::load(const string& filename) -> bool {
if(loadBMP(filename) == true) return true;
if(loadPNG(filename) == true) return true;
return false;
}
//assumes image and data are in the same format; pitch is adapted to image
auto image::copy(const void* data, uint pitch, uint width, uint height) -> void {
allocate(width, height);
for(uint y : range(height)) {
auto input = (const uint8_t*)data + y * pitch;
auto output = (uint8_t*)_data + y * this->pitch();
memory::copy(output, input, width * stride());
}
}
auto image::allocate(unsigned width, unsigned height) -> void {
if(_data && _width == width && _height == height) return;
free();
_width = width;
_height = height;
_data = allocate(_width, _height, stride());
}
//private
auto image::allocate(unsigned width, unsigned height, unsigned stride) -> uint8_t* {
//allocate 1x1 larger than requested; so that linear interpolation does not require bounds-checking
unsigned size = width * height * stride;
unsigned padding = width * stride + stride;
auto data = new uint8_t[size + padding];
memory::fill(data + size, padding);
return data;
}
}