;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; $Id: img-util.lsh,v 1.3 2005/08/24 21:47:14 leonb Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#? *** Miscellaneous Image Macros and Conversions
;; basic macros and functions to manipulate
;; dimensions and internal formats of images

(dm FRED(f)  '0.299)
(dm FGREEN(f) '0.587)
(dm FBLUE(f) '0.114)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; internal format conversions

#? (rgbaim2rgbafim <in>)
;; convert RGBA image of ubytes into RGBA image of floats.
(de rgbaim2rgbafim (in)
    ((-idx3- (-ubyte-)) in)
    (let ((rez (float-matrix-nc (idx-dim in 0) (idx-dim in 1) (idx-dim in 2))))
      (idx-copy in rez)))

#? (rgbafim2rgbaim <in>)
;; convert RGBA image of floats into RGBA image of ubytes.
(de rgbafim2rgbaim (in)
    ((-idx3- (-flt-)) in)
    (let ((rez (ubyte-matrix-nc (idx-dim in 0) (idx-dim in 1) (idx-dim in 2))))
      (idx-copy in rez)))

#? (pixel2rgbim <in>)
;; converts an integer matrix of pixel values into a RGB image
(de pixel2rgbim(image)
  ((-idx2- (-int-)) image)
  (let* ((w (idx-dim image 1))
         (h (idx-dim image 0))
         (ubim (ubyte-matrix-nc h w 3)))
    ((-int-) w h)
    (idx-bloop ((ubrow ubim)(row image))
      (idx-bloop ((ubpix ubrow)(pix row))
        (let ((x (pix)))
          ((-int-) x) ;; C inline?
          (ubpix 0 (bitand 0xff (bitshr x 24)))
          (ubpix 0 (bitand 0xff (bitshr x 16)))
          (ubpix 2 (bitand 0xff x)) ) ) )
    ubim ) )
  


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; dynamic resizing

(dm anyim-resize(f out-im sz)
    `(when (or
           (< (idx-dim ,out-im 0) ncol)
           (< (idx-dim ,out-im 1) nlin)
           )
      (idx-u3resize ,out-im nlin ncol ,sz)
      ))

(dm ubim-resize(f out-im)
    `(when (or
           (< (idx-dim ,out-im 0) ncol)
           (< (idx-dim ,out-im 1) nlin)
           )
      (idx-u2resize ,out-im nlin ncol)
      ))



(dm rgba-resize(f out-im)
    `(when (or
           (< (idx-dim ,out-im 0) ncol)
           (< (idx-dim ,out-im 1) nlin)
           )
      (idx-u3resize ,out-im nlin ncol 4)
      ))

(dm rgb-resize(f out-im)
    `(when (or
           (< (idx-dim ,out-im 0) ncol)
           (< (idx-dim ,out-im 1) nlin)
           )
      (idx-u3resize ,out-im nlin ncol 3)
      ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#? * img-dim-C
;; class that holds the two dimensions of an image
;; d0: height
;; d1: width
(defclass img-dim-C object
  ((-int-) d0 d1))

#? (new img-dim-C <d0> <d1>)
(defmethod img-dim-C img-dim-C (ad0 ad1)
  ((-int-) ad0 ad1)
  (setq d0  ad0 )
  (setq d1  ad1 ))

#? (equal-dim <dimA> <dimB>)
;;.VP
;; ((-obj- (img-dim-C)) dimA dimB):
;;
;; CREATED: Oct 97
;; DESCRIPTION:
;; t if dimA and dimB are equal
(de equal-dim(dimA dimB)
    ((-obj- (img-dim-C)) dimA dimB)
    (and (= :dimA:d0 :dimB:d0)
         (= :dimA:d1 :dimB:d1)))

#? (align-dimensions <img-dim> <align-block>)
;;.VP
;; ((-obj- (img-dim-C)) img-dim):
;; (-int- align-block):
;;
;; CREATED: Oct 97
;; DESCRIPTION:
;; return new dimension object
;; - dimensions larger than img-dim
;; - multiple of align-block
(de align-dimensions (img-dim align-block)
    ((-obj- (img-dim-C)) img-dim)
    ((-int-) align-block)
    (new img-dim-C
         (* align-block (1+ (div (1- :img-dim:d0) align-block)))
         (* align-block (1+ (div (1- :img-dim:d1) align-block)))))

________________________________________________________________

(dhc-make () 
          rgbaim2rgbafim rgbafim2rgbaim pixel2rgbim
          (img-dim-C img-dim-C) 
          equal-dim align-dimensions)
