Sunday, 27 December 2009

Sudoku for (S)XEmacs

Once a night, there was no soft girlfriend at the side and I decided to play in some game. Tetris is to fast and active, sokoban is completed long ago and World of Goo heats CPU too much — so I choose sudoku. Opened websudoku.com and started playing. Everything is ok, but that is not under SXEmacs and not available offline. Decided to write sudoku.el to play sudoku under SXEmacs. Skim over the internet and found sudoku.el for (X)Emacs. Downloaded, started and disappointed, looked to the code and got depression. So night was spent rewriting sudoku.el to satisfy my needs, namely:

  • Handy, eye-candy and informative interface
  • Possibility to download sudokus from websudoku.com and menneske.no
  • Possibility to print sudokus
  • Possibility to edit sudokus
  • Possibility to save/load sudokus
  • Pencils for master techniques
  • Easy cells autocompletion, so you solve only hard part of the puzzle

Example

Let's run M-x sudoku RET:

sudoku-mode

Here is a bunch of easy cells, for example R5C5 (where cursor is) and of course we are to lazy to fill them, so let's press a, we will get:

autoinsert

Now is really hard part of the puzzle, but peculiarity for the value 4 is seen, so we get the pencil — p and use Nishio technique, starting from R8C9 cell:

pencil
  1. R8C9 -> 4 (Nishio start)
  2. R2C3 -> 4 (only cell in C3 where 4 can be)
  3. R3C7 -> 4 (only cell in R1 where 4 can be)
  4. No place for 4 in 8th square

So 4 can't be in R8C9. C-u p to cancel the pencil. Now we know that 4 is in R2C9:

won

Nice sudoku. I like it, my son will like it too, so let's print it with P. On the paper it will look like this:

printed

Download

Download from http://lgarc.narod.ru/xemacs/sudoku.el

Send notices, bugreports, fixes to lg AT sxemacs.org

Buttons generator

There is outstanging package in (S)XEmacs called xpm-button.el. It can generate button image on fly. It is very handy, but eye-ugly results depresses much. So I decided to write flexibly generator for eye-candy buttons, using Wand-mode. While implementing it, I also extended Wand-mode to get necessary functionality. Here is what I've got:

(wand-button 150 32 "Wand Button"
             :bg "cornflowerblue" :fg "white"
             :round 8.0 :depth 1 :yoff 8 ; :type 'pressed
             :wand-font (make-wand-font :family "Zapfino"))

button1

(wand-button 100 20 "Wand Button"
             :bg "gray80" :fg "black" :round 10.0
             :contrast 0.5 :depth 2 ; :type 'pressed
             :wand-font (make-wand-font :family "Copperplate"))

button2

