aboutsummaryrefslogtreecommitdiffstats
path: root/liblss16.h
diff options
context:
space:
mode:
Diffstat (limited to 'liblss16.h')
-rw-r--r--liblss16.h150
1 files changed, 147 insertions, 3 deletions
diff --git a/liblss16.h b/liblss16.h
index cf51ec0..12d7e7a 100644
--- a/liblss16.h
+++ b/liblss16.h
@@ -104,6 +104,51 @@ enum liblss16_decode_error {
/**
+ * Image encoding state
+ */
+enum liblss16_encode_state {
+ /**
+ * Still encoding
+ */
+ LIBLSS16_ENCODE_RUNNING,
+
+ /**
+ * Encodig done
+ */
+ LIBLSS16_ENCODE_DONE,
+
+ /**
+ * Error occured while encoding
+ */
+ LIBLSS16_ENCODE_ERROR
+};
+
+
+/**
+ * Error values for image encoding
+ */
+enum liblss16_encode_error {
+ /**
+ * The image has zero width, zero height,
+ * or the height exceeds 32767
+ */
+ LIBLSS16_ENCODE_BAD_IMAGE_SIZE,
+
+ /**
+ * User specified that colour were encoded in
+ * 6 bits, however at least one colour value
+ * had at least one of it it high bits set
+ */
+ LIBLSS16_ENCODE_BAD_COLOUR,
+
+ /**
+ * Colour index above 15 specified for a pixel
+ */
+ LIBLSS16_ENCODE_BAD_COLOUR_INDEX
+};
+
+
+/**
* sRGB colour values, with transfer function applied,
* encoded in values in [0, 255]
*/
@@ -126,9 +171,9 @@ struct liblss16_colour {
/**
- * Decode state
+ * Image header
*/
-struct liblss16_decoder {
+struct liblss16_header {
/**
* The width of the raster
*/
@@ -143,7 +188,17 @@ struct liblss16_decoder {
* Image colour map
*/
struct liblss16_colour colour_map[16];
+};
+
+/**
+ * Decode state
+ */
+struct liblss16_decoder {
+ /**
+ * Image header
+ */
+ struct liblss16_header image;
/**
* For internal use
@@ -161,6 +216,23 @@ struct liblss16_decoder {
/**
+ * Encode state
+ *
+ * Should be considered opaque
+ */
+struct liblss16_encoder {
+ struct liblss16_header header;
+ uint16_t x_rem;
+ uint16_t y_rem;
+ uint16_t repetition;
+ uint8_t header_position;
+ uint8_t previous;
+ uint8_t nbuffered;
+ uint8_t buffered[6];
+};
+
+
+/**
* Initialise a decoder
*
* @param decoder The decoder
@@ -169,6 +241,8 @@ void liblss16_decoder_init(struct liblss16_decoder *decoder);
/**
+ * Decode an LSS16 file
+ *
* @param decoder Decoder state, must have been initialised with
* `liblss16_decoder_init` before the first call
* @param data Data to decode image from (may be partial)
@@ -179,7 +253,8 @@ void liblss16_decoder_init(struct liblss16_decoder *decoder);
* for the pixels in the image. They are returned
* primarily from to the top down and secondarily
* from left to right.
- * @param pixels_size The number of elements that may be stored in `pixels`
+ * @param pixels_size The number of elements that may be stored in `pixels`,
+ * must be at least 1
* @param npixels_out Output parameter for the number of elements stored
* in `pixels`. Always set.
* @param error_out Output parameter for the error; only set if
@@ -213,4 +288,73 @@ __attribute__((__const__))
const char *liblss16_decode_strerror(enum liblss16_decode_error error);
+/**
+ * Initialise an encoder
+ *
+ * @param encoder The encoder
+ * @param header Image information
+ * @param rgb6 If non-zero, colour values in `header->colour_map` are
+ * encoded in 6 bits (the limitation of the LSS16 format)
+ * rather than 8 bits
+ * @param error_out Output parameter for the error; only set when -1 is
+ * returned; may be `NULL`
+ * @return 0 on success, -1 on failure
+ */
+int liblss16_encoder_init(struct liblss16_encoder *encoder, const struct liblss16_header *header,
+ int rgb6, enum liblss16_encode_error *error_out);
+
+
+/**
+ * Encode an LSS16 file
+ *
+ * @param encoder Encoder state, must have been initialised with
+ * `liblss16_encoder_init` before the first call
+ * @param buffer Output buffer for the image data
+ * @param size Size of `buffer`
+ * @param written_out The number of bytes written to `size`, must be at least 1
+ * @param pixels Buffer with pixels to encode; the pixels are to preencoded
+ * (before the function is called) with the colour index of
+ * each pixel. Pixels are input primarily from to the top
+ * down and secondarily from left to right.
+ * @param npixels The number of provided pixels
+ * @param nconsumed_out Output parameter for the number of pixels consumed into
+ * to state of the encoder
+ * @param error_out Output parameter for the error; only set if
+ * `LIBLSS16_ENCODE_ERROR` is returned; may be `NULL`
+ * @return The processing state: normally `LIBLSS16_ENCODE_RUNNING`,
+ * but `LIBLSS16_ENCODE_DONE` when the image has been
+ * fully encoded, and `LIBLSS16_ENCODE_ERROR` on error
+ */
+enum liblss16_encode_state liblss16_encode_from_colour_index(
+ struct liblss16_encoder *encoder, void *buffer, size_t size,
+ size_t *written_out, const uint8_t *pixels, size_t npixels,
+ size_t *nconsumed_out, enum liblss16_encode_error *error_out);
+
+
+/**
+ * Get error description for an error code
+ *
+ * @param error The error code
+ * @return The error description
+ */
+#if defined(__GNUC__)
+__attribute__((__const__))
+#endif
+const char *liblss16_encode_strerror(enum liblss16_encode_error error);
+
+
+/**
+ * Optimise and image
+ *
+ * @param header The image metadata
+ * @param pixel Buffer with pixels; the pixels are to encoded with the
+ * colour index of each pixel. Pixels are encoded primarily
+ * from to the top down and secondarily from left to right.
+ *
+ * Both `pixels` and `header->colour_map` may be rewritten
+ * (to an equivalent image with at least as good compression with LSS16)
+ */
+void liblss16_optimise(struct liblss16_header *header, uint8_t *pixels);
+
+
#endif