Skip to content

Commit 72ce2e0

Browse files
committed
Feature: Add to-base64 and from-base64
1 parent cbfc69b commit 72ce2e0

3 files changed

Lines changed: 97 additions & 2 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ will be imported as well.
4343
[`bitmap:copy-to-pcolors`](#bitmapcopy-to-pcolors)
4444
[`bitmap:difference-rgb`](#bitmapdifference-rgb)
4545
[`bitmap:export`](#bitmapexport)
46+
[`bitmap:from-base64`](#bitmapfrom-base64)
47+
[`bitmap:to-base64`](#bitmapto-base64)
4648
[`bitmap:from-view`](#bitmapfrom-view)
4749
[`bitmap:to-grayscale`](#bitmapto-grayscale)
4850
[`bitmap:height`](#bitmapheight)
@@ -127,6 +129,24 @@ bitmap:export image filename
127129
Writes *image* to *filename*.
128130

129131

132+
### `bitmap:from-base64`
133+
134+
```NetLogo
135+
bitmap:from-base64 base64-string
136+
```
137+
138+
Turns a base64 encoded string into a bitmap image for use by the extension. The [Fetch](https://github.com/NetLogo/Fetch-Extension) and [Export-The](https://github.com/NetLogo/ExportThe-Extension) extensions would be common sources of these encoded strings.
139+
140+
141+
### `bitmap:to-base64`
142+
143+
```NetLogo
144+
bitmap:to-base64 base64-string
145+
```
146+
147+
Turns a bitmap image into a base64 encoded string with a PNG format.
148+
149+
130150
### `bitmap:from-view`
131151

132152
```NetLogo

documentation.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ the same width and height as each other, or errors will ensue.
9494
arguments: [ { name: image, type: bitmap }, { name: filename, type: string } ],
9595
description: "Writes *image* to *filename*.",
9696
},
97+
{
98+
name: from-base64
99+
type: reporter
100+
arguments: [ { name: base64-string, type: string } ]
101+
returns: bitmap
102+
description: "Turns a base64 encoded string into a bitmap image for use by the extension. The [Fetch](https://github.com/NetLogo/Fetch-Extension) and [Export-The](https://github.com/NetLogo/ExportThe-Extension) extensions would be common sources of these encoded strings."
103+
},
104+
{
105+
name: to-base64
106+
type: reporter
107+
arguments: [ { name: base64-string, type: string } ]
108+
returns: string
109+
description: "Turns a bitmap image into a base64 encoded string with a PNG format."
110+
},
97111
{
98112
name: from-view,
99113
type: reporter,

src/BitmapExtension.java

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@
1616
import org.nlogo.nvm.Workspace;
1717

1818
import java.awt.image.BufferedImage;
19+
import java.io.ByteArrayInputStream;
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.util.Base64;
23+
import javax.imageio.ImageIO;
1924

2025
public class BitmapExtension extends DefaultClassManager {
2126

2227
public void load(PrimitiveManager primitiveManager) {
2328
primitiveManager.addPrimitive("import", new LoadImage());
2429
// saves to disk as PNG format
2530
primitiveManager.addPrimitive("export", new SaveImage());
31+
32+
primitiveManager.addPrimitive("from-base64", new FromBase64());
33+
primitiveManager.addPrimitive("to-base64", new ToBase64());
34+
2635
// returns a LogoBitmap image from NetLogo's primary view, as if by export-view
2736
primitiveManager.addPrimitive("from-view", new GrabView());
2837

@@ -73,7 +82,7 @@ public Object report(Argument args[], Context context)
7382
try {
7483
String path = fm.attachPrefix(args[0].getString());
7584
return new LogoBitmap(
76-
javax.imageio.ImageIO.read(fm.getFile(path).getInputStream()));
85+
ImageIO.read(fm.getFile(path).getInputStream()));
7786
} catch (java.io.IOException e) {
7887
throw new ExtensionException(e.getMessage());
7988
}
@@ -99,14 +108,66 @@ public void perform(Argument args[], Context context)
99108
String filename = context.attachCurrentDirectory(args[1].getString());
100109
java.io.FileOutputStream stream =
101110
new java.io.FileOutputStream(filename);
102-
javax.imageio.ImageIO.write(image, "png", stream);
111+
ImageIO.write(image, "png", stream);
103112
stream.close();
104113
} catch (java.io.IOException e) {
105114
throw new ExtensionException(e.getMessage());
106115
}
107116
}
108117
}
109118

119+
public static class FromBase64 implements Reporter {
120+
121+
public Syntax getSyntax() {
122+
return SyntaxJ.reporterSyntax(new int[] { Syntax.StringType() }, Syntax.WildcardType());
123+
}
124+
125+
public String getAgentClassString() {
126+
return "OTPL";
127+
}
128+
129+
public Object report(Argument args[], Context context)
130+
throws ExtensionException, LogoException {
131+
String base64 = args[0].getString();
132+
byte[] bytes = Base64.getDecoder().decode(base64);
133+
try {
134+
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
135+
return new LogoBitmap(image);
136+
}
137+
catch (final IOException ex) {
138+
throw new ExtensionException(ex);
139+
}
140+
}
141+
142+
}
143+
144+
public static class ToBase64 implements Reporter {
145+
146+
public Syntax getSyntax() {
147+
return SyntaxJ.reporterSyntax(new int[] { Syntax.WildcardType() }, Syntax.StringType());
148+
}
149+
150+
public String getAgentClassString() {
151+
return "OTPL";
152+
}
153+
154+
public Object report(Argument args[], Context context)
155+
throws ExtensionException, LogoException {
156+
BufferedImage image = getBitmapFromArgument(args[0]);
157+
158+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
159+
160+
try {
161+
ImageIO.write(image, "png", baos);
162+
return Base64.getEncoder().encodeToString(baos.toByteArray());
163+
}
164+
catch (final IOException ex) {
165+
throw new ExtensionException(ex);
166+
}
167+
}
168+
169+
}
170+
110171
public static class GrabView implements Reporter {
111172

112173
public Syntax getSyntax() {

0 commit comments

Comments
 (0)