(wand-button 100 40 (encode-coding-string "привет" 'utf-8)
             :bg "palegreen" :fg "green" :round 0.0
             :depth 4 :yoff -2 ; :type 'pressed
             :wand-font (make-wand-font :family "Arial Black"
                                        :size 20))

button2

Quite interesting to integrate this functionality into the SXEmacs widgets. Current implementation of buttons (using ugly static images) is far from ideal.

Wand-mode - new features

Wand-mode evolves. Some new features has been added since I've posted Wand-mode description. I'll describe them.

Filename in the buffer name

Let's open some image: C-x C-f lg-tree.jpg RET, we will get something like:

orig image

Take a look at mode-line and notice that there is filename contained in the buffer name. This is very handy when switching buffers with C-x C-b.

New operation: modulate

This picture is nice, but nature looks despondently, so let us colour the truth using next command o modulate RET saturation RET 60 RET. We will get:

sat inc 60 percents

60% increased saturation

This is much better.

Support for IPTC profile

We want to add some information related to the photo, what is this and where all this happens. Let's add IPTC tags using p caption RET lg climbs trees RET and p keywords RET js-kit, corporative RET. We'll get:

caption and keywords added

Caption and Keywords added

Pixel color picker

Wonder what is the color of my shorts, let's click on them:

pixel color picker

Pixel color picker

Pixel coordinates, it's color and RGB values are displayed. Very handy.

Global operations

Sometimes you need to view bunch of images applying certain operations stack, for example zoom-in. For this purpose there is command to fixate current operations, bound to O by default. Let's press O and follow up to next image with SPC.

global operations

Global operations applied

We see global operations, and also notice that those operations was automatically applied to the image. However increased saturation is not suitable for this photo, so we cancel it with undo (C-/):

operations canceled

Operations canceled

To reset global operations press C-u O.

Simple EXIF data (properties) editor

Sometimes you need to edit EXIF data. I don't use this functionality, IPTC profiles are enough for me, however friend of mine asked for it. It is not that hard for couple of beers. Start editor with e key. It will filter out image properties according to Wand-mode-properties-pattern ("^exif:" by default).

New operation: grayscale

There are many methods to make image be black&white. Simplest and greatest one is to zerofy the saturation with o modulate RET saturation RET -100 RET command. However o grayscale RET operation has been added for your convenience.

New operation: fit to size

If you need scale image to fit into given box, then use o fit to size RET operation. It will use Wand-mode-scaler procedure to perform scaling. You can create custom scalers with Wand-make-scaler macro. Pretty good scaler might look like:

(setq Wand-mode-scaler
      (Wand-make-scaler :GaussianFilter 1))

Take a look at docstrings for Wand-mode-scaler and Wand-make-scaler for exhaustive information.

Wand-mode - image viewer

Wand-mode is the special mode to view and edit images under SXEmacs. Wand-mode is based on ImageMagick API therefore it can perform action usually done by programs such as convert(1), display(1) etc. Wand-mode supports all the formats handled by ImageMagick; there are over 100 formats including PDF, EPS, etc.

Getting started with Wand-mode

Using M-x Wand-display RET /path/to/image.jpg RET we will get something like:

Wand-display sample

To get file info we press i:

Wand-display identify

Now let us enhance this image a little. We want to remove noise and decrease contrast a little. Use o despeckle RET and then o contrast RET decrease RET. Don't worry, completion will work everywhere. We'll get the result:

Wand-display edit

Viewing image directories

Wand-mode has simple yet very handy navigation possibilities to view images. Open any file with Wand-display, if there any other image files (supported by ImageMagick) in the directory, then you can open them using next keys:

  • Space - Next image
  • BackSpace - Previous image
  • M-< - Very first image in the directory (in alphabet order)
  • M-> - Last image in the directory

Pages navigation

When viewing multi-page images such as PDF, EPS, MPEG, etc you can navigate across the pages with next keystrokes:

  • PgDown - Next page
  • PgUp - Previous page
  • Home - Jump to the first page
  • End - Last page
  • g or M-g - Select the page. You can either pass page number via universal argument (C-u) or type it interactively.

Of course Wand-mode is not xpdf or gv, however it is pretty handle to skim over the document or for picking and saving particular page from book or video file.

If you set Wand-mode-show-fileinfo variable to non-nil value, then information about current page and total number of pages will be displayed.

Also note that any operation you apply, applies only to current page and not to whole bunch of pages in the image.

Wand-mode Menu

Wand-mode has nice menu (resembling display(1) menu) to work with images:

Wand-display menu

You can reach menu either from menubar (if enabled), or using right button on your mouse. All commands available through the keyboard are available via menu and vice versa.

Customizing Wand-mode

Wand-mode has few parameters to change it's behavior. Use M-x customize-group RET Wand-mode RET to change them. Here is a little description of most want-to-customize parameters:

  • Wand-mode-zoom-factor - Number (default: 2)

    As you can notice from it's name, this is the coefficient for + (zoom-in) and - (zoom-out) commands.

  • Wand-mode-show-fileinfo - Boolean (default: t)

    When non-nil, then display some information about the file, for more info, as was already noticed above, use i command.

  • Wand-mode-show-operations - Boolean (default: t)

    When non-nil, then display image operations stack. You can use undo/redo commands even if Wand-mode-show-operations is nil.

  • Wand-mode-auto-fit - Boolean (default: t)

    When non-nil, then scale image to fit it into SXEmacs window.

  • Wand-mode-auto-rotate - Boolean (default: t)

    When non-nil, then rotate image in case it has orientation info inside (modern photo cameras can detect own space orientation and store this info inside photos). If rotation was done, then rotation operation will be placed into image operations stack. Very handy.

Changing image and saving

Use next keys to operate on images:

  • l - Rotate by 90° counterclockwise (like to the left)
  • r - Rotate by 90° clockwise (like to the right)
  • + - Zoom in the image according to Wand-mode-zoom-factor
  • - - Zoom out the image according to Wand-mode-zoom-factor
  • o - General operational key. Inquiries for the operation to do and for its parameters. Supports completion.

You can apply operations one by other, like we already did. All operations are stored in image operations stack, so you can undo/redo any using standard C-/ (or C-_) and C-x C-/ keys. Also you can re-apply last operation with C-. (or C-x M-:) key. To abolish all operations just reload image with C-r.

Now when new image is read, we can save it to the disk. There are two commands to do it:

Attention!

ATTENTION: Saving to non-graphical formats such as HTML, TXT, etc, may lead to core dump. This is ImageMagick bug, but unfortunately ImageMagick developers not quite sociable guys, so only god knows when this will be fixed.

  • C-x C-w - You'll be asked about file format to use and filename.
  • C-x C-s - Simple way to save. You'll be queried for filename only. And file format will be automatically defined according to the extension of the filename you provided.

Operating on image region

Wand-display region

Using left mouse you can select image region, for example Sarah's hand. If Wand-mode-show-fileinfo is set to non-nil, then region's geometry (coordinates and size) will be displayed, sometimes this useful. When region is active then operations that can be applied to region, such as contrast, despeckle, etc will be applied to region only. There are few commands that are applicable only to regions:

  • c - crop the region and cut everything else
  • o chop RET - chop the region and assemble everything else together
  • Red eye reduction

After applying operation to region, region deactivates. To activate last region use C-M-z command.

Red eye reduction

Most important thing in the red eye reduction is correctly selected region. Best way to select region so all red part is in the centre of the region and there is a little free space from red part to the edges of the region. Here is an example of well formed region:

Wand-display red eye good region

And here is not so well selected region, however red eye reduction algorithm will handle it in this case, but in more complicated cases it might produce some artifacts:

Wand-display red eye bad region

After selecting the region simple press . or choose menu item Wand→Region→Remove red eye.

Red eye reduction algorithm used in Wand-mode is straightforward and can't compete with algorithms implemented in professional red eye reduction tools, however it demonstrates pretty good and usable results. Here is the comparison:

Wand-display red eyes sxemacs vs prof

Operations table

To finalize the article, here is brief overview of operations you can do on images using Wand-mode. Notice that operations which does not changes image size can be applied to region without problems:

Transform
Sample
Width: 400
Height: 200
Rotate
Degrees: 120°
Flip
Flop
Effect
Add Noise
Noise type: Poisson
Despeckle
Edge detect
Radius: 1.0
Emboss
Radius: 1.0
Sigma: 2.0
Gaussian Blur
Radius: 1.0
Sigma: 2.0
Radial Blur
Radius: 3.0
Reduce Noise
Sharpen
Radius: 1.0
Sigma: 2.0
Spread
Radius: 1.0
Enhance
Contrast
Type: increase
Contrast
Type: decrease
Enhance
Equalize
Negate
Normalize
F/X
Charcoal
Radius: 1.0
Sigma: 2.0
Implode
Radius: 0.4
Implode
Radius: -0.4
Oil Paint
Radius: 2.0
Sepia Tone
Threshold: 85%
Solarize
Threshold: 50%
Swirl
Angle: 90°
Vignette
Black/White: 10
Wave
Amplitude: 2
Wave length: 10