Skip to content

ScriptFu: Add byte support.

Richard Szibele requested to merge rszibele/gimp:master into master

In Short

This merge request adds a new type 'byte', and related functions to be able to read and write binary data in ScriptFu directly. This change is backwards compatible and does not break the existing string and character handling behavior of ScriptFu.

How Scriptfu Handles Binary Data Now

As of right now, ScriptFu can not read binary data from either file or string ports as characters, as all characters must be valid UTF-8 code points.

There are workarounds, but these are extremely hacky, such as loading binary data as an image and then reading the pixel data or having to change your binary data into the base64 encoding first and then reading it as chars and manually convert. I didn't want to go there and I'd rather solve the problem.

How Scriptfu Handles Binary Data After This Merge Request

After this MR you can use the functions read-byte, peek-byte, byte-ready? and write-byte to directly deal with bytes from and to file and string ports. Functions to convert and check for the byte type have also been added: byte->integer, integer->byte, byte?.

Note: No direct conversion between byte and character has been added. For bytes which hold single-byte chars you can do (integer->char (byte->integer b)), and for multi-byte chars you can write bytes into a string port to build your string.

Internal Changes

As I did not want to now have two completely separate ways to read and write data from ports, I changed ScriptFu internally to only read and write bytes and reworked the character functions to be implemented in terms of these byte functions. This means that there is only one place where data is read basic_inbyte and one place where data is written putbytes. inchar is implemented in terms of inbyte and putchars is implemented in terms of putbytes respectively.

Tests

I've added a new test "test9" which ensures that character and string handling still work as expected. I've also tested it with my own plugins and a few other ScriptFu plugins and I did not find any issues.

Merge request reports