Subject: 28.0.50; Exporting image data

From: Lars Ingebrigtsen <[email-masked]>
Date: Fri, 23 Oct 2020 13:18:40 +0200

There's been some discussion off-list about adding simple image
manipulation functions to Emacs, and there's really only three
transforms that are in scope for Emacs: Cropping, rotating and resizing.

Now, there are packages out there that allow doing this, but they rely
on external programs to actually do the image wrangling.  I wondered
what it would take to make it possible to do this in-Emacs, and I think
we basically have all the needed stuff -- we just need to hook it up and
make it available.

1) Making image data available to Emacs Lisp.

This is something that I've often wanted for other reasons -- I want to
know how an SVG was rendered, for instance (especially when doing
font-based stuff), and there's no way to actually tell.

So I think Emacs should have a function like

(export-image-data IMAGE &rest SPECS)

that will just take the image cache data for IMAGE (or the frame
specified by SPECS, if multi-frame) and dump the RGB(A) data into the
current buffer (which should be unibyte, for efficiency), and return
a plist of width/height/bytes-per-pixel/rgb(a) ordering.  This is
basically the PPM binary format, but without the heading.

This allows us to do easy transforms like cropping and rotating from
Lisp, too.

2) Create image files.

But mogrifying images is all well and good, but if we can't save the
data, it's not much use.  Of course we could just save the data as PPM,
but that's not very helpful.  So Emacs needs to be able to write image
files...  which sounds like a big thing, but it isn't really: As long as
we can save .jpeg and .png files, that's fine.

Fortunately, Emacs links with libpng and libjpeg already, so adding a

(encode-image-data TYPE)

function is less work than it would seem.

In GNU Emacs 28.0.50 (build 124, x86_64-pc-linux-gnu, GTK+ Version 3.24.23, cairo version 1.16.0)
 of 2020-10-22 built on xo
Repository revision: 954a4decfcc8e41084789516773b22d0adc11d91
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12008000
System Description: Debian GNU/Linux bullseye/sid

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no