Clean Up: a new sidebar deck for shrinking Impress presentations

Presentations get big. Someone drops a 12-megapixel photo onto a slide and scales it down to the size of a stamp. Someone else crops a screenshot to a quarter of its size, and the file still carries all of it. A presentation built from last year’s one brings along three master slides nobody uses. Then the file is too large to attach to an email.

Impress used to answer this with the Presentation Minimizer, a five-page wizard under the Tools menu. CODE 26.04.5.1 brings its replacement, a new sidebar deck called Clean Up, and the wizard itself is now removed from the code. Here is what the deck does, why I did not fix the old wizard instead, and what I learned building it.

The Clean Up deck after a scan

What was wrong with the wizard

I read the Minimizer’s code and its bug history before starting. Some of what I found:

  • It cleared the whole undo stack when it ran. Neither the cleanup nor anything you did before it could be undone.

  • Every option was one checkbox for the whole document. It never showed you what it was about to delete.

  • With its default settings it turned every embedded object into a picture. Charts are embedded objects in Impress, so after a cleanup your charts were no longer editable.

  • The “estimated size” was the current file size multiplied by two fixed factors. A presentation you had never saved showed 0.0 MB.

  • For almost three years it did not recompress fill and background images (tdf#169160). One reporter’s file went from 1.6 MB to about 1.2 MB, where it used to reach about 0.3 MB.

  • It was a modal desktop dialog. It never worked in Collabora Online at all.

Fixing a wizard that nobody in the browser could open was not worth it. I wanted a panel that lists every problem it finds, says what fixing each one would save, and changes nothing until you press a button.

What Clean Up does

Open it from Tools > Clean Up, or from the Review tab in the tabbed view. Press “Scan presentation” and the deck lists every check it runs, grouped under three headings:

  • Images: high-resolution images, meaning images stored with more detail than the slide can show, and cropped images.

  • Slides: hidden slides and unused master slides.

  • For sharing: speaker notes and embedded objects. The deck checks these two only when you tick “Prepare for sharing”, because you usually want your notes while you are still working.

Each check shows a count, “None” if the scan found nothing, or “Not checked” if the scan did not look. Each finding shows the slide it is on and how much a fix would save. A line at the bottom adds it all up. Click a finding to jump to its slide. Fix one finding, or press “Fix all”. Either way, one undo brings it all back.

The screenshot above is my demo presentation: ten slides, 15.7 MB. The scan found five images stored at between 325 and 1269 DPI, two hidden slides and two master slides that no slide uses, and it said “14.5 MB (97%) can be cleaned up”. I pressed “Fix all” and saved. The file on disk went from 15,687,869 bytes to 518,213 bytes.

The deck leaves vector graphics and animated images alone. Re-encoding an animated GIF would keep only its first frame. The row would promise a big saving, and the fix would replace your animation with a still picture.

How the pieces fit together

The scan runs in the document engine, next to the document. The panel runs in the browser, and like the rest of the sidebar it is built with the JSDialog framework. The two talk through one command.

How the Clean Up panel talks to the document engine

Every request from the browser is one JSON string sent with .uno:PresentationCleanup. The actions are list, scan, fix, fixAll, goTo, stop and release. Every answer comes back as a command result.

I looked at three other ways in before settling on a plain sd slot. A new method on the kit interface would have meant changes in seven places, across wsd, the kit and the engine. The getCommandValues path blocks the kit while it answers, and its reply does not say which request it belongs to. A slot on ViewShellBase needed no change to wsd or the kit, and its handler already has the view, the document and the sd library to hand.

The kit sends figures, not sentences. A row arrives as the slide number, which image on that slide it is, the resolution it is stored at, how much of it is cropped away and how many bytes it takes. The browser writes the sentence in the language of its own view. That took 16 translated strings out of the engine and made every message shorter.

The browser names a row by a number. The session numbers the rows of each list it sends, and a fix request carries that number and the number of the list. The session turns away a request for an older list or for a row that has gone. A late event from an old scan cannot change the current list either, because the browser checks the list number too.

Making it fast without threads

The slow part is working out what an image would save. The only honest way is to re-encode it at the target resolution and look at the size. My first version did that on a pool of worker threads, which brought its own problems with shutdown and memory.

Then I timed one 4000×3000 photo, scaled down to 1476×1107. Decoding the JPEG took 45 ms and encoding the result took 18 ms. Scaling took 2700 ms.

From the start I had scaled with BmpScaleFlag::BestQuality, which uses the same filter as the default setting of the Compress Image dialog. It gives a sharp result, but at the time it was slow, and the more the image shrank the slower it got. That is a fair price when you compress one picture by hand. It is too much for a figure the scan works out for every large image in a presentation.

BmpScaleFlag::Default did the same job in about a twentieth of the time, and the JPEG was no larger. But my workers could not safely use it, because Default runs on the same thread pool they ran on. A worker that waits for the pool to finish its job can end up waiting forever.

So the threads were hiding a cost that a different scaler removed, and they were also what kept me from switching to it.

The threads went, and three commits of support code in vcl went with them. Back on the main thread, the scan works in two steps:

  1. Walk the document and send the list at once, with no figures yet.

  1. Measure the images afterwards, one at a time, in an idle task at the lowest priority. Each slice stops as soon as the client has sent something.

The list shows up before a single image is encoded, and the savings fill in row by row. If you type in the document meanwhile, your keystrokes go first. On the demo presentation, with four photos between 3600×2400 and 4200×2800 pixels, the last figure arrived 0.26 s after the list. That was on my development build, which is a debug build without optimisation.

A cache keeps the measured figures, keyed by the image checksum and the settings, so a rescan does not encode anything twice. After review the cache moved from the view to the document. Three people scanning the same presentation no longer encode every image three times.

The JPEG quality setting turned out to matter much less than I expected. For a 1350×1350 image drawn at 8 cm, going from quality 95 to quality 30 changed the saving by about 5%. The downscale removes about 94% of the bytes on its own.

Back to BestQuality

That was not the end of the story. My colleague Tomaž Vajngerl rewrote the filter behind BestQuality. It now works on whole rows of pixels instead of one pixel at a time, splits the image into strips on the thread pool, and has AVX2 versions of its inner loops. When an image shrinks to a quarter of its size or less, it first averages blocks of pixels, so the filter has less to do.

So Clean Up went back to BestQuality. The scaled image is what the document keeps after a fix, so it should be the sharp one. A 4000×3000 photo now scales to 1500×1125 in about 105 ms, where Default takes 283 ms.

BestQuality now uses the thread pool too. That is fine, because the measurement runs on the main thread and is not one of the pool’s own tasks.

Cropped images

A crop in a presentation is only an attribute on the picture. The file still stores every pixel, including the part you cropped away. Nothing in the engine baked a crop into the image, so the first version skipped cropped pictures entirely.

Now a picture with at least 10% of its area cropped away gets a row of its own. The fix trims the bitmap to what is visible, removes the crop and re-encodes the result at the target resolution, all as one undo step. Two cases needed care. If two pictures share one bitmap with different crops, the deck leaves it alone. A mirrored picture has to be trimmed in the coordinates of the bitmap before mirroring, and it took a test of its own to get that right.

Design feedback

The first version opened on two image settings, a checkbox and a Scan button. After a scan it showed only the groups that found something. Ivana Koron from our design team pointed out that it was not obvious what the deck was for, and that you could not tell what it had checked.

The deck before the design review

So every check is now always on screen, with its state beside it. A short line at the top says what the deck is for. The settings moved into one Options row pinned to the bottom of the deck, which shows a summary of the current values when it is closed.

The deck after the design review

The next round was about words and colour:

  • “Cleanup” became “Clean Up”. The noun names an event. The verb names something you do.

  • The savings badge turned from green to yellow, because the saving has not happened yet.

  • “Images over 150 DPI” became “High-resolution images”.

  • Speaker notes get a “Remove” button. Everything else keeps “Fix”, because the embedded-object fix replaces the object with a picture rather than deleting it.

Goodbye, Minimizer

Clean Up shares no code with the Presentation Minimizer, so removing the wizard meant deleting the library, its six dialogs, its settings, icons, menu entry and command. The commit touches 68 files and removes 6,133 lines.

Try it

Open a presentation in Collabora Online, choose Tools > Clean Up and press “Scan presentation”. Clean Up first ships in CODE 26.04.5.1, the free development edition, which you can download from https://www.collaboraoffice.com/code/. The switch to BestQuality landed after that release was tagged, so 26.04.5.1 still scales with Default, and the sharper images come in the next release.

There is room for more checks. Each group in the deck is one entry in a table of tools, so an accessibility checker, for example, would be a new group rather than a new panel. If Clean Up misses something in one of your presentations, send it to me.

Thanks

Szymon Kłos reviewed every patch in the series, and the deck is better for each round of his comments. Ivana Koron reviewed the design and turned a panel that only made sense to me into one that explains itself. Tomaž Vajngerl made BestQuality fast enough to bring back, and reviewed the switch.