[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Drawing in images?

From: joakim
Subject: Re: Drawing in images?
Date: Wed, 16 Sep 2009 21:04:14 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

Chong Yidong <address@hidden> writes:

> address@hidden writes:
>> This should be possible by drawing a SVG image on top of another
>> image. The SVG image is XML which can be generated in a buffer within
>> Emacs.
> If I understand you, you're suggesting to generate an SVG image that
> contains a bitmap (the underlying image), plus a rectangle (or whatever
> else), correct?  This approach has the great advantage that it can be
> implemented entirely in Lisp.  Anyone want to give it a shot?

Here's an attempt. Its hardly industrial strength, but seems to work.
Test with m-x  dragbox-start, enter an image file name compatible with
svg, I tried png. Then set the corner coords for the box with LMB and RMB.

(setq dragbox-image-url "")
(setq dragbox-image-width 744)
(setq dragbox-image-height 1052)

(setq dragbox-x1y1 '(0 . 0))
(setq dragbox-x2y2 '(100 . 100))

(defun dragbox-start (image-file)
  "start here"
  (interactive "fimage file:")
  (get-buffer-create "*dragbox*")
  (switch-to-buffer  "*dragbox*")
  (setq dragbox-image-url (concat "file://" (expand-file-name image-file)))

(defun dragbox-make-svg-data (x y width height image-url)
         ((xmlns:dc . "http://purl.org/dc/elements/1.1/";)
          (xmlns:cc . "http://creativecommons.org/ns#";)
          (xmlns:rdf . "http://www.w3.org/1999/02/22-rdf-syntax-ns#";)
          (xmlns:svg . "http://www.w3.org/2000/svg";)
          (xmlns:xlink . "http://www.w3.org/1999/xlink";)
          (xmlns . "http://www.w3.org/2000/svg";)
          (width . ,(number-to-string dragbox-image-width))
          (height . ,(number-to-string dragbox-image-height))
          (id . "svg2"))
          ((id . "layer1"))
           ((style . "fill:#cfcfcf;fill-opacity:1")
            (width . ,(number-to-string dragbox-image-width))
            (height . ,(number-to-string dragbox-image-height))
            (x . "0")
            (y . "0")))
          (image ((y . "0")
                  (x . "0")
                  (width . ,(number-to-string dragbox-image-width))
                  (height . ,(number-to-string dragbox-image-height))
                  (xlink:href . ,image-url)
           ((style . 
            (id . "dragbox")
            (width . , (number-to-string width) )
            (height . ,(number-to-string height))
            (x . ,(number-to-string x))
            (y . ,(number-to-string y))))


(defun dragbox-lmb-click-handler ()
            (setq dragbox-x1y1 (dragbox-extract-event-coords last-input-event))

(defun dragbox-rmb-click-handler ()
            (setq dragbox-x2y2 (dragbox-extract-event-coords last-input-event))

(defun dragbox-extract-event-coords (event)
   (caddr(cadr last-input-event))

(define-key image-mode-map [down-mouse-1] 'dragbox-lmb-click-handler)
(define-key image-mode-map [down-mouse-2] 'dragbox-rmb-click-handler)

(defun dragbox-update-box (x y width height)

  (xml-print (dragbox-make-svg-data x y width height dragbox-image-url))

(defun dragbox-update-box-from-state ()
      ((x1 (car dragbox-x1y1))
       (y1 (cdr dragbox-x1y1))
       (x2 (car dragbox-x2y2))
       (y2 (cdr dragbox-x2y2))
       (w (- x2 x1))
       (h (- y2 y1)))
    (message "(%d %d) (%d %d) w:%d h:%d" x1 y1 x2 y2 w h)


Joakim Verona

reply via email to

[Prev in Thread] Current Thread [Next in Thread]