Skip to content

Instantly share code, notes, and snippets.

@dockimbel
Last active June 13, 2025 19:57
Show Gist options
  • Save dockimbel/8db123c382d76014b2232f09acb82e73 to your computer and use it in GitHub Desktop.
Save dockimbel/8db123c382d76014b2232f09acb82e73 to your computer and use it in GitHub Desktop.
Codecs draft design

Codecs

Concept

Codecs are standardized components dedicated to the encoding and decoding of dataset formats accessed through IO or from memory. Codecs are indirectly called by LOAD and SAVE functions.

Mezz-level API

Encoding

encode <data> <type> /options <opts>

<data> : The data to encode (image! binary! string!)
<type> : Media type (jpeg, png, etc.) (word!)
<opts> : Special encoding options (block!)

Proposed implementation:

encode: function [
	"Encodes Red data into a series of bytes"
	data [image! binary! string!] "The data to encode"
	type [word!] "Media type (jpeg, png, etc.)"	
	/options opts [block!] "Special encoding options"	
][
	unless codec: select system/codecs type [
		 cause-error 'access 'no-codec type
	]
	codec/encode data make codec/options any [opts []]
]

Decoding

decode <data> <type> /options <opts>

<data> : The data to encode (image! binary! string!)
<type> : Media type (jpeg, png, etc.) (word!)
<opts> : Special encoding options (block!)

Proposed implementation:

decode: function [
	"Decodes a series of bytes into the related datatype"
	data [image! binary! string!] "The data to encode"
	type [word!] "Media type (jpeg, png, etc.)"	
	/options opts [block!] "Special encoding options"	
][
	unless codec: select system/codecs type [
		 cause-error 'access 'no-codec type
	]
	codec/decode data make codec/options any [opts []]
]

Standard object

system/standard/codec: object [
	title:			; [string!]		Codec's short description (80 chars max)
	name:			; [word!]		Codec's name
	mime-type:		; [path! block!]	Associated MIME type or list of types
	suffixes:		; [file! block!]	Associated file extension(s)
	options:		; [object!]		Default options
??	session:		; [object!]		Session values
	help:			; [block!]
	encode:			; [function! routine!]	Encoder function
	decode:			; [function! routine!]	Decoder function
]

Codecs Internals

Public API

encode: func [data [any-type!] opts [object!]]
decode: func [data [string! binary! file!] opts [object!]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment