B
This commit is contained in:
parent
e5e8880e41
commit
518da4a923
1019 changed files with 15877 additions and 0 deletions
2
Task/Bitmap-Write-a-PPM-file/0DESCRIPTION
Normal file
2
Task/Bitmap-Write-a-PPM-file/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images, write the image to a PPM file (binary P6 prefered). <BR>
|
||||
(Read [[wp:Netpbm_format|the definition of PPM file]] on Wikipedia.)
|
||||
4
Task/Bitmap-Write-a-PPM-file/1META.yaml
Normal file
4
Task/Bitmap-Write-a-PPM-file/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Input Output
|
||||
note: Raster graphics operations
|
||||
26
Task/Bitmap-Write-a-PPM-file/Ada/bitmap-write-a-ppm-file.ada
Normal file
26
Task/Bitmap-Write-a-PPM-file/Ada/bitmap-write-a-ppm-file.ada
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
with Ada.Characters.Latin_1;
|
||||
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
|
||||
|
||||
procedure Put_PPM (File : File_Type; Picture : Image) is
|
||||
use Ada.Characters.Latin_1;
|
||||
Size : constant String := Integer'Image (Picture'Length (2)) & Integer'Image (Picture'Length (1));
|
||||
Buffer : String (1..Picture'Length (2) * 3);
|
||||
Color : Pixel;
|
||||
Index : Positive;
|
||||
begin
|
||||
String'Write (Stream (File), "P6" & LF);
|
||||
String'Write (Stream (File), Size (2..Size'Last) & LF);
|
||||
String'Write (Stream (File), "255" & LF);
|
||||
for I in Picture'Range (1) loop
|
||||
Index := Buffer'First;
|
||||
for J in Picture'Range (2) loop
|
||||
Color := Picture (I, J);
|
||||
Buffer (Index) := Character'Val (Color.R);
|
||||
Buffer (Index + 1) := Character'Val (Color.G);
|
||||
Buffer (Index + 2) := Character'Val (Color.B);
|
||||
Index := Index + 3;
|
||||
end loop;
|
||||
String'Write (Stream (File), Buffer);
|
||||
end loop;
|
||||
Character'Write (Stream (File), LF);
|
||||
end Put_PPM;
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
cyan := color(0,255,255) ; r,g,b
|
||||
cyanppm := Bitmap(10, 10, cyan) ; width, height, background-color
|
||||
Bitmap_write_ppm3(cyanppm, "cyan.ppm")
|
||||
run, cyan.ppm
|
||||
return
|
||||
|
||||
#include bitmap_storage.ahk ; see basic bitmap storage task
|
||||
|
||||
Bitmap_write_ppm3(bitmap, filename)
|
||||
{
|
||||
file := FileOpen(filename, 0x11) ; utf-8, write
|
||||
file.seek(0,0) ; overwrite BOM created with fileopen()
|
||||
file.write("P3`n" ; `n = \n in ahk
|
||||
. bitmap.width . " " . bitmap.height . "`n"
|
||||
. "255`n")
|
||||
loop % bitmap.height
|
||||
{
|
||||
height := A_Index
|
||||
loop % bitmap.width
|
||||
{
|
||||
width := A_Index
|
||||
color := bitmap[height, width]
|
||||
file.Write(color.R . " ")
|
||||
file.Write(color.G . " ")
|
||||
file.Write(color.B . " ")
|
||||
}
|
||||
file.write("`n")
|
||||
}
|
||||
file.close()
|
||||
return 0
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
Width% = 200
|
||||
Height% = 200
|
||||
|
||||
VDU 23,22,Width%;Height%;8,16,16,128
|
||||
*display c:\lena
|
||||
|
||||
f% = OPENOUT("c:\lena.ppm")
|
||||
IF f%=0 ERROR 100, "Failed to open output file"
|
||||
BPUT #f%, "P6"
|
||||
BPUT #f%, "# Created using BBC BASIC"
|
||||
BPUT #f%, STR$(Width%) + " " +STR$(Height%)
|
||||
BPUT #f%, "255"
|
||||
|
||||
FOR y% = Height%-1 TO 0 STEP -1
|
||||
FOR x% = 0 TO Width%-1
|
||||
rgb% = FNgetpixel(x%,y%)
|
||||
BPUT #f%, rgb% >> 16
|
||||
BPUT #f%, (rgb% >> 8) AND &FF
|
||||
BPUT #f%, rgb% AND &FF
|
||||
NEXT
|
||||
NEXT y%
|
||||
CLOSE#f%
|
||||
|
||||
END
|
||||
|
||||
DEF FNgetpixel(x%,y%)
|
||||
LOCAL col%
|
||||
col% = TINT(x%*2,y%*2)
|
||||
SWAP ?^col%,?(^col%+2)
|
||||
= col%
|
||||
23
Task/Bitmap-Write-a-PPM-file/C/bitmap-write-a-ppm-file-1.c
Normal file
23
Task/Bitmap-Write-a-PPM-file/C/bitmap-write-a-ppm-file-1.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const int dimx = 800, dimy = 800;
|
||||
int i, j;
|
||||
FILE *fp = fopen("first.ppm", "wb"); /* b - binary mode */
|
||||
(void) fprintf(fp, "P6\n%d %d\n255\n", dimx, dimy);
|
||||
for (j = 0; j < dimy; ++j)
|
||||
{
|
||||
for (i = 0; i < dimx; ++i)
|
||||
{
|
||||
static unsigned char color[3];
|
||||
color[0] = i % 256; /* red */
|
||||
color[1] = j % 256; /* green */
|
||||
color[2] = (i * j) % 256; /* blue */
|
||||
(void) fwrite(color, 1, 3, fp);
|
||||
}
|
||||
}
|
||||
(void) fclose(fp);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
36
Task/Bitmap-Write-a-PPM-file/C/bitmap-write-a-ppm-file-2.c
Normal file
36
Task/Bitmap-Write-a-PPM-file/C/bitmap-write-a-ppm-file-2.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
const char *filename = "n.pgm";
|
||||
int x, y;
|
||||
/* size of the image */
|
||||
const int x_max = 100; /* width */
|
||||
const int y_max = 100; /* height */
|
||||
/* 2D array for colors (shades of gray) */
|
||||
unsigned char data[y_max][x_max];
|
||||
/* color component is coded from 0 to 255 ; it is 8 bit color file */
|
||||
const int MaxColorComponentValue = 255;
|
||||
FILE * fp;
|
||||
/* comment should start with # */
|
||||
const char *comment = "# this is my new binary pgm file";
|
||||
|
||||
/* fill the data array */
|
||||
for (y = 0; y < y_max; ++y) {
|
||||
for (x = 0; x < x_max; ++x) {
|
||||
data[y][x] = (x + y) & 255;
|
||||
}
|
||||
}
|
||||
|
||||
/* write the whole data array to ppm file in one step */
|
||||
/* create new file, give it a name and open it in binary mode */
|
||||
fp = fopen(filename, "wb");
|
||||
/* write header to the file */
|
||||
fprintf(fp, "P5\n %s\n %d\n %d\n %d\n", comment, x_max, y_max,
|
||||
MaxColorComponentValue);
|
||||
/* write image data bytes to the file */
|
||||
fwrite(data, sizeof(data), 1, fp);
|
||||
fclose(fp);
|
||||
printf("OK - file %s saved\n", filename);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
void output_ppm(FILE *fd, image img);
|
||||
10
Task/Bitmap-Write-a-PPM-file/C/bitmap-write-a-ppm-file-4.c
Normal file
10
Task/Bitmap-Write-a-PPM-file/C/bitmap-write-a-ppm-file-4.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include "imglib.h"
|
||||
|
||||
void output_ppm(FILE *fd, image img)
|
||||
{
|
||||
unsigned int n;
|
||||
(void) fprintf(fd, "P6\n%d %d\n255\n", img->width, img->height);
|
||||
n = img->width * img->height;
|
||||
(void) fwrite(img->buf, sizeof(pixel), n, fd);
|
||||
(void) fflush(fd);
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
: write-ppm { bmp fid -- }
|
||||
s" P6" fid write-line throw
|
||||
bmp bdim swap
|
||||
0 <# bl hold #s #> fid write-file throw
|
||||
0 <# #s #> fid write-line throw
|
||||
s" 255" fid write-line throw
|
||||
bmp bdata bmp bdim * pixels
|
||||
bounds do
|
||||
i 3 fid write-file throw
|
||||
pixel +loop ;
|
||||
|
||||
s" red.ppm" w/o create-file throw
|
||||
test over write-ppm
|
||||
close-file throw
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
module RCImageIO
|
||||
use RCImageBasic
|
||||
|
||||
implicit none
|
||||
|
||||
contains
|
||||
|
||||
subroutine output_ppm(u, img)
|
||||
integer, intent(in) :: u
|
||||
type(rgbimage), intent(in) :: img
|
||||
integer :: i, j
|
||||
|
||||
write(u, '(A2)') 'P6'
|
||||
write(u, '(I0,'' '',I0)') img%width, img%height
|
||||
write(u, '(A)') '255'
|
||||
|
||||
do j=1, img%height
|
||||
do i=1, img%width
|
||||
write(u, '(3A1)', advance='no') achar(img%red(i,j)), achar(img%green(i,j)), &
|
||||
achar(img%blue(i,j))
|
||||
end do
|
||||
end do
|
||||
|
||||
end subroutine output_ppm
|
||||
|
||||
end module RCImageIO
|
||||
54
Task/Bitmap-Write-a-PPM-file/Go/bitmap-write-a-ppm-file-1.go
Normal file
54
Task/Bitmap-Write-a-PPM-file/Go/bitmap-write-a-ppm-file-1.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package raster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// WriteTo outputs 8-bit P6 PPM format to an io.Writer.
|
||||
func (b *Bitmap) WritePpmTo(w io.Writer) (err error) {
|
||||
// magic number
|
||||
if _, err = fmt.Fprintln(w, "P6"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// comments
|
||||
for _, c := range b.Comments {
|
||||
if _, err = fmt.Fprintln(w, c); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// x, y, depth
|
||||
_, err = fmt.Fprintf(w, "%d %d\n255\n", b.cols, b.rows)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// raster data in a single write
|
||||
b3 := make([]byte, 3*len(b.px))
|
||||
n1 := 0
|
||||
for _, px := range b.px {
|
||||
b3[n1] = px.R
|
||||
b3[n1+1] = px.G
|
||||
b3[n1+2] = px.B
|
||||
n1 += 3
|
||||
}
|
||||
if _, err = w.Write(b3); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// WriteFile writes to the specified filename.
|
||||
func (b *Bitmap) WritePpmFile(fn string) (err error) {
|
||||
var f *os.File
|
||||
if f, err = os.Create(fn); err != nil {
|
||||
return
|
||||
}
|
||||
if err = b.WritePpmTo(f); err != nil {
|
||||
return
|
||||
}
|
||||
return f.Close()
|
||||
}
|
||||
19
Task/Bitmap-Write-a-PPM-file/Go/bitmap-write-a-ppm-file-2.go
Normal file
19
Task/Bitmap-Write-a-PPM-file/Go/bitmap-write-a-ppm-file-2.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
// Files required to build supporting package raster are found in:
|
||||
// * This task (immediately above)
|
||||
// * Bitmap task
|
||||
|
||||
import (
|
||||
"raster"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := raster.NewBitmap(400, 300)
|
||||
b.FillRgb(0x240008) // a dark red
|
||||
err := b.WritePpmFile("write.ppm")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
|
||||
module Bitmap.Netpbm(readNetpbm, writeNetpbm) where
|
||||
|
||||
import Bitmap
|
||||
import Data.Char
|
||||
import System.IO
|
||||
import Control.Monad
|
||||
import Control.Monad.ST
|
||||
import Data.Array.ST
|
||||
|
||||
nil :: a
|
||||
nil = undefined
|
||||
|
||||
readNetpbm :: forall c. Color c => FilePath -> IO (Image RealWorld c)
|
||||
readNetpbm path = do
|
||||
let die = fail "readNetpbm: bad format"
|
||||
ppm <- readFile path
|
||||
let (s, rest) = splitAt 2 ppm
|
||||
unless (s == magicNumber) die
|
||||
let getNum :: String -> IO (Int, String)
|
||||
getNum ppm = do
|
||||
let (s, rest) = span isDigit $ skipBlanks ppm
|
||||
when (null s) die
|
||||
return (read s, rest)
|
||||
(width, rest) <- getNum rest
|
||||
(height, rest) <- getNum rest
|
||||
(_, c : rest) <-
|
||||
if getMaxval then getNum rest else return (nil, rest)
|
||||
unless (isSpace c) die
|
||||
i <- stToIO $ listImage width height $
|
||||
fromNetpbm $ map fromEnum rest
|
||||
return i
|
||||
where skipBlanks =
|
||||
dropWhile isSpace .
|
||||
until ((/= '#') . head) (tail . dropWhile (/= '\n')) .
|
||||
dropWhile isSpace
|
||||
magicNumber = netpbmMagicNumber (nil :: c)
|
||||
getMaxval = not $ null $ netpbmMaxval (nil :: c)
|
||||
|
||||
writeNetpbm :: forall c. Color c => FilePath -> Image RealWorld c -> IO ()
|
||||
writeNetpbm path i = withFile path WriteMode $ \h -> do
|
||||
(width, height) <- stToIO $ dimensions i
|
||||
let w = hPutStrLn h
|
||||
w $ magicNumber
|
||||
w $ show width ++ " " ++ show height
|
||||
unless (null maxval) (w maxval)
|
||||
stToIO (getPixels i) >>= hPutStr h . toNetpbm
|
||||
where magicNumber = netpbmMagicNumber (nil :: c)
|
||||
maxval = netpbmMaxval (nil :: c)
|
||||
106
Task/Bitmap-Write-a-PPM-file/Lua/bitmap-write-a-ppm-file.lua
Normal file
106
Task/Bitmap-Write-a-PPM-file/Lua/bitmap-write-a-ppm-file.lua
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
-- helper function, simulates PHP's array_fill function
|
||||
local array_fill = function(vbegin, vend, value)
|
||||
local t = {}
|
||||
for i=vbegin, vend do
|
||||
t[i] = value
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
Bitmap = {}
|
||||
Bitmap.__index = Bitmap
|
||||
|
||||
function Bitmap.new(width, height)
|
||||
local self = {}
|
||||
setmetatable(self, Bitmap)
|
||||
local white = array_fill(0, width, {255, 255, 255})
|
||||
self.data = array_fill(0, height, white)
|
||||
self.width = width
|
||||
self.height = height
|
||||
return self
|
||||
end
|
||||
|
||||
function Bitmap:writeRawPixel(file, c)
|
||||
local dt
|
||||
dt = string.format("%c", c)
|
||||
file:write(dt)
|
||||
end
|
||||
|
||||
function Bitmap:writeComment(fh, ...)
|
||||
local strings = {...}
|
||||
local str = ""
|
||||
local result
|
||||
for _, s in pairs(strings) do
|
||||
str = str .. tostring(s)
|
||||
end
|
||||
result = string.format("# %s\n", str)
|
||||
fh:write(result)
|
||||
end
|
||||
|
||||
function Bitmap:writeP6(filename)
|
||||
local fh = io.open(filename, 'w')
|
||||
if not fh then
|
||||
error(string.format("failed to open %q for writing", filename))
|
||||
else
|
||||
fh:write(string.format("P6 %d %d 255\n", self.width, self.height))
|
||||
self:writeComment(fh, "automatically generated at ", os.date())
|
||||
for _, row in pairs(self.data) do
|
||||
for _, pixel in pairs(row) do
|
||||
self:writeRawPixel(fh, pixel[1])
|
||||
self:writeRawPixel(fh, pixel[2])
|
||||
self:writeRawPixel(fh, pixel[3])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Bitmap:fill(x, y, width, heigth, color)
|
||||
width = (width == nil) and self.width or width
|
||||
height = (height == nil) and self.height or height
|
||||
width = width + x
|
||||
height = height + y
|
||||
for i=y, height do
|
||||
for j=x, width do
|
||||
self:setPixel(j, i, color)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Bitmap:setPixel(x, y, color)
|
||||
if x >= self.width then
|
||||
--error("x is bigger than self.width!")
|
||||
return false
|
||||
elseif x < 0 then
|
||||
--error("x is smaller than 0!")
|
||||
return false
|
||||
elseif y >= self.height then
|
||||
--error("y is bigger than self.height!")
|
||||
return false
|
||||
elseif y < 0 then
|
||||
--error("y is smaller than 0!")
|
||||
return false
|
||||
end
|
||||
self.data[y][x] = color
|
||||
return true
|
||||
end
|
||||
|
||||
function example_colorful_stripes()
|
||||
local w = 260*2
|
||||
local h = 260*2
|
||||
local b = Bitmap.new(w, h)
|
||||
--b:fill(2, 2, 18, 18, {240,240,240})
|
||||
b:setPixel(0, 15, {255,68,0})
|
||||
for i=1, w do
|
||||
for j=1, h do
|
||||
b:setPixel(i, j, {
|
||||
(i + j * 8) % 256,
|
||||
(j + (255 * i)) % 256,
|
||||
(i * j) % 256
|
||||
}
|
||||
);
|
||||
end
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
example_colorful_stripes():writeP6('p6.ppm')
|
||||
52
Task/Bitmap-Write-a-PPM-file/PHP/bitmap-write-a-ppm-file.php
Normal file
52
Task/Bitmap-Write-a-PPM-file/PHP/bitmap-write-a-ppm-file.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
class Bitmap {
|
||||
public $data;
|
||||
public $w;
|
||||
public $h;
|
||||
public function __construct($w = 16, $h = 16){
|
||||
$white = array_fill(0, $w, array(255,255,255));
|
||||
$this->data = array_fill(0, $h, $white);
|
||||
$this->w = $w;
|
||||
$this->h = $h;
|
||||
}
|
||||
//Fills a rectangle, or the whole image with black by default
|
||||
public function fill($x = 0, $y = 0, $w = null, $h = null, $color = array(0,0,0)){
|
||||
if (is_null($w)) $w = $this->w;
|
||||
if (is_null($h)) $h = $this->h;
|
||||
$w += $x;
|
||||
$h += $y;
|
||||
for ($i = $y; $i < $h; $i++){
|
||||
for ($j = $x; $j < $w; $j++){
|
||||
$this->setPixel($j, $i, $color);
|
||||
}
|
||||
}
|
||||
}
|
||||
public function setPixel($x, $y, $color = array(0,0,0)){
|
||||
if ($x >= $this->w) return false;
|
||||
if ($x < 0) return false;
|
||||
if ($y >= $this->h) return false;
|
||||
if ($y < 0) return false;
|
||||
$this->data[$y][$x] = $color;
|
||||
}
|
||||
public function getPixel($x, $y){
|
||||
return $this->data[$y][$x];
|
||||
}
|
||||
public function writeP6($filename){
|
||||
$fh = fopen($filename, 'w');
|
||||
if (!$fh) return false;
|
||||
fputs($fh, "P6 {$this->w} {$this->h} 255\n");
|
||||
foreach ($this->data as $row){
|
||||
foreach($row as $pixel){
|
||||
fputs($fh, pack('C', $pixel[0]));
|
||||
fputs($fh, pack('C', $pixel[1]));
|
||||
fputs($fh, pack('C', $pixel[2]));
|
||||
}
|
||||
}
|
||||
fclose($fh);
|
||||
}
|
||||
}
|
||||
|
||||
$b = new Bitmap(16,16);
|
||||
$b->fill();
|
||||
$b->fill(2, 2, 18, 18, array(240,240,240));
|
||||
$b->setPixel(0, 15, array(255,0,0));
|
||||
$b->writeP6('p6.ppm');
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#! /usr/bin/perl
|
||||
|
||||
use strict;
|
||||
use Image::Imlib2;
|
||||
|
||||
my $img = Image::Imlib2->new(100,100);
|
||||
$img->set_color(100,200,0, 255);
|
||||
$img->fill_rectangle(0,0,100,100);
|
||||
|
||||
$img->save("out0.ppm");
|
||||
$img->save("out0.jpg");
|
||||
$img->save("out0.png");
|
||||
|
||||
exit 0;
|
||||
|
|
@ -0,0 +1 @@
|
|||
$img->image_set_format("jpeg"); # or png, tiff, ppm ...
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(de ppmWrite (Ppm File)
|
||||
(out File
|
||||
(prinl "P6")
|
||||
(prinl (length (car Ppm)) " " (length Ppm))
|
||||
(prinl 255)
|
||||
(for Y Ppm (for X Y (apply wr X))) ) )
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
# String masquerading as ppm file (version P3)
|
||||
import io
|
||||
ppmfileout = io.StringIO('')
|
||||
|
||||
def writeppmp3(self, f):
|
||||
self.writeppm(f, ppmformat='P3')
|
||||
|
||||
def writeppm(self, f, ppmformat='P6'):
|
||||
assert ppmformat in ['P3', 'P6'], 'Format wrong'
|
||||
magic = ppmformat + '\n'
|
||||
comment = '# generated from Bitmap.writeppm\n'
|
||||
maxval = max(max(max(bit) for bit in row) for row in self.map)
|
||||
assert ppmformat == 'P3' or 0 <= maxval < 256, 'R,G,B must fit in a byte'
|
||||
if ppmformat == 'P6':
|
||||
fwrite = lambda s: f.write(bytes(s, 'UTF-8'))
|
||||
maxval = 255
|
||||
else:
|
||||
fwrite = f.write
|
||||
numsize=len(str(maxval))
|
||||
fwrite(magic)
|
||||
fwrite(comment)
|
||||
fwrite('%i %i\n%i\n' % (self.width, self.height, maxval))
|
||||
for h in range(self.height-1, -1, -1):
|
||||
for w in range(self.width):
|
||||
r, g, b = self.get(w, h)
|
||||
if ppmformat == 'P3':
|
||||
fwrite(' %*i %*i %*i' % (numsize, r, numsize, g, numsize, b))
|
||||
else:
|
||||
fwrite('%c%c%c' % (r, g, b))
|
||||
if ppmformat == 'P3':
|
||||
fwrite('\n')
|
||||
|
||||
Bitmap.writeppmp3 = writeppmp3
|
||||
Bitmap.writeppm = writeppm
|
||||
|
||||
# Draw something simple
|
||||
bitmap = Bitmap(4, 4, black)
|
||||
bitmap.fillrect(1, 0, 1, 2, white)
|
||||
bitmap.set(3, 3, Colour(127, 0, 63))
|
||||
# Write to the open 'file' handle
|
||||
bitmap.writeppmp3(ppmfileout)
|
||||
# Whats in the generated PPM file
|
||||
print(ppmfileout.getvalue())
|
||||
|
||||
'''
|
||||
The print statement above produces the following output :
|
||||
|
||||
P3
|
||||
# generated from Bitmap.writeppmp3
|
||||
4 4
|
||||
255
|
||||
0 0 0 0 0 0 0 0 0 127 0 63
|
||||
0 0 0 0 0 0 0 0 0 0 0 0
|
||||
0 0 0 255 255 255 0 0 0 0 0 0
|
||||
0 0 0 255 255 255 0 0 0 0 0 0
|
||||
|
||||
'''
|
||||
|
||||
# Write a P6 file
|
||||
ppmfileout = open('tmp.ppm', 'wb')
|
||||
bitmap.writeppm(ppmfileout)
|
||||
ppmfileout.close()
|
||||
6
Task/Bitmap-Write-a-PPM-file/R/bitmap-write-a-ppm-file.r
Normal file
6
Task/Bitmap-Write-a-PPM-file/R/bitmap-write-a-ppm-file.r
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# View the existing code in the library
|
||||
library(pixmap)
|
||||
pixmap::write.pnm
|
||||
|
||||
#Usage
|
||||
write.pnm(theimage, filename)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
/*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.*/
|
||||
20
Task/Bitmap-Write-a-PPM-file/Ruby/bitmap-write-a-ppm-file.rb
Normal file
20
Task/Bitmap-Write-a-PPM-file/Ruby/bitmap-write-a-ppm-file.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
class RGBColour
|
||||
def values
|
||||
[@red, @green, @blue]
|
||||
end
|
||||
end
|
||||
|
||||
class Pixmap
|
||||
def save(filename)
|
||||
File.open(filename, 'w') do |f|
|
||||
f.puts "P6", "#{@width} #{@height}", "255"
|
||||
f.binmode
|
||||
@height.times do |y|
|
||||
@width.times do |x|
|
||||
f.print @data[x][y].values.pack('C3')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
alias_method :write, :save
|
||||
end
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
object Pixmap {
|
||||
def save(bm:RgbBitmap, filename:String)={
|
||||
val out=new DataOutputStream(new FileOutputStream(filename))
|
||||
|
||||
out.writeBytes("P6\u000a%d %d\u000a%d\u000a".format(bm.width, bm.height, 255))
|
||||
|
||||
for(y <- 0 until bm.height; x <- 0 until bm.width; c=bm.getPixel(x, y)){
|
||||
out.writeByte(c.getRed)
|
||||
out.writeByte(c.getGreen)
|
||||
out.writeByte(c.getBlue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
(define (write-ppm image file)
|
||||
(define (write-image image)
|
||||
(define (write-row row)
|
||||
(define (write-colour colour)
|
||||
(if (not (null? colour))
|
||||
(begin (write-char (integer->char (car colour)))
|
||||
(write-colour (cdr colour)))))
|
||||
(if (not (null? row))
|
||||
(begin (write-colour (car row)) (write-row (cdr row)))))
|
||||
(if (not (null? image))
|
||||
(begin (write-row (car image)) (write-image (cdr image)))))
|
||||
(with-output-to-file file
|
||||
(lambda ()
|
||||
(begin (display "P6")
|
||||
(newline)
|
||||
(display (length (car image)))
|
||||
(display " ")
|
||||
(display (length image))
|
||||
(newline)
|
||||
(display 255)
|
||||
(newline)
|
||||
(write-image image)))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(define image (make-image 800 600))
|
||||
(image-fill! image *black*)
|
||||
(image-set! image 400 300 *blue*)
|
||||
(write-ppm image "out.ppm")
|
||||
19
Task/Bitmap-Write-a-PPM-file/Tcl/bitmap-write-a-ppm-file.tcl
Normal file
19
Task/Bitmap-Write-a-PPM-file/Tcl/bitmap-write-a-ppm-file.tcl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package require Tk
|
||||
|
||||
proc output_ppm {image filename} {
|
||||
$image write $filename -format ppm
|
||||
}
|
||||
|
||||
set img [newImage 150 150]
|
||||
fill $img red
|
||||
setPixel $img green 40 40
|
||||
output_ppm $img filename.ppm
|
||||
|
||||
# check the file format:
|
||||
set fh [open filename.ppm]
|
||||
puts [gets $fh] ;# ==> P6
|
||||
puts [gets $fh] ;# ==> 150 150
|
||||
puts [gets $fh] ;# ==> 255
|
||||
binary scan [read $fh 3] c3 pixel
|
||||
foreach colour $pixel {puts [expr {$colour & 0xff}]} ;# ==> 255 \n 0 \n 0 \n
|
||||
close $fh
|
||||
Loading…
Add table
Add a link
Reference in a new issue