From 27fe64103554f68b0ac34a99272317d16f4e7368 Mon Sep 17 00:00:00 2001 From: Lauren Budorick Date: Tue, 22 Aug 2017 14:55:45 -0700 Subject: [PATCH 1/2] Support adding SDF images at runtime --- src/symbol/sprite_atlas.js | 4 +- src/ui/map.js | 3 +- test/integration/image/dot.sdf.png | Bin 0 -> 1666 bytes .../image-add-1.5x-image-1x-screen/style.json | 4 +- .../image-add-1.5x-image-2x-screen/style.json | 4 +- .../image-add-2x-image-1x-screen/style.json | 4 +- .../image-add-2x-image-2x-screen/style.json | 4 +- .../image-add-nonsdf/expected.png | Bin 0 -> 510 bytes .../image-add-nonsdf/style.json | 44 ++++++++++++++++ .../image-add-sdf/expected.png | Bin 0 -> 359 bytes .../runtime-styling/image-add-sdf/style.json | 47 ++++++++++++++++++ test/suite_implementation.js | 5 +- 12 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 test/integration/image/dot.sdf.png create mode 100644 test/integration/render-tests/runtime-styling/image-add-nonsdf/expected.png create mode 100644 test/integration/render-tests/runtime-styling/image-add-nonsdf/style.json create mode 100644 test/integration/render-tests/runtime-styling/image-add-sdf/expected.png create mode 100644 test/integration/render-tests/runtime-styling/image-add-sdf/style.json diff --git a/src/symbol/sprite_atlas.js b/src/symbol/sprite_atlas.js index 4756c72860f..1c353f2679e 100644 --- a/src/symbol/sprite_atlas.js +++ b/src/symbol/sprite_atlas.js @@ -116,7 +116,7 @@ class SpriteAtlas extends Evented { return rect; } - addImage(name: string, pixels: HTMLImageElement | $ArrayBufferView, options: { width: number, height: number, pixelRatio?: number }) { + addImage(name: string, pixels: HTMLImageElement | $ArrayBufferView, options: { width: number, height: number, pixelRatio?: number, sdf?: boolean }) { let width, height, pixelRatio; if (pixels instanceof HTMLImageElement) { width = pixels.width; @@ -151,7 +151,7 @@ class SpriteAtlas extends Evented { width, height, pixelRatio, - sdf: false + sdf: options.sdf || false }; this.copy(pixels, width, rect, {x: 0, y: 0, width, height}, false); diff --git a/src/ui/map.js b/src/ui/map.js index 7b4a3d5337e..ef7346d3cad 100755 --- a/src/ui/map.js +++ b/src/ui/map.js @@ -1147,11 +1147,12 @@ class Map extends Camera { * @param {number} [options.width] The pixel width of the `ArrayBufferView` image * @param {number} [options.height] The pixel height of the `ArrayBufferView` image * @param {number} [options.pixelRatio] The ratio of pixels in the `ArrayBufferView` image to physical pixels on the screen + * @param {boolean} [options.sdf] Whether the image should be interpreted as an SDF image */ addImage( name: string, image: HTMLImageElement | $ArrayBufferView, - options?: {width: number, height: number, pixelRatio: number} + options?: {width: number, height: number, pixelRatio: number, sdf: boolean} ) { this.style.spriteAtlas.addImage(name, image, (options: any)); } diff --git a/test/integration/image/dot.sdf.png b/test/integration/image/dot.sdf.png new file mode 100644 index 0000000000000000000000000000000000000000..9935fcedb2e5c205e169876a69828830c7cdb052 GIT binary patch literal 1666 zcmb`HyG{Z@6ozLN7Z4E@yhJ-op1@WE0b(@9XklketTllrFtN~9D`R12=NtGOc9s^R zc)x4#|2CY>Vze+}$j{8q%=z|QW_D*`zLf4A?{&_lE9JtX`mB^o`1;>i_G{|=wb@d^ zHTfN#9`9);xmjMRI+xPcRMv9`hhv(I*D7O)_6by3NRY783MbN?3rQ^GN31^4Mq_V zKnt01v=N?p350@J*ddInvC)nMke?t;1zr(8;ZjZP$UD93;!lkS6A2F%S@@;dIt4JB zppXXp#peH01|ykNC91z@>|s9zp+{6T43h~e!=Xe}ExaZgTVv0)MAE6Cf#|~m03fLd z5P6~Tb_zoT7_fFF0c^CMv28nNyorr`s4V$*;yG6avqK9I7$O2}L(h1?$gY=xI4XDv z%x(ZL2EgSu4}pfpEOaxW28i6?1)B`UJRz|L3=npx4R)ys8s98DAh~xEAxB}Hc=BW3 z5pJC?hWg-Y(DO|r(OHR;-!E~s01-N2;u%HYMV7m^kE7;0*LJt}*Yo)a(%>q^`NGM} G>ee@$C$T{Q literal 0 HcmV?d00001 diff --git a/test/integration/render-tests/runtime-styling/image-add-1.5x-image-1x-screen/style.json b/test/integration/render-tests/runtime-styling/image-add-1.5x-image-1x-screen/style.json index 2d063abc471..e050778aa8c 100644 --- a/test/integration/render-tests/runtime-styling/image-add-1.5x-image-1x-screen/style.json +++ b/test/integration/render-tests/runtime-styling/image-add-1.5x-image-1x-screen/style.json @@ -10,7 +10,9 @@ "addImage", "marker", "./sprites/1.5x.png", - 1.5 + { + "pixelRatio": 1.5 + } ], [ "addLayer", diff --git a/test/integration/render-tests/runtime-styling/image-add-1.5x-image-2x-screen/style.json b/test/integration/render-tests/runtime-styling/image-add-1.5x-image-2x-screen/style.json index 5b974e9c878..c735dd6251d 100644 --- a/test/integration/render-tests/runtime-styling/image-add-1.5x-image-2x-screen/style.json +++ b/test/integration/render-tests/runtime-styling/image-add-1.5x-image-2x-screen/style.json @@ -10,7 +10,9 @@ "addImage", "marker", "./sprites/1.5x.png", - 1.5 + { + "pixelRatio": 1.5 + } ], [ "addLayer", diff --git a/test/integration/render-tests/runtime-styling/image-add-2x-image-1x-screen/style.json b/test/integration/render-tests/runtime-styling/image-add-2x-image-1x-screen/style.json index 472f3f52bd6..376a00c383a 100644 --- a/test/integration/render-tests/runtime-styling/image-add-2x-image-1x-screen/style.json +++ b/test/integration/render-tests/runtime-styling/image-add-2x-image-1x-screen/style.json @@ -10,7 +10,9 @@ "addImage", "marker", "./sprites/2x.png", - 2 + { + "pixelRatio": 2 + } ], [ "addLayer", diff --git a/test/integration/render-tests/runtime-styling/image-add-2x-image-2x-screen/style.json b/test/integration/render-tests/runtime-styling/image-add-2x-image-2x-screen/style.json index 8c84fa532bb..c9b8419a598 100644 --- a/test/integration/render-tests/runtime-styling/image-add-2x-image-2x-screen/style.json +++ b/test/integration/render-tests/runtime-styling/image-add-2x-image-2x-screen/style.json @@ -10,7 +10,9 @@ "addImage", "marker", "./sprites/2x.png", - 2 + { + "pixelRatio": 2 + } ], [ "addLayer", diff --git a/test/integration/render-tests/runtime-styling/image-add-nonsdf/expected.png b/test/integration/render-tests/runtime-styling/image-add-nonsdf/expected.png new file mode 100644 index 0000000000000000000000000000000000000000..8145cf2f10c4d08c5c7a1a31344bf85c4ac4fa60 GIT binary patch literal 510 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=Or9=|Ar*{or0Om)FfeWh@!lRh zJ871KNW;b3OP;Dfo*;1K$G?7YQ3;oWY^y)%RwYK7?!J@rY@YHliR2zb7;)zMLzl=# zDN(PSiAl`u>`dz2qIhc5~gOSy&ph96mtH!5AD-Q8h|2ZaS>h+`` z!eEgzU%P0zk3edi$lk~5T5k#+nq`YDbY4t6te9}DMcZYjxa<2HZlY2$0gm%Tfa)K% zF|vXLHMyqTSk(2zwNFa<-h@NSts34gfxlFoxqiB|iAXP**skIIq#G!7;BRZqqrL`K z#VEy7LV+R%kA=RzP|#1CWB8z}L6i~Xc+}RE`F>x|g%_(r}mJpnC#Nc!xgTe~DWM4f+oH&N literal 0 HcmV?d00001 diff --git a/test/integration/render-tests/runtime-styling/image-add-nonsdf/style.json b/test/integration/render-tests/runtime-styling/image-add-nonsdf/style.json new file mode 100644 index 00000000000..b74d23fc476 --- /dev/null +++ b/test/integration/render-tests/runtime-styling/image-add-nonsdf/style.json @@ -0,0 +1,44 @@ +{ + "version": 8, + "metadata": { + "test": { + "width": 64, + "height": 64, + "operations": [ + [ + "addImage", + "dot.sdf", + "./image/dot.sdf.png" + ], + [ + "addLayer", + { + "id": "geometry", + "type": "symbol", + "source": "geometry", + "layout": { + "icon-image": "dot.sdf" + }, + "paint": { + "icon-color": "red" + } + } + ], + [ + "wait" + ] + ] + } + }, + "sources": { + "geometry": { + "type": "geojson", + "data": { + "type": "Point", + "coordinates": [0, 0] + } + } + }, + "layers": [] +} + diff --git a/test/integration/render-tests/runtime-styling/image-add-sdf/expected.png b/test/integration/render-tests/runtime-styling/image-add-sdf/expected.png new file mode 100644 index 0000000000000000000000000000000000000000..bbc2b7ef5f82e66b88f43864858dc174404dd68e GIT binary patch literal 359 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=Or9=|Ar*{or0Om)Ffb~BcyAAG z)G8}vV1IDjQkKnC!q4pFF*W9;JZgCdVs|i4-d!=(Nb`Hbr?lWTC!R35EL?neo&K`- z|M}|9D=Q&^e;v0vwrt_n@iF{*$lE`k<(SB}GoS2!-_AL2`2E~(UUO!)^f_O@aIF&E za`2epaqBl{42%yRQJLJW^KR4ai@cIrE5c4#Ja(5^yZN2G)GX6$>vrsV9;Z0f$?WEx zq|^M%X5LHocbw`J7O`^W-=xl^ZElKF?bijJFug8c^W&&&x$ImgtyUh%dByM8{cYwJ zty&PJX!}=p`|364jNi`=kK?zrdFbM}wMnjW&x1t^XQpmjSo!Sdq|?lNY&>>9LoUn^ p%*>3wWqwbt;>8OWWbns4KE=^x)@N3gHNcQ#@O1TaS?83{1OQ+0mFNHf literal 0 HcmV?d00001 diff --git a/test/integration/render-tests/runtime-styling/image-add-sdf/style.json b/test/integration/render-tests/runtime-styling/image-add-sdf/style.json new file mode 100644 index 00000000000..4ed985c9754 --- /dev/null +++ b/test/integration/render-tests/runtime-styling/image-add-sdf/style.json @@ -0,0 +1,47 @@ +{ + "version": 8, + "metadata": { + "test": { + "width": 64, + "height": 64, + "operations": [ + [ + "addImage", + "dot.sdf", + "./image/dot.sdf.png", + { + "sdf": true + } + ], + [ + "addLayer", + { + "id": "geometry", + "type": "symbol", + "source": "geometry", + "layout": { + "icon-image": "dot.sdf" + }, + "paint": { + "icon-color": "red" + } + } + ], + [ + "wait" + ] + ] + } + }, + "sources": { + "geometry": { + "type": "geojson", + "data": { + "type": "Point", + "coordinates": [0, 0] + } + } + }, + "layers": [] +} + diff --git a/test/suite_implementation.js b/test/suite_implementation.js index e99a8633ae5..ed25ca02da9 100644 --- a/test/suite_implementation.js +++ b/test/suite_implementation.js @@ -6,6 +6,7 @@ const PNG = require('pngjs').PNG; const Map = require('../src/ui/map'); const window = require('../src/util/window'); const browser = require('../src/util/browser'); +const util = require('../src/util/util'); const rtlTextPlugin = require('../src/source/rtl_text_plugin'); const rtlText = require('@mapbox/mapbox-gl-rtl-text'); const fs = require('fs'); @@ -107,8 +108,8 @@ function applyOperations(map, operations, callback) { } else if (operation[0] === 'addImage') { const img = PNG.sync.read(fs.readFileSync(path.join(__dirname, './integration', operation[2]))); - const pixelRatio = operation.length > 3 ? operation[3] : 1; - map.addImage(operation[1], img.data, {height: img.height, width: img.width, pixelRatio: pixelRatio}); + const options = util.extend({}, {height: img.height, width: img.width, pixelRatio: 1}, operation.length > 3 ? operation[3] : {}); + map.addImage(operation[1], img.data, options); applyOperations(map, operations.slice(1), callback); } else { From 02a99b0b6536bf5e011045f75455295ef1f376af Mon Sep 17 00:00:00 2001 From: Lauren Budorick Date: Wed, 23 Aug 2017 11:54:50 -0700 Subject: [PATCH 2/2] Try alternate sdf image --- test/integration/image/dot.sdf.png | Bin 1666 -> 0 bytes test/integration/image/line.sdf.png | Bin 0 -> 816 bytes .../image-add-nonsdf/expected.png | Bin 510 -> 213 bytes .../runtime-styling/image-add-nonsdf/style.json | 9 +++------ .../runtime-styling/image-add-sdf/expected.png | Bin 359 -> 201 bytes .../runtime-styling/image-add-sdf/style.json | 6 +++--- 6 files changed, 6 insertions(+), 9 deletions(-) delete mode 100644 test/integration/image/dot.sdf.png create mode 100644 test/integration/image/line.sdf.png diff --git a/test/integration/image/dot.sdf.png b/test/integration/image/dot.sdf.png deleted file mode 100644 index 9935fcedb2e5c205e169876a69828830c7cdb052..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1666 zcmb`HyG{Z@6ozLN7Z4E@yhJ-op1@WE0b(@9XklketTllrFtN~9D`R12=NtGOc9s^R zc)x4#|2CY>Vze+}$j{8q%=z|QW_D*`zLf4A?{&_lE9JtX`mB^o`1;>i_G{|=wb@d^ zHTfN#9`9);xmjMRI+xPcRMv9`hhv(I*D7O)_6by3NRY783MbN?3rQ^GN31^4Mq_V zKnt01v=N?p350@J*ddInvC)nMke?t;1zr(8;ZjZP$UD93;!lkS6A2F%S@@;dIt4JB zppXXp#peH01|ykNC91z@>|s9zp+{6T43h~e!=Xe}ExaZgTVv0)MAE6Cf#|~m03fLd z5P6~Tb_zoT7_fFF0c^CMv28nNyorr`s4V$*;yG6avqK9I7$O2}L(h1?$gY=xI4XDv z%x(ZL2EgSu4}pfpEOaxW28i6?1)B`UJRz|L3=npx4R)ys8s98DAh~xEAxB}Hc=BW3 z5pJC?hWg-Y(DO|r(OHR;-!E~s01-N2;u%HYMV7m^kE7;0*LJt}*Yo)a(%>q^`NGM} G>ee@$C$T{Q diff --git a/test/integration/image/line.sdf.png b/test/integration/image/line.sdf.png new file mode 100644 index 0000000000000000000000000000000000000000..c227122996d310a27965c390772d2e4ee9dec157 GIT binary patch literal 816 zcmeAS@N?(olHy`uVBq!ia0vp^6(G#P1|%(0%q}r7Fn#xQaSW-5dwVU?TRBj~^`R=C z#v&yxiv{2Q{}1J}Xy{te@ra>lwzL(0?A;%eQsl!OCknN$y7jwe-kFDiR*gF^8^k4@+DF`-8nn*F(AESj!rG7sTrGO!>}e!~Fe# z?|!|j#YdZF-(gM=zrl8`acSeVdkyXn#CEXht@2zKQK0>G!}LAuIgF>7S@$!Q9|+a| zzGr%@Tj9~B=$*$81loUZ+}e0-UW508HS?D{Xzx4|xbZ>!fgd+d$^Es7yTf_mT-d7@ zRbMg>8qYbfpDS;F_VJ2^Vm60U|GmAaQ=v0;{x|)ogNae1f94+&c{6>D#pQPimU59&6CCa~XlbDf)c zV-3Uk1GWX1*YBVHpyz+-)u!k-zMt4OFrDH5@_};?1HXgj&c)x?#(e0xJ?qTl$PXz+ z^KQlJRAgQBe|AjFX6BsbW#ZiPqP|St`BB^?A!M1rg^4x5oWS7e>gTe~DWM4f?gng5 literal 0 HcmV?d00001 diff --git a/test/integration/render-tests/runtime-styling/image-add-nonsdf/expected.png b/test/integration/render-tests/runtime-styling/image-add-nonsdf/expected.png index 8145cf2f10c4d08c5c7a1a31344bf85c4ac4fa60..f32c41c447fd6573a96a16cfd1613d0820eb854a 100644 GIT binary patch literal 213 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=Or9=|Ar*{or0OmKd3_+>(}RYL zhYc8548L|>`4hO)$4JC^*<`zWXylVLjt^PZ!6Kw+GKon&lwUaPjt%r|OR<2pswGuU}kL!sQ^_>QA~=iIJwe z@8mq2r+iEzxyKMjoVotcC9+XU)GKFV5_3B{(>jTZwiEv!=w75dYlWNS(i0Cp9N-mZ z+oRZiTyW8u?nP|uCO006*D1KOwX^HZxTx6P{-9B8QT>VTMQTdj^TarQC5>h+wF;mfWe}}oViyPwrhM|w8EW_|6rG(s*y^BL6Sqe z^0^Gr)&wdqJlw)4>u0g3 zRik*(8i!sk)omvpOiXY+Aga7eJqZ!?K-P@bP0 Hl+XkK5~oYi delta 319 zcmX@f_?&5iVLhXQr;B6A+k+dm$_g3S9~`%oWpkDAGdp=qjd>}LTHb-!9n6z=S4=h1 z{GRYBEqKj|CrmC27av}yzwG^gzPj_uN=V>e$E}VnTex+648I=o_RnWICbI3!C%fOb zbIu!nKlhv0oS7|s&etzot3w-pyxVm9BCn*@im($FkKJX~ zZhj{(HOutcx*fZo$0<&AGP`*v=`{bcnfH?Y9j7{lMXX%;H>q=Jo15ZP`*lGlOs~t= z{5UFGE<4vrtCdG`UhzA2f19~Qs}@8l+WytuzIx3$9hqi9~+O|&yWi<1T!