From fe6f3598f0884e7d52120a41529ec8011e1bfe88 Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Tue, 2 Apr 2019 18:12:58 +0200 Subject: [PATCH 1/9] Gif animation prototype --- Gemfile | 2 +- Gemfile.lock | 14 ++++++++--- lib/gpx.rb | 70 +++++++++++++++++++++++++++++++++------------------- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/Gemfile b/Gemfile index ce37b7c44..5f040f95c 100644 --- a/Gemfile +++ b/Gemfile @@ -117,7 +117,7 @@ gem "canonical-rails" gem "logstasher" # Used to generate images for traces -gem "gd2-ffij" +gem "gd2-ffij", :git => 'git@github.com:mmd-osm/gd2-ffij.git', :branch => 'animated_gif' # Used for browser detection gem "browser" diff --git a/Gemfile.lock b/Gemfile.lock index 32496e129..ffa3cd937 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,11 @@ +GIT + remote: git@github.com:mmd-osm/gd2-ffij.git + revision: c92057a8f699a36b5f6d208db3d11eb3bae185dd + branch: animated_gif + specs: + gd2-ffij (0.3.1) + ffi (>= 1.0.0) + GEM remote: https://rubygems.org/ specs: @@ -173,8 +181,6 @@ GEM multipart-post (>= 1.2, < 3) ffi (1.10.0) fspath (3.1.0) - gd2-ffij (0.3.0) - ffi (>= 1.0.0) geoip (1.6.4) globalid (0.4.2) activesupport (>= 4.2.0) @@ -462,7 +468,7 @@ DEPENDENCIES factory_bot_rails fakefs faraday - gd2-ffij + gd2-ffij! geoip htmlentities http_accept_language (~> 2.0.0) @@ -512,4 +518,4 @@ DEPENDENCIES webmock BUNDLED WITH - 1.16.2 + 1.16.6 diff --git a/lib/gpx.rb b/lib/gpx.rb index 721e5608b..57b623362 100644 --- a/lib/gpx.rb +++ b/lib/gpx.rb @@ -48,46 +48,66 @@ module GPX end def picture(min_lat, min_lon, max_lat, max_lon, _num_points) - # frames = 10 + nframes = 10 width = 250 height = 250 + delay = 50 + + ptsper = _num_points / nframes; + proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height) - # TODO: create animated gif - # https://github.com/openstreetmap/openstreetmap-website/issues/281 - image = GD2::Image::IndexedColor.new(width, height) + frames = Array.new(nframes, GD2::Image::IndexedColor.new(width, height)) - black = image.palette.allocate(GD2::Color[0, 0, 0]) - white = image.palette.allocate(GD2::Color[255, 255, 255]) + (0..nframes - 1).each do |n| + frames[n] = GD2::Image::IndexedColor.new(width, height) + black = frames[n].palette.allocate(GD2::Color[0, 0, 0]) + white = frames[n].palette.allocate(GD2::Color[255, 255, 255]) + grey = frames[n].palette.allocate(GD2::Color[187, 187, 187]) - image.draw do |pen| - pen.color = white - pen.rectangle(0, 0, width, height, true) - end + frames[n].draw do |pen| + pen.color = white + pen.rectangle(0, 0, width, height, true) + end - image.draw do |pen| - pen.color = black - pen.anti_aliasing = true - pen.dont_blend = false + frames[n].draw do |pen| + pen.color = black + pen.anti_aliasing = true + pen.dont_blend = false - oldpx = 0.0 - oldpy = 0.0 + oldpx = 0.0 + oldpy = 0.0 - first = true + first = true - points do |p| - px = proj.x(p.longitude) - py = proj.y(p.latitude) + points.each_with_index do |p, pt| + px = proj.x(p.longitude) + py = proj.y(p.latitude) - pen.line(px, py, oldpx, oldpy) unless first + if ((pt >= (ptsper * n)) && (pt <= (ptsper * (n+1)))) + pen.thickness=(3) + pen.color = black + else + pen.thickness=(1) + pen.color = grey + end - first = false - oldpy = py - oldpx = px + pen.line(px, py, oldpx, oldpy) unless first + first = false + oldpy = py + oldpx = px + end end end - image.gif + res = GD2::AnimatedGif::gif_anim_begin(frames[0]) + res << GD2::AnimatedGif::gif_anim_add(frames[0], nil, delay) + (0..nframes - 1).each do |n| + res << GD2::AnimatedGif::gif_anim_add(frames[n], frames[n-1], delay) + end + res << GD2::AnimatedGif::gif_anim_end() + + res end def icon(min_lat, min_lon, max_lat, max_lon) From fe805836cc89efd9d053a15e2c1a78eef3f57582 Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Tue, 2 Apr 2019 18:28:49 +0200 Subject: [PATCH 2/9] Fix incorrect start index --- lib/gpx.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gpx.rb b/lib/gpx.rb index 57b623362..7c72c726f 100644 --- a/lib/gpx.rb +++ b/lib/gpx.rb @@ -102,7 +102,7 @@ module GPX res = GD2::AnimatedGif::gif_anim_begin(frames[0]) res << GD2::AnimatedGif::gif_anim_add(frames[0], nil, delay) - (0..nframes - 1).each do |n| + (1..nframes - 1).each do |n| res << GD2::AnimatedGif::gif_anim_add(frames[n], frames[n-1], delay) end res << GD2::AnimatedGif::gif_anim_end() From 1316ed8c1110ff25f9e09c57dd6a85dc4b97fb65 Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Tue, 2 Apr 2019 20:01:59 +0200 Subject: [PATCH 3/9] Remove unnessary init --- lib/gpx.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gpx.rb b/lib/gpx.rb index 7c72c726f..0299f63b5 100644 --- a/lib/gpx.rb +++ b/lib/gpx.rb @@ -57,7 +57,7 @@ module GPX proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height) - frames = Array.new(nframes, GD2::Image::IndexedColor.new(width, height)) + frames = [] (0..nframes - 1).each do |n| frames[n] = GD2::Image::IndexedColor.new(width, height) From ab6d657992f89d4578ef02047cd6b43ef4d1dac7 Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 3 Apr 2019 10:45:48 +0200 Subject: [PATCH 4/9] Fix rubocop warnings and rename a variable --- lib/gpx.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/gpx.rb b/lib/gpx.rb index 0299f63b5..4580421ba 100644 --- a/lib/gpx.rb +++ b/lib/gpx.rb @@ -47,19 +47,19 @@ module GPX end end - def picture(min_lat, min_lon, max_lat, max_lon, _num_points) + def picture(min_lat, min_lon, max_lat, max_lon, num_points) nframes = 10 width = 250 height = 250 delay = 50 - ptsper = _num_points / nframes; + points_per_frame = num_points / nframes proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height) frames = [] - (0..nframes - 1).each do |n| + (0...nframes).each do |n| frames[n] = GD2::Image::IndexedColor.new(width, height) black = frames[n].palette.allocate(GD2::Color[0, 0, 0]) white = frames[n].palette.allocate(GD2::Color[255, 255, 255]) @@ -84,28 +84,28 @@ module GPX px = proj.x(p.longitude) py = proj.y(p.latitude) - if ((pt >= (ptsper * n)) && (pt <= (ptsper * (n+1)))) - pen.thickness=(3) + if (pt >= (points_per_frame * n)) && (pt <= (points_per_frame * (n + 1))) + pen.thickness = 3 pen.color = black else - pen.thickness=(1) + pen.thickness = 1 pen.color = grey end pen.line(px, py, oldpx, oldpy) unless first - first = false - oldpy = py - oldpx = px + first = false + oldpy = py + oldpx = px end end end - res = GD2::AnimatedGif::gif_anim_begin(frames[0]) - res << GD2::AnimatedGif::gif_anim_add(frames[0], nil, delay) - (1..nframes - 1).each do |n| - res << GD2::AnimatedGif::gif_anim_add(frames[n], frames[n-1], delay) + res = GD2::AnimatedGif.gif_anim_begin(frames[0]) + res << GD2::AnimatedGif.gif_anim_add(frames[0], nil, delay) + (1...nframes).each do |n| + res << GD2::AnimatedGif.gif_anim_add(frames[n], frames[n - 1], delay) end - res << GD2::AnimatedGif::gif_anim_end() + res << GD2::AnimatedGif.gif_anim_end res end From 7f138fb014e201e9e6b63ababf57d0f7c53bd86c Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Wed, 3 Apr 2019 19:12:45 +0200 Subject: [PATCH 5/9] Skip gif animation optimization in case both frames are identical This works around an issue in libgd2 library which would otherwise cause segfaults due to zero sized images --- lib/gpx.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/gpx.rb b/lib/gpx.rb index 4580421ba..f3e0c0e26 100644 --- a/lib/gpx.rb +++ b/lib/gpx.rb @@ -103,7 +103,9 @@ module GPX res = GD2::AnimatedGif.gif_anim_begin(frames[0]) res << GD2::AnimatedGif.gif_anim_add(frames[0], nil, delay) (1...nframes).each do |n| - res << GD2::AnimatedGif.gif_anim_add(frames[n], frames[n - 1], delay) + res << GD2::AnimatedGif.gif_anim_add(frames[n], + (frames[n] == frames[n - 1] ? nil : frames[n - 1]), + delay) end res << GD2::AnimatedGif.gif_anim_end From 4c2fa6c44180c1885fd8d0dfbf0f58258aea618c Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 10 Apr 2019 11:57:25 +0200 Subject: [PATCH 6/9] Rubocop fixes --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 5f040f95c..154e53f84 100644 --- a/Gemfile +++ b/Gemfile @@ -117,7 +117,7 @@ gem "canonical-rails" gem "logstasher" # Used to generate images for traces -gem "gd2-ffij", :git => 'git@github.com:mmd-osm/gd2-ffij.git', :branch => 'animated_gif' +gem "gd2-ffij", :git => "git@github.com:mmd-osm/gd2-ffij.git", :branch => "animated_gif" # Used for browser detection gem "browser" From b88b699f49f1c0a58360276d00ba635f57cc48a3 Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 10 Apr 2019 17:43:27 +0200 Subject: [PATCH 7/9] Use https link for gem from github --- Gemfile | 2 +- Gemfile.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 154e53f84..604db1500 100644 --- a/Gemfile +++ b/Gemfile @@ -117,7 +117,7 @@ gem "canonical-rails" gem "logstasher" # Used to generate images for traces -gem "gd2-ffij", :git => "git@github.com:mmd-osm/gd2-ffij.git", :branch => "animated_gif" +gem "gd2-ffij", :git => "https://github.com/mmd-osm/gd2-ffij.git", :branch => "animated_gif" # Used for browser detection gem "browser" diff --git a/Gemfile.lock b/Gemfile.lock index ffa3cd937..73169aaea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,5 +1,5 @@ GIT - remote: git@github.com:mmd-osm/gd2-ffij.git + remote: https://github.com/mmd-osm/gd2-ffij.git revision: c92057a8f699a36b5f6d208db3d11eb3bae185dd branch: animated_gif specs: From a6ea75a2e25420e561ce669ca7c9bcee04ec6f2c Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 13 May 2019 20:17:01 +0100 Subject: [PATCH 8/9] Adjust to final animated GIF interface --- Gemfile | 2 +- Gemfile.lock | 14 ++++---------- lib/gpx.rb | 15 +++++++-------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/Gemfile b/Gemfile index 604db1500..26872a043 100644 --- a/Gemfile +++ b/Gemfile @@ -117,7 +117,7 @@ gem "canonical-rails" gem "logstasher" # Used to generate images for traces -gem "gd2-ffij", :git => "https://github.com/mmd-osm/gd2-ffij.git", :branch => "animated_gif" +gem "gd2-ffij", "= 0.4.0.dev" # Used for browser detection gem "browser" diff --git a/Gemfile.lock b/Gemfile.lock index 73169aaea..a7fcdf30e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,3 @@ -GIT - remote: https://github.com/mmd-osm/gd2-ffij.git - revision: c92057a8f699a36b5f6d208db3d11eb3bae185dd - branch: animated_gif - specs: - gd2-ffij (0.3.1) - ffi (>= 1.0.0) - GEM remote: https://rubygems.org/ specs: @@ -181,6 +173,8 @@ GEM multipart-post (>= 1.2, < 3) ffi (1.10.0) fspath (3.1.0) + gd2-ffij (0.4.0.dev) + ffi (>= 1.0.0) geoip (1.6.4) globalid (0.4.2) activesupport (>= 4.2.0) @@ -468,7 +462,7 @@ DEPENDENCIES factory_bot_rails fakefs faraday - gd2-ffij! + gd2-ffij (= 0.4.0.dev) geoip htmlentities http_accept_language (~> 2.0.0) @@ -518,4 +512,4 @@ DEPENDENCIES webmock BUNDLED WITH - 1.16.6 + 1.17.2 diff --git a/lib/gpx.rb b/lib/gpx.rb index f3e0c0e26..1b1c17ac7 100644 --- a/lib/gpx.rb +++ b/lib/gpx.rb @@ -100,16 +100,15 @@ module GPX end end - res = GD2::AnimatedGif.gif_anim_begin(frames[0]) - res << GD2::AnimatedGif.gif_anim_add(frames[0], nil, delay) - (1...nframes).each do |n| - res << GD2::AnimatedGif.gif_anim_add(frames[n], - (frames[n] == frames[n - 1] ? nil : frames[n - 1]), - delay) + image = GD2::AnimatedGif.new + frames.each do |frame| + image.add(frame, :delay => delay) end - res << GD2::AnimatedGif.gif_anim_end + image.end - res + output = StringIO.new + image.export(output) + output.read end def icon(min_lat, min_lon, max_lat, max_lon) From c17cd3a27994f4a0900ca2f292e9761c7098c684 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 10 Jun 2019 21:27:19 +0100 Subject: [PATCH 9/9] Update to released version 0.4.0 of gd2-ffij --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 26872a043..6c867193c 100644 --- a/Gemfile +++ b/Gemfile @@ -117,7 +117,7 @@ gem "canonical-rails" gem "logstasher" # Used to generate images for traces -gem "gd2-ffij", "= 0.4.0.dev" +gem "gd2-ffij", ">= 0.4.0" # Used for browser detection gem "browser" diff --git a/Gemfile.lock b/Gemfile.lock index a7fcdf30e..a696aaaa9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -173,7 +173,7 @@ GEM multipart-post (>= 1.2, < 3) ffi (1.10.0) fspath (3.1.0) - gd2-ffij (0.4.0.dev) + gd2-ffij (0.4.0) ffi (>= 1.0.0) geoip (1.6.4) globalid (0.4.2) @@ -462,7 +462,7 @@ DEPENDENCIES factory_bot_rails fakefs faraday - gd2-ffij (= 0.4.0.dev) + gd2-ffij (>= 0.4.0) geoip htmlentities http_accept_language (~> 2.0.0)