+(defun sixel-image-sixels (sixel-image)
+ "Extract sixels from SIXEL-DATA."
+ (cdr sixel-image))
+
+(defun sixel-image-dims (sixel-image)
+ "Computes image width from SIXEL-DATA. Returns pair (width . height)."
+ (let ((sixels (sixel-image-sixels sixel-image)))
+ (cons
+ (apply #'max (mapcar (lambda (row) (length row)) sixels))
+ (* 6 (length sixels)))))
+
+(defun sixel-image-to-xpm-values (sixel-image)
+ "Produce string representing parameter values component of XPM
+representation of SIXEL-IMAGE."
+ (let* ((dims (sixel-image-dims sixel-image))
+ (colour-map (sixel-image-colour-map sixel-image))
+ (n-colours (1+ (length colour-map))))
+ (concat "\""
+ (number-to-string (car dims)) " "
+ (number-to-string (cdr dims)) " "
+ (number-to-string n-colours) " 2\"")))
+
+(defun sixel-image-to-xpm-colours (sixel-image)
+ "Produce string representing colour definitions component of XPM
+representation of SIXEL-IMAGE."
+ (let ((colour-map (sixel-image-colour-map sixel-image)))
+ (concat
+ (string-join
+ (mapcar (lambda (colour)
+ (concat
+ "\""
+ (elt colour 0) " "
+ "c #"
+ (format "%02x%02x%02x"
+ (elt colour 1)
+ (elt colour 2)
+ (elt colour 3))
+ "\""))
+ colour-map)
+ ",\n")
+ ",\n"
+ "\"-- c #000000\"")))
+
+(defun sixel-image-to-xpm-pixels (sixel-image)
+ "Produce string representating pixels component of XPM representation
+of SIXEL-IMAGE."
+ (concat
+ "\""
+ (string-join
+ (mapcar (lambda (sixel-row)
+ (string-join
+ (mapcar (lambda (i)
+ (string-join
+ (mapcar (lambda (sixel)
+ (let ((pixel (elt sixel i)))
+ (if pixel
+ pixel
+ "--")))
+ sixel-row)))
+ (number-sequence 0 5))
+ "\",\n\""))
+ (sixel-image-sixels sixel-image))
+ "\",\n\"")
+ "\""))
+
+(defun sixel-to-xpm (string)
+ "Returns an XPM image representation of the SIXEL graphic encoded in STRING."
+ (let* ((param-string (sixel-get-params string))
+ (data-string (sixel-get-data string))
+ (sixel-image (sixel-process-data data-string)))
+ (if (string-prefix-p "\eP" string)
+ (concat
+ "/* XPM */"
+ "static char * pixmap[] = {"
+ (sixel-image-to-xpm-values sixel-image) ",\n"
+ (sixel-image-to-xpm-colours sixel-image) ",\n"
+ (sixel-image-to-xpm-pixels sixel-image) "};")
+ (error "Incorrecly formatted sixel string."))))
+
+;; sixel.el ends here