Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
3
Task/Bitmap-PPM-conversion-through-a-pipe/00-META.yaml
Normal file
3
Task/Bitmap-PPM-conversion-through-a-pipe/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Bitmap/PPM_conversion_through_a_pipe
|
||||
note: Raster graphics operations
|
||||
3
Task/Bitmap-PPM-conversion-through-a-pipe/00-TASK.txt
Normal file
3
Task/Bitmap-PPM-conversion-through-a-pipe/00-TASK.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images, delegate writing a JPEG file through a '''pipe''' using the <tt>output_ppm</tt> function defined [[Write_ppm_file|on this other page]].
|
||||
|
||||
There are various utilities that can be used for this task, for example: '''cjpeg''' (package ''"jpeg-progs"'' on Linux, or ''"media-libs/libjpeg-turbo"'' on Gentoo), '''ppmtojpeg''' (package ''"netpbm"'' on Linux), '''convert''' (from ''ImageMagick'', multi-platform).
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
(* This program uses ImageMagick to convert an image, with the target
|
||||
file specified according to the conventions of ImageMagick. That
|
||||
allows such things as "gif:foobar.jpg" to mean a GIF named
|
||||
"foobar.jpg". But, if you leave out the "gif:" prefix, ImageMagick
|
||||
will make a JPEG. (I notice that one can also insert options to
|
||||
magick, although this was an unexpected result of my design.) *)
|
||||
|
||||
(*
|
||||
|
||||
##myatsccdef=\
|
||||
patscc -std=gnu2x -g -O2 -DATS_MEMALLOC_LIBC \
|
||||
-o $fname($1) $1 \
|
||||
bitmap{,_{read,write}_ppm}_task.{s,d}ats
|
||||
|
||||
*)
|
||||
|
||||
#include "share/atspre_staload.hats"
|
||||
|
||||
staload "bitmap_task.sats"
|
||||
staload "bitmap_read_ppm_task.sats"
|
||||
staload "bitmap_write_ppm_task.sats"
|
||||
|
||||
staload _ = "bitmap_task.dats"
|
||||
staload _ = "bitmap_read_ppm_task.dats"
|
||||
staload _ = "bitmap_write_ppm_task.dats"
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* There is support for pipe-I/O in libats/libc, but I cannot (at
|
||||
least when in a hurry) figure out how it is supposed to be
|
||||
used. So, as elsewhere in the "raster graphics operations"
|
||||
category, what is not in the prelude itself I implement with the
|
||||
foreign function interfaces. :) Using FFI is a typical part of ATS
|
||||
programming, and one should get used to doing it.
|
||||
|
||||
Anyway, here is some UNSAFE support for pipe-I/O. *)
|
||||
|
||||
typedef charstar = $extype"char *"
|
||||
typedef FILEstar = $extype"FILE *"
|
||||
|
||||
fn {}
|
||||
fileref_popen_unsafe (command : string,
|
||||
mode : string)
|
||||
: Option_vt FILEref =
|
||||
let
|
||||
val p = $extfcall (ptr, "popen", $UNSAFE.cast{charstar} command,
|
||||
$UNSAFE.cast{charstar} mode)
|
||||
in
|
||||
if iseqz p then
|
||||
None_vt ()
|
||||
else
|
||||
Some_vt ($UNSAFE.cast{FILEref} p)
|
||||
end
|
||||
|
||||
fn {}
|
||||
fileref_pclose_unsafe (f : FILEref)
|
||||
: int = (* Returns the exit status of the command. *)
|
||||
$extfcall (int, "pclose", $UNSAFE.cast{FILEstar} f)
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
|
||||
implement
|
||||
main0 (argc, argv) =
|
||||
let
|
||||
val args = listize_argc_argv (argc, argv)
|
||||
val nargs = length args
|
||||
|
||||
val inpf =
|
||||
if nargs < 2 then
|
||||
stdin_ref
|
||||
else if args[1] = "-" then
|
||||
stdin_ref
|
||||
else
|
||||
fileref_open_exn (args[1], file_mode_r)
|
||||
val pix_opt = pixmap_read_ppm<rgb24> inpf
|
||||
val () = fileref_close inpf
|
||||
in
|
||||
case+ pix_opt of
|
||||
| ~ None_vt () =>
|
||||
begin
|
||||
free args;
|
||||
println! ("For some reason, I failed to read the image.");
|
||||
exit 1
|
||||
end
|
||||
| ~ Some_vt @(pfgc1 | pix1) =>
|
||||
let
|
||||
val outf_name = if nargs < 3 then "-" else args[2]
|
||||
val command = string_append ("magick ppm:- ", outf_name)
|
||||
val () = free args
|
||||
val pipe_opt =
|
||||
(* Temporarily treating a strptr as a string, just to make a
|
||||
function call of this sort, is not actually unsafe. *)
|
||||
fileref_popen_unsafe ($UNSAFE.strptr2string command, "w")
|
||||
val () = free command
|
||||
in
|
||||
case+ pipe_opt of
|
||||
| ~ None_vt () =>
|
||||
begin
|
||||
free (pfgc1 | pix1);
|
||||
println! ("For some reason, I failed to open a pipe ",
|
||||
"to magick.");
|
||||
exit 3
|
||||
end
|
||||
| ~ Some_vt outf =>
|
||||
let
|
||||
val success = pixmap_write_ppm (outf, pix1)
|
||||
in
|
||||
ignoret (fileref_pclose_unsafe outf);
|
||||
free (pfgc1 | pix1);
|
||||
if ~success then
|
||||
begin
|
||||
println! ("For some reason, I failed to pipe the ",
|
||||
"image to magick.");
|
||||
exit 2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
/* interface */
|
||||
void print_jpg(image img, int qual);
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#define MAXCMDBUF 100
|
||||
void print_jpg(image img, int qual)
|
||||
{
|
||||
char buf[MAXCMDBUF];
|
||||
unsigned int n;
|
||||
FILE *pipe;
|
||||
|
||||
snprintf(buf, MAXCMDBUF, "convert ppm:- -quality %d jpg:-", qual);
|
||||
pipe = popen(buf, "w");
|
||||
if ( pipe != NULL )
|
||||
{
|
||||
fprintf(pipe, "P6\n%d %d\n255\n", img->width, img->height);
|
||||
n = img->width * img->height;
|
||||
fwrite(img->buf, sizeof(pixel), n, pipe);
|
||||
pclose(pipe);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#include "imglib.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
image img;
|
||||
|
||||
img = alloc_img(100,100);
|
||||
fill_img(img, 50, 20, 200);
|
||||
draw_line(img, 0, 0, 80, 80, 255, 0, 0);
|
||||
print_jpg(img, 75);
|
||||
free_img(img);
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
// Files required to build supporting package raster are found in:
|
||||
// * Bitmap
|
||||
// * Write a PPM file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os/exec"
|
||||
"raster"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := raster.NewBitmap(400, 300)
|
||||
// a little extravagant, this draws a design of dots and lines
|
||||
b.FillRgb(0xc08040)
|
||||
for i := 0; i < 2000; i++ {
|
||||
b.SetPxRgb(rand.Intn(400), rand.Intn(300), 0x804020)
|
||||
}
|
||||
for x := 0; x < 400; x++ {
|
||||
for y := 240; y < 245; y++ {
|
||||
b.SetPxRgb(x, y, 0x804020)
|
||||
}
|
||||
for y := 260; y < 265; y++ {
|
||||
b.SetPxRgb(x, y, 0x804020)
|
||||
}
|
||||
}
|
||||
for y := 0; y < 300; y++ {
|
||||
for x := 80; x < 85; x++ {
|
||||
b.SetPxRgb(x, y, 0x804020)
|
||||
}
|
||||
for x := 95; x < 100; x++ {
|
||||
b.SetPxRgb(x, y, 0x804020)
|
||||
}
|
||||
}
|
||||
|
||||
// pipe logic
|
||||
c := exec.Command("cjpeg", "-outfile", "pipeout.jpg")
|
||||
pipe, err := c.StdinPipe()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = c.Start()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = b.WritePpmTo(pipe)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = pipe.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
using Images, FileIO
|
||||
|
||||
ppmimg = load("data/bitmapInputTest.ppm")
|
||||
save("data/bitmapOutputTest.jpg", ppmimg)
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
// Version 1.2.40
|
||||
|
||||
import java.awt.Color
|
||||
import java.awt.Graphics
|
||||
import java.awt.image.BufferedImage
|
||||
|
||||
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 the object in PPM format to ImageMagick's STDIN via a pipe
|
||||
// so it can be converted to a .jpg file and written to disk
|
||||
val pb = ProcessBuilder("convert", "-", "output_piped.jpg")
|
||||
pb.directory(null)
|
||||
pb.redirectInput(ProcessBuilder.Redirect.PIPE)
|
||||
val buffer = ByteArray(width * 3) // write one line at a time
|
||||
val proc = pb.start()
|
||||
val pStdIn = proc.outputStream
|
||||
pStdIn.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
convert[image_,out_]:=Module[{process=StartProcess[{
|
||||
"wolfram","-noinit","-noprompt","-run",
|
||||
"Export[FromCharacterCode["~~ToString[ToCharacterCode[out]]~~"],ImportString[StringRiffle[Table[InputString[],{4}],FromCharacterCode[10]],FromCharacterCode[{80,80,77}]]]"
|
||||
}]},
|
||||
WriteLine[process,image];
|
||||
WriteLine[process,"Quit[]"];
|
||||
];
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import bitmap
|
||||
import ppm_write
|
||||
import osproc
|
||||
|
||||
# Build an image.
|
||||
var image = newImage(100, 50)
|
||||
image.fill(color(255, 0, 0))
|
||||
for row in 10..20:
|
||||
for col in 0..<image.w:
|
||||
image[col, row] = color(0, 255, 0)
|
||||
for row in 30..40:
|
||||
for col in 0..<image.w:
|
||||
image[col, row] = color(0, 0, 255)
|
||||
|
||||
# Launch ImageMagick "convert".
|
||||
# Input is taken from stdin and result written in "output1.jpeg".
|
||||
var p = startProcess("convert", args = ["ppm:-", "output1.jpeg"], options = {poUsePath})
|
||||
var stream = p.inputStream()
|
||||
image.writePPM(stream)
|
||||
p.close()
|
||||
|
||||
# Launch Netpbm "pnmtojpeg".
|
||||
# Input is taken from stdin and output sent to "output2.jpeg".
|
||||
p = startProcess("pnmtojpeg >output2.jpeg", options = {poUsePath, poEvalCommand})
|
||||
stream = p.inputStream()
|
||||
image.writePPM(stream)
|
||||
p.close()
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
let print_jpeg ~img ?(quality=96) () =
|
||||
let cmd = Printf.sprintf "cjpeg -quality %d" quality in
|
||||
(*
|
||||
let cmd = Printf.sprintf "ppmtojpeg -quality %d" quality in
|
||||
let cmd = Printf.sprintf "convert ppm:- -quality %d jpg:-" quality in
|
||||
*)
|
||||
let ic, oc = Unix.open_process cmd in
|
||||
output_ppm ~img ~oc;
|
||||
try
|
||||
while true do
|
||||
let c = input_char ic in
|
||||
print_char c
|
||||
done
|
||||
with End_of_file -> ()
|
||||
;;
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
# 20211224 Perl programming solution
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Imager;
|
||||
use Imager::Test 'test_image_raw';
|
||||
|
||||
my $img = test_image_raw();
|
||||
my $IO = Imager::io_new_bufchain();
|
||||
Imager::i_writeppm_wiol($img, $IO) or die;
|
||||
my $raw = Imager::io_slurp($IO) or die;
|
||||
|
||||
open my $fh, '|-', '/usr/local/bin/convert - -compress none output.jpg' or die;
|
||||
binmode $fh;
|
||||
syswrite $fh, $raw or die;
|
||||
close $fh;
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
(notonline)-->
|
||||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Bitmap_PPM_conversion_through_a_pipe.exw</span>
|
||||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o, system_exec(), pipes[!!]</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">pipeio</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #7060A8;">serialize</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">ppm</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- read_ppm()</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pipes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">pipes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">PIPEIN</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">create_pipe</span><span style="color: #0000FF;">(</span><span style="color: #000000;">INHERIT_WRITE</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Create the child process, with replacement stdin. </span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">cmd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%s viewppm -save test.jpg"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">get_interpreter</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">hProc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">system_exec</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cmd</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pipes</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">hPipe</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pipes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">PIPEIN</span><span style="color: #0000FF;">][</span><span style="color: #000000;">WRITE_PIPE</span><span style="color: #0000FF;">]</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">img</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">serialize</span><span style="color: #0000FF;">(</span><span style="color: #000000;">read_ppm</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Lena.ppm"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bFlat</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">write_to_pipe</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hPipe</span><span style="color: #0000FF;">,</span><span style="color: #000000;">img</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"error"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Close the pipe handle so the child process stops reading.
|
||||
--hPipe = close_handles(hPipe)</span>
|
||||
<span style="color: #000000;">pipes</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">close_handles</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipes</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (may as well do the lot)</span>
|
||||
|
||||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
|
||||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
# Create an empty image of 120 x 90 pixels
|
||||
(setq *Ppm (make (do 90 (link (need 120)))))
|
||||
|
||||
# Fill background with green color
|
||||
(ppmFill *Ppm 0 255 0)
|
||||
|
||||
# Draw a diagonal line
|
||||
(for I 80 (ppmSetPixel *Ppm I I 0 0 0))
|
||||
|
||||
# Write to "img.jpg" through a pipe
|
||||
(ppmWrite *Ppm '("convert" "-" "img.jpg"))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
"""
|
||||
Adapted from https://stackoverflow.com/questions/26937143/ppm-to-jpeg-jpg-conversion-for-python-3-4-1
|
||||
Requires pillow-5.3.0 with Python 3.7.1 32-bit on Windows.
|
||||
Sample ppm graphics files from http://www.cs.cornell.edu/courses/cs664/2003fa/images/
|
||||
"""
|
||||
|
||||
from PIL import Image
|
||||
|
||||
im = Image.open("boxes_1.ppm")
|
||||
im.save("boxes_1.jpg")
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(define (ppm->jpeg bitmap [jpg-file "output"] [quality 75])
|
||||
(define command (format "convert ppm:- -quality ~a jpg:~a.jpg" quality jpg-file))
|
||||
(match-define (list in out pid err ctrl) (process command))
|
||||
(bitmap->ppm bitmap out)
|
||||
(close-input-port in)
|
||||
(close-output-port out))
|
||||
|
||||
(ppm->jpeg bm)
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
# Reference:
|
||||
# https://rosettacode.org/wiki/Bitmap/Write_a_PPM_file#Raku
|
||||
|
||||
use v6;
|
||||
|
||||
class Pixel { has uint8 ($.R, $.G, $.B) }
|
||||
class Bitmap {
|
||||
has UInt ($.width, $.height);
|
||||
has Pixel @!data;
|
||||
|
||||
method fill(Pixel $p) {
|
||||
@!data = $p.clone xx ($!width*$!height)
|
||||
}
|
||||
method pixel(
|
||||
$i where ^$!width,
|
||||
$j where ^$!height
|
||||
--> Pixel
|
||||
) is rw { @!data[$i*$!height + $j] }
|
||||
|
||||
method data { @!data }
|
||||
}
|
||||
|
||||
role PPM {
|
||||
method P6 returns Blob {
|
||||
"P6\n{self.width} {self.height}\n255\n".encode('ascii')
|
||||
~ Blob.new: flat map { .R, .G, .B }, self.data
|
||||
}
|
||||
}
|
||||
|
||||
my Bitmap $b = Bitmap.new(width => 125, height => 125) but PPM;
|
||||
for flat ^$b.height X ^$b.width -> $i, $j {
|
||||
$b.pixel($i, $j) = Pixel.new: :R($i*2), :G($j*2), :B(255-$i*2);
|
||||
}
|
||||
|
||||
my $proc = run '/usr/bin/convert','-','output_piped.jpg', :in;
|
||||
$proc.in.write: $b.P6;
|
||||
$proc.in.close;
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
class Pixmap
|
||||
PIXMAP_FORMATS = ["P3", "P6"] # implemented output formats
|
||||
PIXMAP_BINARY_FORMATS = ["P6"] # implemented output formats which are binary
|
||||
|
||||
def write_ppm(ios, format="P6")
|
||||
if not PIXMAP_FORMATS.include?(format)
|
||||
raise NotImplementedError, "pixmap format #{format} has not been implemented"
|
||||
end
|
||||
ios.puts format, "#{@width} #{@height}", "255"
|
||||
ios.binmode if PIXMAP_BINARY_FORMATS.include?(format)
|
||||
@height.times do |y|
|
||||
@width.times do |x|
|
||||
case format
|
||||
when "P3" then ios.print @data[x][y].values.join(" "),"\n"
|
||||
when "P6" then ios.print @data[x][y].values.pack('C3')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def save(filename, opts={:format=>"P6"})
|
||||
File.open(filename, 'w') do |f|
|
||||
write_ppm(f, opts[:format])
|
||||
end
|
||||
end
|
||||
|
||||
def print(opts={:format=>"P6"})
|
||||
write_ppm($stdout, opts[:format])
|
||||
end
|
||||
|
||||
def save_as_jpeg(filename, quality=75)
|
||||
pipe = IO.popen("convert ppm:- -quality #{quality} jpg:#{filename}", 'w')
|
||||
write_ppm(pipe)
|
||||
pipe.close
|
||||
end
|
||||
end
|
||||
|
||||
image = Pixmap.open('file.ppm')
|
||||
image.save_as_jpeg('file.jpg')
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
val useOSConvert = fn ppm =>
|
||||
let
|
||||
val img = String.translate (fn #"\"" => "\\\""|n=>str n ) ppm ;
|
||||
val app = " convert - jpeg:- "
|
||||
val fname = "/tmp/fConv" ^ (String.extract (Time.toString (Posix.ProcEnv.time()),7,NONE) );
|
||||
val shellCommand = " echo \"" ^ img ^ "\" | " ^ app ;
|
||||
val me = ( Posix.FileSys.mkfifo
|
||||
(fname,
|
||||
Posix.FileSys.S.flags [ Posix.FileSys.S.irusr,Posix.FileSys.S.iwusr ]
|
||||
) ;
|
||||
Posix.Process.fork ()
|
||||
) ;
|
||||
in
|
||||
if (Option.isSome me) then
|
||||
let
|
||||
val fin =BinIO.openIn fname
|
||||
in
|
||||
( Posix.Process.sleep (Time.fromReal 0.1) ;
|
||||
BinIO.inputAll fin before
|
||||
(BinIO.closeIn fin ; OS.FileSys.remove fname )
|
||||
)
|
||||
end
|
||||
else
|
||||
( OS.Process.system ( shellCommand ^ " > " ^ fname ^ " 2>&1 " ) ;
|
||||
Word8Vector.fromList [] before OS.Process.exit OS.Process.success
|
||||
)
|
||||
end;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
useOSConvert "P3 3 2 255 255 0 0 0 255 0 0 0 255 255 255 0 255 255 255 0 0 0" ;
|
||||
val it =
|
||||
fromList[0wxFF, 0wxD8, 0wxFF, 0wxE0, 0wx0, 0wx10, 0wx4A, 0wx46, 0wx49,
|
||||
0wx46, ...]: BinIO.vector
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
val demo = fn () =>
|
||||
let
|
||||
val useOSConvert = fn ppmf =>
|
||||
let
|
||||
val appopt = ("/usr/local/bin/convert", ["convert","-", "/tmp/out.jpeg"])
|
||||
val p = Posix.IO.pipe () ;
|
||||
val me = Posix.Process.fork ()
|
||||
in
|
||||
case me of SOME cpd =>
|
||||
( Posix.IO.close (#outfd p);
|
||||
Posix.IO.dup2 {old=(#infd p), new= Posix.FileSys.stdin } ;
|
||||
Posix.IO.close (#infd p);
|
||||
Posix.Process.exec appopt
|
||||
)
|
||||
| _ =>
|
||||
( Posix.IO.close (#infd p);
|
||||
ppmf (#outfd p) ;
|
||||
Posix.IO.close (#outfd p) ;
|
||||
OS.Process.exit OS.Process.success
|
||||
)
|
||||
end;
|
||||
|
||||
fun output_ppm fd = (* placeholder for the ppm/bitmap functionality *)
|
||||
Posix.IO.writeVec ( fd ,
|
||||
Word8VectorSlice.full ( Byte.stringToBytes
|
||||
"P3 3 2 255 255 0 0 0 255 0 0 0 255 255 255 0 255 255 255 0 0 0 " )
|
||||
)
|
||||
|
||||
in
|
||||
|
||||
useOSConvert output_ppm
|
||||
|
||||
end ;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package require Tk
|
||||
|
||||
proc output_jpeg {image filename {quality 75}} {
|
||||
set f [open |[list convert ppm:- -quality $quality jpg:- > $filename] w]
|
||||
fconfigure $f -translation binary
|
||||
puts -nonewline $f [$image data -format ppm]
|
||||
close $f
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package require Tk
|
||||
package require img::jpeg
|
||||
|
||||
proc output_jpeg {image filename} {
|
||||
$image write $filename -format jpeg
|
||||
}
|
||||
set img [image create photo -filename filename.ppm]
|
||||
output_jpeg $img filename.jpg
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
/* gcc -O3 -std=c11 -shared -o pipeconv.so -fPIC -I./include pipeconv.c */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "dome.h"
|
||||
|
||||
static DOME_API_v0* core;
|
||||
static WREN_API_v0* wren;
|
||||
|
||||
static const char* source = ""
|
||||
"class PipeConv {\n"
|
||||
"foreign static convert(from, to) \n"
|
||||
"} \n";
|
||||
|
||||
void C_convert(WrenVM* vm) {
|
||||
const char *from = wren->getSlotString(vm, 1);
|
||||
const char *to = wren->getSlotString(vm, 2);
|
||||
char command[strlen(from) + strlen(to) + 10];
|
||||
strcpy(command, "convert ");
|
||||
strcat(command, from);
|
||||
strcat(command, " ");
|
||||
strcat(command, to);
|
||||
int res = system(command);
|
||||
}
|
||||
|
||||
DOME_EXPORT DOME_Result PLUGIN_onInit(DOME_getAPIFunction DOME_getAPI, DOME_Context ctx) {
|
||||
core = DOME_getAPI(API_DOME, DOME_API_VERSION);
|
||||
wren = DOME_getAPI(API_WREN, WREN_API_VERSION);
|
||||
core->registerModule(ctx, "pipeconv", source);
|
||||
core->registerClass(ctx, "pipeconv", "PipeConv", NULL, NULL);
|
||||
core->registerFn(ctx, "pipeconv", "static PipeConv.convert(_,_)", C_convert);
|
||||
return DOME_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
DOME_EXPORT DOME_Result PLUGIN_preUpdate(DOME_Context ctx) {
|
||||
return DOME_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
DOME_EXPORT DOME_Result PLUGIN_postUpdate(DOME_Context ctx) {
|
||||
return DOME_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
DOME_EXPORT DOME_Result PLUGIN_preDraw(DOME_Context ctx) {
|
||||
return DOME_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
DOME_EXPORT DOME_Result PLUGIN_postDraw(DOME_Context ctx) {
|
||||
return DOME_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
DOME_EXPORT DOME_Result PLUGIN_onShutdown(DOME_Context ctx) {
|
||||
return DOME_RESULT_SUCCESS;
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import "graphics" for Canvas, ImageData
|
||||
import "dome" for Window
|
||||
import "plugin" for Plugin
|
||||
|
||||
Plugin.load("pipeconv")
|
||||
|
||||
import "pipeconv" for PipeConv
|
||||
|
||||
class ConvertPPM {
|
||||
construct new(fileName, fileName2, width, height) {
|
||||
Window.resize(width, height)
|
||||
Canvas.resize(width, height)
|
||||
Window.title = "Convert PPM file via pipe"
|
||||
// convert .ppm file to .jpg via a pipe
|
||||
PipeConv.convert(fileName, fileName2)
|
||||
// load and display .jpg file
|
||||
var image = ImageData.loadFromFile(fileName2)
|
||||
image.draw(0, 0)
|
||||
}
|
||||
|
||||
init() {}
|
||||
|
||||
update() {}
|
||||
|
||||
draw(alpha) {}
|
||||
}
|
||||
|
||||
var Game = ConvertPPM.new("output.ppm", "output_piped.jpg", 350, 350)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
img:=PPM.readPPMFile("fractal.ppm");
|
||||
p:=System.popen(0'|convert ppm:- jpg:"fractal.jpg"|,"w");
|
||||
img.write(p); p.close();
|
||||
Loading…
Add table
Add a link
Reference in a new issue