浏览代码

added fmt.img_scale, minor gl fix

Nicolas Cannasse 9 年之前
父节点
当前提交
55bcce220a
共有 2 个文件被更改,包括 48 次插入1 次删除
  1. 47 0
      libs/fmt/fmt.c
  2. 1 1
      libs/sdl/gl.c

+ 47 - 0
libs/fmt/fmt.c

@@ -2,6 +2,10 @@
 #include <turbojpeg.h>
 #include <hl.h>
 
+typedef struct {
+	unsigned char a,r,g,b;
+} pixel;
+
 HL_PRIM bool HL_NAME(jpg_decode)( vbyte *data, int dataLen, vbyte *out, int width, int height, int stride, int format, int flags ) {
 	tjhandle h = tjInitDecompress();
 	int result;
@@ -10,4 +14,47 @@ HL_PRIM bool HL_NAME(jpg_decode)( vbyte *data, int dataLen, vbyte *out, int widt
 	return result == 0;
 }
 
+HL_PRIM void HL_NAME(img_scale)( vbyte *out, int outPos, int outStride, int outWidth, int outHeight, vbyte *in, int inPos, int inStride, int inWidth, int inHeight, int flags ) {
+	int x, y;
+	float scaleX = outWidth <= 1 ? 0.0f : (float)((inWidth - 1.001f) / (outWidth - 1));
+	float scaleY = outHeight <= 1 ? 0.0f : (float)((inHeight - 1.001f) / (outHeight - 1));
+	out += outPos;
+	in += inPos;
+	for(y=0;y<outHeight;y++) {
+		for(x=0;x<outWidth;x++) {
+			float fx = x * scaleX;
+			float fy = y * scaleY;
+			int ix = (int)fx;
+			int iy = (int)fy;
+			if( (flags & 1) == 0 ) {
+				// nearest
+				vbyte *rin = in + iy * inStride;
+				*(pixel*)out = *(pixel*)(rin + (ix<<2));
+				out += 4;
+			} else {
+				// bilinear
+				float rx = fx - ix;
+				float ry = fy - iy;
+				float rx1 = 1.0f - rx;
+				float ry1 = 1.0f - ry;
+				int w1 = (int)(rx1 * ry1 * 256.0f + 0.5f);
+				int w2 = (int)(rx * ry1 * 256.0f + 0.5f);
+				int w3 = (int)(rx1 * ry * 256.0f + 0.5f);
+				int w4 = (int)(rx * ry * 256.0f + 0.5f); 
+				vbyte *rin = in + iy * inStride;
+				pixel p1 = *(pixel*)(rin + (ix<<2));
+				pixel p2 = *(pixel*)(rin + ((ix + 1)<<2));
+				pixel p3 = *(pixel*)(rin + inStride + (ix<<2));
+				pixel p4 = *(pixel*)(rin + inStride + ((ix + 1)<<2));
+				*out++ = (unsigned char)((p1.a * w1 + p2.a * w2 + p3.a * w3 + p4.a * w4 + 128)>>8);
+				*out++ = (unsigned char)((p1.r * w1 + p2.r * w2 + p3.r * w3 + p4.r * w4 + 128)>>8);
+				*out++ = (unsigned char)((p1.g * w1 + p2.g * w2 + p3.g * w3 + p4.g * w4 + 128)>>8);
+				*out++ = (unsigned char)((p1.b * w1 + p2.b * w2 + p3.b * w3 + p4.b * w4 + 128)>>8);
+			}
+		}
+		out += outStride - (outWidth << 2);
+	}
+}
+
 DEFINE_PRIM(_BOOL, jpg_decode, _BYTES _I32 _BYTES _I32 _I32 _I32 _I32 _I32);
+DEFINE_PRIM(_VOID, img_scale, _BYTES _I32 _I32 _I32 _I32 _BYTES _I32 _I32 _I32 _I32 _I32);

+ 1 - 1
libs/sdl/gl.c

@@ -292,7 +292,7 @@ HL_PRIM vdynamic *HL_NAME(gl_create_renderbuffer)() {
 }
 
 HL_PRIM void HL_NAME(gl_bind_renderbuffer)( int target, vdynamic *r ) {
-	glBindRenderbuffer(target, r->v.i);
+	glBindRenderbuffer(target, ZIDX(r));
 }
 
 HL_PRIM void HL_NAME(gl_renderbuffer_storage)( int target, int format, int width, int height ) {