first go at generic resize_bitmap() for NCCSCALE_INFLATE #1576

pull/1713/head
nick black 3 years ago committed by Nick Black
parent 3a6a2fa90b
commit f2fefacfc0

@ -1063,8 +1063,48 @@ int ncvisual_resize(ncvisual* nc, int rows, int cols){
return 0;
}
// naive resize of |bmap| from |srows|x|scols| -> |drows|x|dcols|, suitable for
// pixel art. we either select at a constant interval (for shrinking) or duplicate
// at a constant ratio (for inflation). in the absence of a multimedia engine, this
// is the only kind of resizing we support.
static inline uint32_t*
resize_bitmap(const uint32_t* bmap, int srows, int scols, size_t sstride,
int drows, int dcols, size_t dstride){
if(sstride < scols * sizeof(*bmap)){
return NULL;
}
if(dstride < dcols * sizeof(*bmap)){
return NULL;
}
size_t size = drows * dstride;
uint32_t* ret = malloc(size);
if(ret == NULL){
return NULL;
}
float xrat = (float)dcols / scols;
float yrat = (float)drows / srows;
fprintf(stderr, "xrat: %f yrat: %f\n", xrat, yrat);
int dy = 0;
for(int y = 0 ; y < srows ; ++y){
float ytarg = y * yrat;
while(ytarg > dy){
int dx = 0;
for(int x = 0 ; x < scols ; ++x){
float xtarg = x * xrat;
while(xtarg > dx){
ret[dy * dstride / sizeof(*ret) + dx] = bmap[y * sstride / sizeof(*ret) + x];
++dx;
}
}
}
++dy;
}
return ret;
}
// Inflate each pixel of 'bmap' to 'scale'x'scale' pixels square, using the
// same color as the original pixel.
// FIXME replace with resize_bitmap()
static inline void*
inflate_bitmap(const uint32_t* bmap, int scale, int rows, int stride, int cols){
size_t size = rows * cols * scale * scale * sizeof(*bmap);

Loading…
Cancel
Save