June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,45 @@
|
|||
-module(ppm).
|
||||
|
||||
-export([ppm/1, write/2]).
|
||||
|
||||
-define(WHITESPACE, <<10>>).
|
||||
-define(SPACE, <<32>>).
|
||||
|
||||
% data structure introduced in task Bitmap (module ros_bitmap.erl)
|
||||
-record(bitmap, {
|
||||
pixels = nil,
|
||||
shape = {0, 0}
|
||||
}).
|
||||
|
||||
% create ppm image from bitmap record
|
||||
ppm(Bitmap) ->
|
||||
{Width, Height} = Bitmap#bitmap.shape,
|
||||
Pixels = ppm_pixels(Bitmap),
|
||||
Maxval = 255, % original ppm format maximum
|
||||
list_to_binary([
|
||||
header(), width_and_height(Width, Height), maxval(Maxval), Pixels]).
|
||||
|
||||
% write bitmap as ppm file
|
||||
write(Bitmap, Filename) ->
|
||||
Ppm = ppm(Bitmap),
|
||||
{ok, File} = file:open(Filename, [binary, write]),
|
||||
file:write(File, Ppm),
|
||||
file:close(File).
|
||||
|
||||
%%%%%%%%%%%% four parts of ppm file %%%%%%%%%%%%%%%%%%%%%%
|
||||
header() ->
|
||||
[<<"P6">>, ?WHITESPACE].
|
||||
|
||||
width_and_height(Width, Height) ->
|
||||
[encode_decimal(Width), ?SPACE, encode_decimal(Height), ?WHITESPACE].
|
||||
|
||||
maxval(Maxval) ->
|
||||
[encode_decimal(Maxval), ?WHITESPACE].
|
||||
|
||||
ppm_pixels(Bitmap) ->
|
||||
% 24 bit color depth
|
||||
array:to_list(Bitmap#bitmap.pixels).
|
||||
|
||||
%%%%%%%%%%%% Internals %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
encode_decimal(Number) ->
|
||||
integer_to_list(Number).
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
using Images, FileIO
|
||||
|
||||
h, w = 50, 70
|
||||
img = zeros(RGB{N0f8}, h, w)
|
||||
img[10:40, 5:35] = colorant"skyblue"
|
||||
for i in 26:50, j in (i-25):40
|
||||
img[i, j] = colorant"sienna1"
|
||||
end
|
||||
|
||||
save("data/bitmapWrite.ppm", img)
|
||||
save("data/bitmapWrite.png", img)
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
// Version 1.2.40
|
||||
|
||||
import java.awt.Color
|
||||
import java.awt.Graphics
|
||||
import java.awt.image.BufferedImage
|
||||
import java.io.FileOutputStream
|
||||
|
||||
class BasicBitmapStorage(width: Int, height: Int) {
|
||||
val image = BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR)
|
||||
|
||||
fun fill(c: Color) {
|
||||
val g = image.graphics
|
||||
g.color = c
|
||||
g.fillRect(0, 0, image.width, image.height)
|
||||
}
|
||||
|
||||
fun setPixel(x: Int, y: Int, c: Color) = image.setRGB(x, y, c.getRGB())
|
||||
|
||||
fun getPixel(x: Int, y: Int) = Color(image.getRGB(x, y))
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// create BasicBitmapStorage object
|
||||
val width = 640
|
||||
val height = 640
|
||||
val bbs = BasicBitmapStorage(width, height)
|
||||
for (y in 0 until height) {
|
||||
for (x in 0 until width) {
|
||||
val c = Color(x % 256, y % 256, (x * y) % 256)
|
||||
bbs.setPixel(x, y, c)
|
||||
}
|
||||
}
|
||||
|
||||
// now write it to a PPM file
|
||||
val fos = FileOutputStream("output.ppm")
|
||||
val buffer = ByteArray(width * 3) // write one line at a time
|
||||
fos.use {
|
||||
val header = "P6\n$width $height\n255\n".toByteArray()
|
||||
with (it) {
|
||||
write(header)
|
||||
for (y in 0 until height) {
|
||||
for (x in 0 until width) {
|
||||
val c = bbs.getPixel(x, y)
|
||||
buffer[x * 3] = c.red.toByte()
|
||||
buffer[x * 3 + 1] = c.green.toByte()
|
||||
buffer[x * 3 + 2] = c.blue.toByte()
|
||||
}
|
||||
write(buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,21 @@
|
|||
/*REXX program to write a PPM formatted image file, P6 (binary). */
|
||||
oFID = 'IMAGE.PPM' /*name of the output file. */
|
||||
green = '00 ff 00'x
|
||||
image. = green /*define all IMAGE RGB's to green*/
|
||||
width = 20 /*define the width of IMAGE. */
|
||||
height = 20 /* " " height " " */
|
||||
sep = '9'x
|
||||
call put 'P6'width||sep||height||sep||255||sep /*write header info.*/
|
||||
|
||||
do j =1 for width
|
||||
do k=1 for height
|
||||
call put image.j.k /*write IMAGE, 3 bytes at a time.*/
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────────subroutines──────────────────────*/
|
||||
put: call charout oFID,arg(1); return /*write out character(s) to file.*/
|
||||
/*REXX program writes a PPM formatted image file, also known as a P6 (binary) file. */
|
||||
green = 00ff00 /*define a pixel with the color green. */
|
||||
parse arg oFN width height color . /*obtain optional arguments from the CL*/
|
||||
if oFN=='' | oFN=="," then oFN='IMAGE' /*Not specified? Then use the default.*/
|
||||
if width=='' | width=="," then width= 20 /* " " " " " " */
|
||||
if height=='' | height=="," then height= 20 /* " " " " " " */
|
||||
if color=='' | color=="," then color= green /* " " " " " " */
|
||||
oFID= oFN'.PPM' /*define oFID by adding an extension.*/
|
||||
@. = x2c(color) /*set all pixels of image a hex color. */
|
||||
$ = '9'x /*define the separator (in the header).*/
|
||||
# = 255 /* " " max value for all colors. */
|
||||
call charout oFID, , 1 /*set the position of the file's output*/
|
||||
call charout oFID,'P6'width || $ || height || $ || # || $ /*write file header info. */
|
||||
_=
|
||||
do j =1 for width
|
||||
do k=1 for height; _=_ || @.j.k /*write the PPM file, 1 pixel at a time*/
|
||||
end /*k*/ /* ↑ a pixel contains three bytes, */
|
||||
end /*j*/ /* └────which defines the pixel's color*/
|
||||
call charout oFID, _ /*write the image's raster to the file.*/
|
||||
call charout oFID /*close the output file just to be safe*/
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue