ref: 453dfd1fdd9780c0381cc3fd8c5a63edfb623e3a
dir: /resample.c/
#include <u.h> #include <libc.h> #include <draw.h> #include <memdraw.h> #define STB_IMAGE_RESIZE_IMPLEMENTATION #define STBIR_MALLOC(x,u) malloc(x) #define STBIR_FREE(x,u) free(x) #define STBIR_ASSERT(x) assert(x) #define NULL nil typedef uintptr size_t; #include "stb_image_resize.h" static char *filters[] = { [STBIR_FILTER_BOX] = "box", [STBIR_FILTER_TRIANGLE] = "triangle", [STBIR_FILTER_CUBICBSPLINE] = "cubicbspline", [STBIR_FILTER_CATMULLROM] = "catmullrom", [STBIR_FILTER_MITCHELL] = "mitchell", }; static char *cspaces[] = { [STBIR_COLORSPACE_LINEAR] = "linear", [STBIR_COLORSPACE_SRGB] = "srgb", }; static void usage(void) { fprint(2, "usage: %s [-P] [-x size] [-y size] [-f filter] [-c colorspace]\n", argv0); exits("usage"); } void main(int argc, char **argv) { Memimage *a, *b; u8int *in, *out; char *flts, *s, *csps; int alphai, flags; int w, h, ow, oh; int wp, hp, f, c; int n, bp, to; ow = oh = 0; wp = hp = 0; alphai = STBIR_ALPHA_CHANNEL_NONE; flags = STBIR_FLAG_ALPHA_PREMULTIPLIED; flts = filters[STBIR_FILTER_MITCHELL]; csps = cspaces[STBIR_COLORSPACE_LINEAR]; ARGBEGIN{ case 'x': s = EARGF(usage()); wp = (ow = atoi(s)) > 0 && s[strlen(s)-1] == '%'; break; case 'y': s = EARGF(usage()); hp = (oh = atoi(s)) > 0 && s[strlen(s)-1] == '%'; break; case 'f': flts = EARGF(usage()); break; case 'c': csps = EARGF(usage()); break; case 'P': flags &= ~STBIR_FLAG_ALPHA_PREMULTIPLIED; break; default: usage(); }ARGEND if(wp && oh == 0){ oh = ow; hp = 1; }else if(hp && ow == 0){ ow = oh; wp = 1; } if(ow < 1 && oh < 1) usage(); for(f = 0; f < nelem(filters) && (filters[f] == nil || strcmp(flts, filters[f]) != 0); f++); if(f >= nelem(filters)){ fprint(2, "invalid filter %s\n", flts); exits("filter"); } for(c = 0; c < nelem(cspaces) && strcmp(csps, cspaces[c]) != 0; c++); if(c >= nelem(cspaces)){ fprint(2, "invalid colorspace %s\n", csps); exits("colorspace"); } memimageinit(); if((a = readmemimage(0)) == nil) sysfatal("memory"); bp = 0; again: switch(a->chan){ case GREY8: bp = 1; break; case RGB24: bp = 3; break; case RGBA32: bp = 4; alphai = 3; break; case ARGB32: case XRGB32: bp = 4; alphai = 0; break; case GREY1: case GREY2: case GREY4: to = GREY8; goto convert; case CMAP8: case RGB15: case RGB16: to = RGB24; goto convert; case BGR24: to = RGB24; goto convert; case ABGR32: to = ARGB32; goto convert; case XBGR32: to = XRGB32; convert: if((b = allocmemimage(a->r, to)) == nil) sysfatal("memory"); memimagedraw(b, a->r, a, ZP, nil, ZP, S); freememimage(a); a = b; goto again; default: sysfatal("invalid chan %#lux", a->chan); } w = Dx(a->r); h = Dy(a->r); if(wp) ow = w*ow/100.0; if(hp) oh = h*oh/100.0; if(oh == 0) oh = ow*h/w; if(ow == 0) ow = oh*w/h; if(ow < 1 || oh < 1) sysfatal("invalid size: %dx%d", ow, oh); n = w*h*bp; if((in = malloc(w*h*bp)) == nil) sysfatal("%r"); if(unloadmemimage(a, a->r, in, n) < 0) sysfatal("%r"); if((out = malloc(ow*oh*bp)) == nil) sysfatal("memory"); stbir_resize_uint8_generic( in, w, h, w*bp, out, ow, oh, ow*bp, bp, alphai, flags, STBIR_EDGE_CLAMP, f, c, NULL); free(in); if((b = allocmemimage(Rect(0,0,ow,oh), a->chan)) == nil) sysfatal("%r"); loadmemimage(b, b->r, out, ow*oh*bp); writememimage(1, b); exits(nil); }