scaling pgm images

This commit is contained in:
Kartik K. Agaram 2021-07-08 16:42:30 -07:00
parent f07ebb9e5e
commit b8488c5223
1 changed files with 65 additions and 21 deletions

86
img.mu
View File

@ -16,6 +16,7 @@ fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk)
var img/esi: (addr image) <- address img-storage
load-image img, data-disk
render-image screen, img, 0x20/x 0x80/y, 0x100/width, 0x100/height
render-image screen, img, 0x120/x 0x180/y, 0x12c/width=300, 0xc8/height=200
render-image screen, img, 0x320/x 0x280/y, 0x60/width=96, 0x1c/height=28
}
@ -213,6 +214,7 @@ fn initialize-image-from-ppm _self: (addr image), in: (addr stream byte) {
}
}
# dispatch to a few variants with mostly identical boilerplate
fn render-image screen: (addr screen), _img: (addr image), xmin: int, ymin: int, width: int, height: int {
var img/esi: (addr image) <- copy _img
var type-a/eax: (addr int) <- get img, type
@ -237,6 +239,7 @@ fn render-image screen: (addr screen), _img: (addr image), xmin: int, ymin: int,
abort "render-image: unrecognized image type"
}
# portable bitmap: each pixel is 0 or 1
fn render-pbm-image screen: (addr screen), _img: (addr image), xmin: int, ymin: int, width: int, height: int {
var img/esi: (addr image) <- copy _img
# yratio = height/img->height
@ -315,35 +318,76 @@ fn render-pbm-image screen: (addr screen), _img: (addr image), xmin: int, ymin:
}
}
# portable greymap: each pixel is a shade of grey from 0 to 255
fn render-pgm-image screen: (addr screen), _img: (addr image), xmin: int, ymin: int, width: int, height: int {
var img/esi: (addr image) <- copy _img
var img-width-a/ecx: (addr int) <- get img, width
var data-ah/eax: (addr handle array byte) <- get img, data
var _data/eax: (addr array byte) <- lookup *data-ah
var data/esi: (addr array byte) <- copy _data
var y/edx: int <- copy ymin
var i/edi: int <- copy 0
var max/eax: int <- length data
# yratio = height/img->height
var img-height-a/eax: (addr int) <- get img, height
var img-height/xmm0: float <- convert *img-height-a
var yratio/xmm1: float <- convert height
yratio <- divide img-height
# xratio = width/img->width
var img-width-a/eax: (addr int) <- get img, width
var img-width/ebx: int <- copy *img-width-a
var img-width-f/xmm0: float <- convert img-width
var xratio/xmm2: float <- convert width
xratio <- divide img-width-f
# esi = img->data
var img-data-ah/eax: (addr handle array byte) <- get img, data
var _img-data/eax: (addr array byte) <- lookup *img-data-ah
var img-data/esi: (addr array byte) <- copy _img-data
var len/edi: int <- length img-data
#
var one/eax: int <- copy 1
var one-f/xmm3: float <- convert one
var width-f/xmm4: float <- convert width
var height-f/xmm5: float <- convert height
var zero/eax: int <- copy 0
var zero-f/xmm0: float <- convert zero
var y/xmm6: float <- copy zero-f
set-cursor-position 0/screen, 0x20/x 0x20/y
{
compare i, max
break-if->=
var x/ebx: int <- copy xmin
var img-x/eax: int <- copy 0
compare y, height-f
break-if-float>=
var imgy-f/xmm5: float <- copy y
imgy-f <- divide yratio
var imgy/edx: int <- truncate imgy-f
var x/xmm7: float <- copy zero-f
{
compare img-x, *img-width-a
break-if->=
compare x, width-f
break-if-float>=
var imgx-f/xmm5: float <- copy x
imgx-f <- divide xratio
var imgx/ecx: int <- truncate imgx-f
var idx/eax: int <- copy imgy
{
var src-a/eax: (addr byte) <- index data, i
var src/eax: byte <- copy-byte *src-a
var color/eax: int <- nearest-grey src
pixel screen, x, y, color
compare idx, 0
break-if-<=
idx <- decrement
idx <- multiply img-width
}
x <- increment
i <- increment
img-x <- increment
idx <- add imgx
# error info in case we rounded wrong and 'index' will fail bounds-check
compare idx, len
{
break-if-<
set-cursor-position 0/screen, 0x20/x 0x20/y
draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, imgx, 3/fg 0/bg
draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, imgy, 4/fg 0/bg
draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, idx, 5/fg 0/bg
}
var src-a/eax: (addr byte) <- index img-data, idx
var src/eax: byte <- copy-byte *src-a
var color-int/eax: int <- nearest-grey src
var screenx/ecx: int <- convert x
screenx <- add xmin
var screeny/edx: int <- convert y
screeny <- add ymin
pixel screen, screenx, screeny, color-int
x <- add one-f
loop
}
y <- increment
y <- add one-f
loop
}
}