| 1 |
module SDL_image; |
|---|
| 2 |
|
|---|
| 3 |
import qd, tools.compat; |
|---|
| 4 |
extern(C) { |
|---|
| 5 |
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc); |
|---|
| 6 |
int SDL_SaveBMP_RW(SDL_Surface *what, SDL_RWops *dst, int freedst); |
|---|
| 7 |
} |
|---|
| 8 |
|
|---|
| 9 |
class Image : Area { |
|---|
| 10 |
import tools.log; |
|---|
| 11 |
this(void[] data) { |
|---|
| 12 |
tl=pt(0, 0); |
|---|
| 13 |
auto ops=SDL_RWFromMem(data.ptr, data.length); |
|---|
| 14 |
surf = RefSurf(IMG_Load_RW(ops, true)); |
|---|
| 15 |
if (!surf.surface) throw new Exception("Could not interpret data"); |
|---|
| 16 |
surf.refs ++; |
|---|
| 17 |
dimensions = pt(surface.w, surface.h); |
|---|
| 18 |
} |
|---|
| 19 |
private this() { } |
|---|
| 20 |
~this() { if (surf.surface && !--surf.refs) SDL_FreeSurface(surface); } |
|---|
| 21 |
Image map(string NEWSIZE, string SRCPOS, T...)(T t) { |
|---|
| 22 |
auto res = mixin("SDL_CreateRGBSurface(0, "~NEWSIZE~", 32)"); |
|---|
| 23 |
ubyte[4] pixel; |
|---|
| 24 |
auto surf = surface(); |
|---|
| 25 |
switch (surface.format.BytesPerPixel) { |
|---|
| 26 |
case 3: |
|---|
| 27 |
for (int y=0; y<res.h; ++y) for (int x=0; x<res.w; ++x) { |
|---|
| 28 |
mixin("getpixel24(surf, "~SRCPOS~", &pixel); "); |
|---|
| 29 |
putpixel32(res, x, y, SDL_MapRGBA(res.format, pixel[0], pixel[1], pixel[2], 0)); |
|---|
| 30 |
} |
|---|
| 31 |
break; |
|---|
| 32 |
case 4: |
|---|
| 33 |
for (int y=0; y<res.h; ++y) for (int x=0; x<res.w; ++x) { |
|---|
| 34 |
mixin("getpixel32(surf, "~SRCPOS~", &pixel); "); |
|---|
| 35 |
putpixel32(res, x, y, SDL_MapRGBA(res.format, pixel[0], pixel[1], pixel[2], pixel[3])); |
|---|
| 36 |
} |
|---|
| 37 |
break; |
|---|
| 38 |
default: throw new Exception("invalid depth"); |
|---|
| 39 |
} |
|---|
| 40 |
auto img = new Image; |
|---|
| 41 |
img.surf = RefSurf(res); |
|---|
| 42 |
img.surf.refs ++; |
|---|
| 43 |
img.dimensions = mixin("pt("~NEWSIZE~")"); |
|---|
| 44 |
return img; |
|---|
| 45 |
} |
|---|
| 46 |
alias map!("t[0], t[1]", "(x*surface.w)/t[0], (y*surface.h)/t[1]", int, int) nearscale; |
|---|
| 47 |
alias map!("dimensions.x, dimensions.y", "x, dimensions.y - 1 - y") xflip; |
|---|
| 48 |
alias map!("dimensions.x, dimensions.y", "dimensions.x - 1 - x, y") yflip; |
|---|
| 49 |
alias map!("dimensions.y, dimensions.x", "y, x") flip; |
|---|
| 50 |
Image rot_right() { return flip().yflip(); } |
|---|
| 51 |
Image rot_left() { return flip().xflip(); } |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
void SaveBMP(Area area, string name) { |
|---|
| 55 |
if (-1==SDL_SaveBMP_RW(area.surface, SDL_RWFromFile(toStringz(name), toStringz("wb")), 1)) throw new Exception("Couldn't save "~name);; |
|---|
| 56 |
} |
|---|