shithub: pain

ref: 4bcb73eb20638b1564d6757405508ea63ccf5f14
dir: /resize.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>

#include "dat.h"
#include "fns.h"

// Inspired by /sys/src/games/doom/i_video.c:/^convproc
// and /sys/src/cmd/page.c:/^zoomdraw
// Thanks sigrid!
int
resizeimage(Image * di, Rectangle dr, int scale, Image * si, Point sp)
{
	int i, c, sh;
	Image * t, * ci;

	if (scale < 1) {
		werrstr("scale < 1");
		return -1;
	} else if (scale == 1) {
		draw(di, dr, si, nil, addpt(sp, dr.min));
		return 1;
	}

	sh = Dy(si->r);
	if ((t = allocimage(display, dr, si->chan, 1, DNofill)) == nil) {
		werrstr("allocimage: %r");
		return -1;
	}

	if ((ci = allocimage(display, Rect(0, 0, 1, sh), si->chan, 1, DNofill)) == nil) {
		werrstr("allocimage: %r");
		freeimage(t);
		return -1;
	}

	for (i = dr.min.x; i < dr.max.x; i+=scale) {
		draw(ci, ci->r, si, nil, addpt(sp, Pt(i/scale, dr.min.y/scale)));
		draw(t, Rect(i, dr.min.y, i + scale, dr.max.y), ci, nil, ZP);
	}
	freeimage(ci);
	
	if ((ci = allocimage(display, Rect(0, 0, Dx(dr), 1), si->chan, 1, DNofill)) == nil) {
		werrstr("allocimage: %r");
		freeimage(t);
		return -1;
	}
	c = dr.min.y;
 	for (i = dr.min.y; i < dr.max.y; i+=scale) {
		draw(ci, ci->r, t, nil, Pt(dr.min.x, c++));
 		draw(di, Rect(dr.min.x, i, dr.max.x, i + scale), ci, nil, ZP);
	}

	freeimage(t);
	freeimage(ci);

// 	x = dr.min.y;
// 	for (i = sp.y; i < sh; i++) 
// 		draw(di, Rect(dr.min.x, x, dr.max.x, (x += scale)), t, nil, dr.min);
// 	freeimage(t);

// 	
// 	// Draw the cursor for vertical line
//  	if (t == nil || ) {
//  		if (t != nil)
//  			freeimage(t);
//  	 	t = allocimage(display, Rect(0, 0, sw*scale, sh), si->chan, 1, DNofill);
//  	 	if (t == nil) {
//  	 		return -1;
//  	 	}
//  	}

// 	// print("min.x = %d, min.y = %d, max.x = %d, max.y = %d\n",
// 	//	clipr.min.x, clipr.min.y, clipr.max.x, clipr.max.y);
// 	r = Rect(0, 0, 1, sh);
// 	for (i = 0; i < sw; i++) {
// 		cr = Rect(i*scale, 0, (i+1)*scale, r.max.y);
// 		if (!rectclip(&cr, clipr))
// 			continue;
// 		draw(t, cr, si, nil, Pt(i, 0));
// 	}

//  	// Draw the cursor for vertical lines
// 	cr = di->clipr;
//     r = Rect(0, 0, sw*scale, 1);
//    	for (i = 0; i < sh; i++) {
// 		cr = Rect(0, i*scale, r.max.x, (i+1)*scale);
// 		if (!rectclip(&cr, clipr))
// 			continue;
//    		draw(di, cr, t, nil, Pt(0, i));
// 	}


	return 1;
}