nl-fast_png 0.0.0.pre.usegit

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 64d8014055d54b9e7940be354f9e63af3185212a13b1b3063d181068531730e0
4
+ data.tar.gz: 7004e22bbd82073df23f0b66776c410ef0675b62460769ffdd6d364d9a7a8a15
5
+ SHA512:
6
+ metadata.gz: 610ba7f0b0c7930a2808e6256221a26ea1355623cb32ddeae797c1c820ef5a82ced924f21b2f1d042bef5b1ac6d961dce3047cd4cc068bf12c8e96c89cb32d4e
7
+ data.tar.gz: bb52db8b67c2c2b32c2fd701f7c331d5055433839d5889665507ab78f6f23844fcd6d10845df65b77e839c129bcee16ff66cad7c19eef96f8e35a2212d08afd9
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ /ext/nl-fast_png/Makefile
11
+ /ext/**/*.o
12
+ /ext/**/*.so
13
+ /ext/**/mkmf.log
14
+ /lib/**/*.so
15
+
16
+ /.ruby-version
17
+ /.ruby-gemset
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nl-fast_png.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nl-fast_png (0.0.0.pre.usegit)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.3)
10
+ method_source (1.0.0)
11
+ pry (0.13.1)
12
+ coderay (~> 1.1)
13
+ method_source (~> 1.0)
14
+ rake (12.3.3)
15
+ rake-compiler (1.1.1)
16
+ rake
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ nl-fast_png!
23
+ pry
24
+ rake (~> 12.0)
25
+ rake-compiler
26
+
27
+ BUNDLED WITH
28
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011-2020, Mike Bourgeous
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
17
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,41 @@
1
+ # NL::FastPNG
2
+
3
+ NL::FastPng is a C-based extension that uses libpng in a simplified
4
+ configuration for faster performance on black and white images, at the expense
5
+ of slightly larger file sizes.
6
+
7
+ This code was used by the Nitrogen Logic Depth Camera Controller to generate
8
+ PNG images from realtime depth data on limited hardware.
9
+
10
+ ## License
11
+
12
+ NL::FastPng is licensed under a 2-clause BSD license. See the LICENSE file for
13
+ details. NL::FastPng is © 2011-2020, Mike Bourgeous.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'nl-fast_png'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle install
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install nl-fast_png
30
+
31
+ ## Usage
32
+
33
+ ```
34
+ require 'nl/fast_png'
35
+
36
+ # Store a 2x2 checkerboard pattern to /tmp/x.png
37
+ File.write('/tmp/x.png', NL::FastPng.store_png(2, 2, 8, "\xff\x00\x00\xff"))
38
+
39
+ # Store a 256x256 horizontal gradient to /tmp/grad.png
40
+ File.write('/tmp/grad.png', NL::FastPng.store_png(256, 256, 8, 256.times.map(&:chr).join * 256))
41
+ ```
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/extensiontask'
3
+
4
+ task :default => :spec
5
+
6
+ Rake::ExtensionTask.new 'png_ext' do |ext|
7
+ ext.name = 'png_ext'
8
+ ext.ext_dir = 'ext/nl-fast_png'
9
+ ext.lib_dir = 'lib/nl/fast_png'
10
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nl/fast_png"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ raise 'libpng not found; please install libpng-dev' unless have_library("png", "png_create_write_struct")
4
+
5
+ with_cflags("#{$CFLAGS} -O3 -Wall -Wextra #{ENV['EXTRACFLAGS']} -std=c99") do
6
+ create_makefile('nl/fast_png/png_ext')
7
+ end
@@ -0,0 +1,172 @@
1
+ /*
2
+ * Kinect data manipulation utilities for Ruby.
3
+ * (C)2011 Mike Bourgeous
4
+ *
5
+ * References:
6
+ * http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html
7
+ * http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
8
+ */
9
+ #include <ruby.h>
10
+ #include <ruby/thread.h>
11
+ #include <libpng/png.h>
12
+ #include <zlib.h>
13
+
14
+ // Information passed into store_png_blocking()
15
+ struct kinpng_info {
16
+ unsigned int w, h, d;
17
+ unsigned char *data;
18
+ png_structp png_ptr;
19
+ png_infop info_ptr;
20
+ };
21
+
22
+ // The PngExt Ruby module
23
+ VALUE PngExt = Qnil;
24
+
25
+ // Adds a libpng error message to the error string.
26
+ static void error_func(png_structp png, const char *msg)
27
+ {
28
+ VALUE *errstr = png_get_error_ptr(png);
29
+
30
+ rb_warning("%s\n", msg);
31
+
32
+ if(RSTRING_LEN(*errstr) > 0) {
33
+ rb_str_cat2(*errstr, " - ");
34
+ }
35
+ rb_str_cat2(*errstr, "Err: ");
36
+ rb_str_cat2(*errstr, msg);
37
+ }
38
+
39
+ // Adds a libpng warning message to the error string.
40
+ static void warn_func(png_structp png, const char *msg)
41
+ {
42
+ VALUE *errstr = png_get_error_ptr(png);
43
+
44
+ rb_warning("%s\n", msg);
45
+
46
+ if(RSTRING_LEN(*errstr) > 0) {
47
+ rb_str_cat2(*errstr, " - ");
48
+ }
49
+ rb_str_cat2(*errstr, "Warn: ");
50
+ rb_str_cat2(*errstr, msg);
51
+ }
52
+
53
+ // Appends PNG data to the output buffer.
54
+ static void write_func(png_structp png, png_bytep data, png_size_t length)
55
+ {
56
+ VALUE *outbuf = png_get_io_ptr(png);
57
+
58
+ // FIXME: Wrap with rb_protect(), re-acquire the GVL
59
+ rb_str_cat(*outbuf, data, length);
60
+ }
61
+
62
+ // Does nothing.
63
+ static void flush_func(png_structp png)
64
+ {
65
+ }
66
+
67
+ void *store_png_blocking(void *data)
68
+ {
69
+ struct kinpng_info *info = data;
70
+ png_bytep row_pointers[info->h];
71
+ unsigned int span = info->w * (info->d / 8);
72
+ int i;
73
+
74
+ if(setjmp(png_jmpbuf(info->png_ptr))) {
75
+ return NULL;
76
+ }
77
+
78
+ for(i = 0; i < info->h; i++, info->data += span) {
79
+ row_pointers[i] = info->data;
80
+ }
81
+ png_set_swap(info->png_ptr);
82
+ png_write_image(info->png_ptr, row_pointers);
83
+
84
+ png_write_end(info->png_ptr, info->info_ptr);
85
+
86
+ return data;
87
+ }
88
+
89
+ // Stores the given data as a grayscale image with depth 8 or 16, using
90
+ // compression settings optimized for speed (SUB filter, minimal zlib
91
+ // compression). 16-bit data is expected to be in little-endian order.
92
+ VALUE rb_store_png(VALUE self, VALUE width, VALUE height, VALUE depth, VALUE data)
93
+ {
94
+ VALUE outbuf;
95
+ VALUE errstr;
96
+ VALUE ret;
97
+
98
+ unsigned int w, h, d;
99
+ png_structp png_ptr;
100
+ png_infop info_ptr;
101
+
102
+ Check_Type(data, T_STRING);
103
+ w = NUM2UINT(width);
104
+ h = NUM2UINT(height);
105
+ d = NUM2UINT(depth);
106
+
107
+ if(d != 8 && d != 16) {
108
+ rb_raise(rb_eArgError, "Depth must be either 8 or 16 (got %u).", d);
109
+ }
110
+
111
+ if(RSTRING_LEN(data) < w * h * (d / 8)) {
112
+ rb_raise(rb_eArgError, "Data must contain at least %u bytes (got %zd).", w * h * (d / 8), (ssize_t)RSTRING_LEN(data));
113
+ }
114
+
115
+ outbuf = rb_str_buf_new(0);
116
+
117
+ if(!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, &errstr, error_func, warn_func))) {
118
+ rb_raise(rb_eStandardError, "Unable to create PNG writing structure: %s\n", RSTRING_PTR(errstr));
119
+ }
120
+
121
+ if(!(info_ptr = png_create_info_struct(png_ptr))) {
122
+ png_destroy_write_struct(&png_ptr, NULL);
123
+ rb_raise(rb_eStandardError, "Unable to create PNG info structure: %s\n", RSTRING_PTR(errstr));
124
+ }
125
+
126
+ if(setjmp(png_jmpbuf(png_ptr))) {
127
+ png_destroy_write_struct(&png_ptr, &info_ptr);
128
+ rb_raise(rb_eStandardError, "A libpng error occurred: %s\n", RSTRING_PTR(errstr));
129
+ }
130
+
131
+ png_set_write_fn(png_ptr, &outbuf, write_func, flush_func);
132
+
133
+ png_set_IHDR(png_ptr, info_ptr, w, h, d,
134
+ PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
135
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
136
+
137
+ png_set_filter(png_ptr, 0, PNG_FILTER_SUB);
138
+ png_set_compression_level(png_ptr, Z_BEST_SPEED);
139
+
140
+ png_write_info(png_ptr, info_ptr);
141
+
142
+ // FIXME: this is accessing a Ruby string's contents without the GVL; is that a problem?
143
+ ret = rb_thread_call_without_gvl(
144
+ store_png_blocking,
145
+ &(struct kinpng_info){
146
+ .w = w, .h = h, .d = d, .data = RSTRING_PTR(data), .png_ptr = png_ptr, .info_ptr = info_ptr
147
+ },
148
+ NULL,
149
+ NULL
150
+ );
151
+ if(ret == NULL) {
152
+ rb_raise(rb_eStandardError, "A libpng error occurred while writing: %s\n", RSTRING_PTR(errstr));
153
+ }
154
+
155
+ if(setjmp(png_jmpbuf(png_ptr))) {
156
+ rb_raise(rb_eStandardError, "A libpng error occurred: %s\n", RSTRING_PTR(errstr));
157
+ }
158
+
159
+ png_destroy_write_struct(&png_ptr, &info_ptr);
160
+
161
+ return outbuf;
162
+ }
163
+
164
+ // Initializes the kinpng module.
165
+ void Init_png_ext()
166
+ {
167
+ VALUE nl = rb_define_module("NL");
168
+ VALUE fast_png = rb_define_module_under(nl, "FastPng");
169
+ PngExt = rb_define_module_under(fast_png, "PngExt");
170
+
171
+ rb_define_module_function(PngExt, "store_png", rb_store_png, 4);
172
+ }
@@ -0,0 +1,17 @@
1
+ require "nl/fast_png/version"
2
+
3
+ module NL
4
+ module FastPng
5
+ # Returns a String containing a compressed PNG version of the given
6
+ # grayscale +data+, which has +width+ columns, +height+ lines, and has a
7
+ # bit depth of +depth+ (either 8 or 16).
8
+ #
9
+ # Example:
10
+ # File.write('/tmp/x.png', NL::FastPng.store_png(2, 2, 8, "\xff\x00\x00\xff"))
11
+ def self.store_png(width, height, depth, data)
12
+ PngExt.store_png(width, height, depth, data)
13
+ end
14
+ end
15
+ end
16
+
17
+ require_relative 'fast_png/png_ext'
@@ -0,0 +1,5 @@
1
+ module NL
2
+ module FastPng
3
+ VERSION = "0.0.0-usegit"
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/nl/fast_png/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "nl-fast_png"
5
+ spec.version = NL::FastPng::VERSION
6
+ spec.authors = ["Mike Bourgeous"]
7
+ spec.email = ["mike@mikebourgeous.com"]
8
+
9
+ spec.summary = %q{A fast libpng-based compressor for black and white images.}
10
+ spec.homepage = 'https://github.com/nitrogenlogic/nl-fast_png'
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ spec.metadata["source_code_uri"] = spec.homepage
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.extensions = [
26
+ 'ext/nl-fast_png/extconf.rb'
27
+ ]
28
+
29
+ spec.add_development_dependency 'rake-compiler'
30
+ spec.add_development_dependency 'pry'
31
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nl-fast_png
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.pre.usegit
5
+ platform: ruby
6
+ authors:
7
+ - Mike Bourgeous
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - mike@mikebourgeous.com
44
+ executables: []
45
+ extensions:
46
+ - ext/nl-fast_png/extconf.rb
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - ext/nl-fast_png/extconf.rb
58
+ - ext/nl-fast_png/png_ext.c
59
+ - lib/nl/fast_png.rb
60
+ - lib/nl/fast_png/version.rb
61
+ - nl-fast_png.gemspec
62
+ homepage: https://github.com/nitrogenlogic/nl-fast_png
63
+ licenses: []
64
+ metadata:
65
+ homepage_uri: https://github.com/nitrogenlogic/nl-fast_png
66
+ source_code_uri: https://github.com/nitrogenlogic/nl-fast_png
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.3.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">"
79
+ - !ruby/object:Gem::Version
80
+ version: 1.3.1
81
+ requirements: []
82
+ rubygems_version: 3.1.4
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A fast libpng-based compressor for black and white images.
86
+ test_files: []