pixel_raster 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ ![](Beispiel.svg)
2
+
3
+ # pixel-raster
4
+ Create a pixel raster representation of an image (for educational purposes).
5
+
6
+ ## Bits and pixels
7
+
8
+ A digital (pixel) image simply is a number of bits in a given format.
9
+ It encodes the size (width and height) of the image, i.e. how many
10
+ columns and rows of pixels the image consists of as well as the color
11
+ (and saturation and brightness) of each pixel.
12
+
13
+ The most easy case is a black and white image where each pixel is
14
+ represented by one bit.
15
+
16
+ ## A paint yourself binary pixel image
17
+
18
+ To explain how an image is represented as data it can help to show
19
+ paint-yourself raster versions (see [images.pdf](images.pdf)).
20
+
21
+ ## xpm2tikz
22
+
23
+ The script takes xpm images from the command line as input and
24
+ outputs [tikz](https://ctan.org/pkg/pgf)-code to stdout.
25
+
26
+ See `xpm2tikz -h`.
data/bin/xpm2tikz ADDED
@@ -0,0 +1,8 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'xpm2tikz'
5
+
6
+ include PixelRaster
7
+
8
+ command_line
@@ -0,0 +1,44 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ module PixelRaster
4
+ # read xpm files
5
+ # this is not very robust but assumes a straight forward file format
6
+ # without color/grayscale etc being mixed.
7
+ def read_xpm(filename)
8
+ lines = File.readlines(filename)
9
+ lines.shift # get rid of /* XPM */
10
+ lines.shift # get rid of static char * ...[] = {
11
+ meta = lines.shift.scan(/[0-9]+/)
12
+ n_x = meta[0].to_i
13
+ n_y = meta[1].to_i
14
+ n_of_colors = meta[2].to_i
15
+ n_of_chars = meta[3].to_i
16
+
17
+ colors = {}
18
+
19
+ n_of_colors.times do
20
+ lines.shift =~ /"(.{#{n_of_chars}})\s+([cgms]4?)\s+(.*?)"/
21
+ colors[$1] = $3
22
+ end
23
+
24
+ (1..n_y).collect do
25
+ lines.shift[1..n_x].scan(/.{#{n_of_chars}}/).collect { |c| colors[c] }
26
+ end
27
+ end
28
+
29
+ def xpm2tikz(filename, prefix="n")
30
+ xpm = read_xpm(filename)
31
+ "\\begin{tikzpicture}[yscale=-1]
32
+ \\draw[step=1,help lines] (0,0) grid (#{xpm.first.length},#{xpm.length});
33
+ " + xpm.each_with_index.collect { |row,r|
34
+ row.each_with_index.collect { |color,c|
35
+ cc = color=='#FFFFFF' ? '0' : '1'
36
+ "\\node[#{prefix+cc}] at (#{c+0.5},#{r+0.5}) {#{cc}};"
37
+ }.join("\n")
38
+ }.join("\n") + "
39
+ \\end{tikzpicture}
40
+ "
41
+ end
42
+ end
43
+
44
+
data/lib/xpm2tikz.rb ADDED
@@ -0,0 +1,45 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'pixel_raster'
5
+ require 'optparse'
6
+
7
+ module PixelRaster
8
+
9
+ def command_line
10
+ out = $stdout
11
+
12
+ OptionParser.new do |parser|
13
+ parser.banner = "\nConvert xpm image(s) to tikz. \n\nUsage: $0 [-O OUTFILE] INFILE [INFILE…]"
14
+
15
+ parser.on("-h", "--help", "Show this help message") do
16
+ puts parser
17
+ exit
18
+ end
19
+
20
+ parser.on('-O', '--output FILENAME',
21
+ 'write output to FILENAME (default: stdout)') do |filename|
22
+ begin
23
+ # We open the outfile here without ensuring it will be closed.
24
+ # This is ok as we need it throughout the whole script runtime
25
+ # and it will be closed on exit automatically.
26
+ out = File.open(filename, 'w')
27
+
28
+ # We could be more specific about catching errors, but we
29
+ # exit the script anyway, and this may be different on different OSes.
30
+ rescue StandardError => e
31
+ warn "Could not open “#{filename}” for writing."
32
+ warn e
33
+ exit e.errno
34
+ end
35
+ end
36
+ end.parse!
37
+
38
+ ARGV.each do |filename|
39
+ out << xpm2tikz(filename)
40
+ out << "\n\\vfill\n\n"
41
+ out << xpm2tikz(filename,'y')
42
+ out << "\n\\vfill\n\n"
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pixel_raster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Klaus Stein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Create a pixel raster representation of an image (for educational purposes).
14
+ email: ruby@istik.de
15
+ executables:
16
+ - xpm2tikz
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Beispiel.svg
21
+ - LICENSE
22
+ - README.md
23
+ - bin/xpm2tikz
24
+ - lib/pixel_raster.rb
25
+ - lib/xpm2tikz.rb
26
+ homepage: https://github.com/Lapizistik/pixel_raster
27
+ licenses:
28
+ - GPL-3.0
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.5.2.1
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Pixel image TikZ creator
50
+ test_files: []