How to resolve the algorithm Bitmap/Write a PPM file step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitmap/Write a PPM file step by step in the Wren programming language

Table of Contents

Problem Statement

Using the data storage type defined on this page for raster images, write the image to a PPM file (binary P6 preferred). (Read the definition of PPM file on Wikipedia.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitmap/Write a PPM file step by step in the Wren programming language

Source code in the wren programming language

import "graphics" for Canvas, ImageData, Color
import "dome" for Window, Process
import "io" for FileSystem
import "./str" for Strs

class Bitmap {
    construct new(name, width, height) {
        Window.title = name
        Window.resize(width, height)
        Canvas.resize(width, height)
        _bmp = ImageData.create(name, width, height)
        // create bitmap
        for (y in 0...height) {
            for (x in 0...width) {
                var c = Color.rgb(x % 256, y % 256, (x * y) % 256)
                pset(x, y, c)
            }
        }
        _w = width
        _h = height
    }

    init() {
        // write bitmap to a PPM file
        var ppm = ["P6\n%(_w) %(_h)\n255\n"]
        for (y in 0..._h) {
            for (x in 0..._w) {
                var c = pget(x, y)
                ppm.add(String.fromByte(c.r))
                ppm.add(String.fromByte(c.g))
                ppm.add(String.fromByte(c.b))
            }
        }
        FileSystem.save("output.ppm", Strs.concat(ppm))
        Process.exit(0)        
    }

    pset(x, y, col) { _bmp.pset(x, y, col) }

    pget(x, y) { _bmp.pget(x, y) }

    update() {}

    draw(alpha) {}
}

var Game = Bitmap.new("Bitmap - write to PPM  file", 320, 320)


  

You may also check:How to resolve the algorithm ISBN13 check digit step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Nim programming language
You may also check:How to resolve the algorithm Fairshare between two and more step by step in the Nim programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the MoonScript programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the Sidef programming language