annotate src/imgproc.c @ 5353:38e24b8be4ea

Improve the lexical scoping in #'block, #'return-from. lisp/ChangeLog addition: 2011-02-07 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el: * bytecomp.el (byte-compile-initial-macro-environment): Shadow `block', `return-from' here, we implement them differently when byte-compiling. * bytecomp.el (byte-compile-active-blocks): New. * bytecomp.el (byte-compile-block-1): New. * bytecomp.el (byte-compile-return-from-1): New. * bytecomp.el (return-from-1): New. * bytecomp.el (block-1): New. These are two aliases that exist to have their own associated byte-compile functions, which functions implement `block' and `return-from'. * cl-extra.el (cl-macroexpand-all): Fix a bug here when macros in the environment have been compiled. * cl-macs.el (block): * cl-macs.el (return): * cl-macs.el (return-from): Be more careful about lexical scope in these macros. * cl.el: * cl.el ('cl-block-wrapper): Removed. * cl.el ('cl-block-throw): Removed. These aren't needed in code generated by this XEmacs. They shouldn't be needed in code generated by XEmacs 21.4, but if it turns out the packages do need them, we can put them back. 2011-01-30 Mike Sperber <mike@xemacs.org> * font-lock.el (font-lock-fontify-pending-extents): Don't fail if `font-lock-mode' is unset, which can happen in the middle of `revert-buffer'. 2011-01-23 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (delete): * cl-macs.el (delq): * cl-macs.el (remove): * cl-macs.el (remq): Don't use the compiler macro if these functions were given the wrong number of arguments, as happens in lisp-tests.el. * cl-seq.el (remove, remq): Removed. I added these to subr.el, and forgot to remove them from here. 2011-01-22 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-setq, byte-compile-set): Remove kludge allowing keywords' values to be set, all the code that does that is gone. * cl-compat.el (elt-satisfies-test-p): * faces.el (set-face-parent): * faces.el (face-doc-string): * gtk-font-menu.el: * gtk-font-menu.el (gtk-reset-device-font-menus): * msw-font-menu.el: * msw-font-menu.el (mswindows-reset-device-font-menus): * package-get.el (package-get-installedp): * select.el (select-convert-from-image-data): * sound.el: * sound.el (load-sound-file): * x-font-menu.el (x-reset-device-font-menus-core): Don't quote keywords, they're self-quoting, and the win from backward-compatibility is sufficiently small now that the style problem overrides it. 2011-01-22 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (block, return-from): Require that NAME be a symbol in these macros, as always documented in the #'block docstring and as required by Common Lisp. * descr-text.el (unidata-initialize-unihan-database): Correct the use of non-symbols in #'block and #'return-from in this function. 2011-01-15 Aidan Kehoe <kehoea@parhasard.net> * cl-extra.el (concatenate): Accept more complicated TYPEs in this function, handing the sequences over to #'coerce if we don't understand them here. * cl-macs.el (inline): Don't proclaim #'concatenate as inline, its compiler macro is more useful than doing that. 2011-01-11 Aidan Kehoe <kehoea@parhasard.net> * subr.el (delete, delq, remove, remq): Move #'remove, #'remq here, they don't belong in cl-seq.el; move #'delete, #'delq here from fns.c, implement them in terms of #'delete*, allowing support for sequences generally. * update-elc.el (do-autoload-commands): Use #'delete*, not #'delq here, now the latter's no longer dumped. * cl-macs.el (delete, delq): Add compiler macros transforming #'delete and #'delq to #'delete* calls. 2011-01-10 Aidan Kehoe <kehoea@parhasard.net> * dialog.el (make-dialog-box): Correct a misplaced parenthesis here, thank you Mats Lidell in 87zkr9gqrh.fsf@mail.contactor.se ! 2011-01-02 Aidan Kehoe <kehoea@parhasard.net> * dialog.el (make-dialog-box): * list-mode.el (display-completion-list): These functions used to use cl-parsing-keywords; change them to use defun* instead, fixing the build. (Not sure what led to me not including this change in d1b17a33450b!) 2011-01-02 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (define-star-compiler-macros): Make sure the form has ITEM and LIST specified before attempting to change to calls with explicit tests; necessary for some tests in lisp-tests.el to compile correctly. (stable-union, stable-intersection): Add compiler macros for these functions, in the same way we do for most of the other functions in cl-seq.el. 2011-01-01 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (dolist, dotimes, do-symbols, macrolet) (symbol-macrolet): Define these macros with defmacro* instead of parsing the argument list by hand, for the sake of style and readability; use backquote where appropriate, instead of calling #'list and and friends, for the same reason. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * x-misc.el (device-x-display): Provide this function, documented in the Lispref for years, but not existing previously. Thank you Julian Bradfield, thank you Jeff Mincy. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * cl-seq.el: Move the heavy lifting from this file to C. Dump the cl-parsing-keywords macro, but don't use defun* for the functions we define that do take keywords, dynamic scope lossage makes that not practical. * subr.el (sort, fillarray): Move these aliases here. (map-plist): #'nsublis is now built-in, but at this point #'eql isn't necessarily available as a test; use #'eq. * obsolete.el (cl-delete-duplicates): Make this available for old compiler macros and old code. (memql): Document that this is equivalent to #'member*, and worse. * cl.el (adjoin, subst): Removed. These are in C. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * simple.el (assoc-ignore-case): Remove a duplicate definition of this function (it's already in subr.el). * iso8859-1.el (char-width): On non-Mule, make this function equivalent to that produced by (constantly 1), but preserve its docstring. * subr.el (subst-char-in-string): Define this in terms of #'substitute, #'nsubstitute. (string-width): Define this using #'reduce and #'char-width. (char-width): Give this a simpler definition, it makes far more sense to check for mule at load time and redefine, as we do in iso8859-1.el. (store-substring): Implement this in terms of #'replace, now #'replace is cheap. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * update-elc.el (lisp-files-needed-for-byte-compilation) (lisp-files-needing-early-byte-compilation): cl-macs belongs in the former, not the latter, it is as fundamental as bytecomp.el. 2010-12-30 Aidan Kehoe <kehoea@parhasard.net> * cl.el: Provde the Common Lisp program-error, type-error as error symbols. This doesn't nearly go far enough for anyone using the Common Lisp errors. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (delete-duplicates): If the form has an incorrect number of arguments, don't attempt a compiler macroexpansion. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (cl-safe-expr-p): Forms that start with the symbol lambda are also safe. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (= < > <= >=): For these functions' compiler macros, the optimisation is safe even if the first and the last arguments have side effects, since they're only used the once. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (inline-side-effect-free-compiler-macros): Unroll a loop here at macro-expansion time, so these compiler macros are compiled. Use #'eql instead of #'eq in a couple of places for better style. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * cl-extra.el (notany, notevery): Avoid some dynamic scope stupidity with local variable names in these functions, when they weren't prefixed with cl-; go into some more detail in the doc strings. 2010-12-29 Aidan Kehoe <kehoea@parhasard.net> * byte-optimize.el (side-effect-free-fns): #'remove, #'remq are free of side-effects. (side-effect-and-error-free-fns): Drop dot, dot-marker from the list. 2010-11-17 Aidan Kehoe <kehoea@parhasard.net> * cl-extra.el (coerce): In the argument list, name the first argument OBJECT, not X; the former name was always used in the doc string and is clearer. Handle vector type specifications which include the length of the target sequence, error if there's a mismatch. * cl-macs.el (cl-make-type-test): Handle type specifications starting with the symbol 'eql. 2010-11-14 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (eql): Don't remove the byte-compile property of this symbol. That was necessary to override a bug in bytecomp.el where #'eql was confused with #'eq, which bug we no longer have. If neither expression is constant, don't attempt to handle the expression in this compiler macro, leave it to byte-compile-eql, which produces better code anyway. * bytecomp.el (eq): #'eql is not the function associated with the byte-eq byte code. (byte-compile-eql): Add an explicit compile method for this function, for cases where the cl-macs compiler macro hasn't reduced it to #'eq or #'equal. 2010-10-25 Aidan Kehoe <kehoea@parhasard.net> Add compiler macros and compilation sanity-checking for various functions that take keywords. * byte-optimize.el (side-effect-free-fns): #'symbol-value is side-effect free and not error free. * bytecomp.el (byte-compile-normal-call): Check keyword argument lists for sanity; store information about the positions where keyword arguments start using the new byte-compile-keyword-start property. * cl-macs.el (cl-const-expr-val): Take a new optional argument, cl-not-constant, defaulting to nil, in this function; return it if the expression is not constant. (cl-non-fixnum-number-p): Make this into a separate function, we want to pass it to #'every. (eql): Use it. (define-star-compiler-macros): Use the same code to generate the member*, assoc* and rassoc* compiler macros; special-case some code in #'add-to-list in subr.el. (remove, remq): Add compiler macros for these two functions, in preparation for #'remove being in C. (define-foo-if-compiler-macros): Transform (remove-if-not ...) calls to (remove ... :if-not) at compile time, which will be a real win once the latter is in C. (define-substitute-if-compiler-macros) (define-subst-if-compiler-macros): Similarly for these functions. (delete-duplicates): Change this compiler macro to use #'plists-equal; if we don't have information about the type of SEQUENCE at compile time, don't bother attempting to inline the call, the function will be in C soon enough. (equalp): Remove an old commented-out compiler macro for this, if we want to see it it's in version control. (subst-char-in-string): Transform this to a call to nsubstitute or nsubstitute, if that is appropriate. * cl.el (ldiff): Don't call setf here, this makes for a load-time dependency problem in cl-macs.el 2010-06-14 Stephen J. Turnbull <stephen@xemacs.org> * term/vt100.el: Refer to XEmacs, not GNU Emacs, in permissions. * term/bg-mouse.el: * term/sup-mouse.el: Put copyright notice in canonical "Copyright DATE AUTHOR" form. Refer to XEmacs, not GNU Emacs, in permissions. * site-load.el: Add permission boilerplate. * mule/canna-leim.el: * alist.el: Refer to XEmacs, not APEL/this program, in permissions. * mule/canna-leim.el: Remove my copyright, I've assigned it to the FSF. 2010-06-14 Stephen J. Turnbull <stephen@xemacs.org> * gtk.el: * gtk-widget-accessors.el: * gtk-package.el: * gtk-marshal.el: * gtk-compose.el: * gnome.el: Add copyright notice based on internal evidence. 2010-06-14 Stephen J. Turnbull <stephen@xemacs.org> * easymenu.el: Add reference to COPYING to permission notice. * gutter.el: * gutter-items.el: * menubar-items.el: Fix typo "Xmacs" in permissions notice. 2010-06-14 Stephen J. Turnbull <stephen@xemacs.org> * auto-save.el: * font.el: * fontconfig.el: * mule/kinsoku.el: Add "part of XEmacs" text to permission notice. 2010-10-14 Aidan Kehoe <kehoea@parhasard.net> * byte-optimize.el (side-effect-free-fns): * cl-macs.el (remf, getf): * cl-extra.el (tailp, cl-set-getf, cl-do-remf): * cl.el (ldiff, endp): Tighten up Common Lisp compatibility for #'ldiff, #'endp, #'tailp; add circularity checking for the first two. #'cl-set-getf and #'cl-do-remf were Lisp implementations of #'plist-put and #'plist-remprop; change the names to aliases, changes the macros that use them to using #'plist-put and #'plist-remprop directly. 2010-10-12 Aidan Kehoe <kehoea@parhasard.net> * abbrev.el (fundamental-mode-abbrev-table, global-abbrev-table): Create both these abbrev tables using the usual #'define-abbrev-table calls, rather than attempting to special-case them. * cl-extra.el: Force cl-macs to be loaded here, if cl-extra.el is being loaded interpreted. Previously other, later files would redundantly call (load "cl-macs") when interpreted, it's more reasonable to do it here, once. * cmdloop.el (read-quoted-char-radix): Use defcustom here, we don't have any dump-order dependencies that would prevent that. * custom.el (eval-when-compile): Don't load cl-macs when interpreted or when byte-compiling, rely on cl-extra.el in the former case and the appropriate entry in bytecomp-load-hook in the latter. Get rid of custom-declare-variable-list, we have no dump-time dependencies that would require it. * faces.el (eval-when-compile): Don't load cl-macs when interpreted or when byte-compiling. * packages.el: Remove some inaccurate comments. * post-gc.el (cleanup-simple-finalizers): Use #'delete-if-not here, now the order of preloaded-file-list has been changed to make it available. * subr.el (custom-declare-variable-list): Remove. No need for it. Also remove a stub define-abbrev-table from this file, given the current order of preloaded-file-list there's no need for it. 2010-10-10 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-constp) Forms quoted with FUNCTION are also constant. (byte-compile-initial-macro-environment): In #'the, if FORM is constant and does not match TYPE, warn at byte-compile time. 2010-10-10 Aidan Kehoe <kehoea@parhasard.net> * backquote.el (bq-vector-contents, bq-list*): Remove; the former is equivalent to (append VECTOR nil), the latter to (list* ...). (bq-process-2): Use (append VECTOR nil) instead of using #'bq-vector-contents to convert to a list. (bq-process-1): Now we use list* instead of bq-list * subr.el (list*): Moved from cl.el, since it is now required to be available the first time a backquoted form is encountered. * cl.el (list*): Move to subr.el. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * test-harness.el (Check-Message): Add an omitted comma here, thank you the buildbot. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * hash-table.el (hash-table-key-list, hash-table-value-list) (hash-table-key-value-alist, hash-table-key-value-plist): Remove some useless #'nreverse calls in these files; our hash tables have no order, it's not helpful to pretend they do. * behavior.el (read-behavior): Do the same in this file, in some code evidently copied from hash-table.el. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * info.el (Info-insert-dir): * format.el (format-deannotate-region): * files.el (cd, save-buffers-kill-emacs): Use #'some, #'every and related functions for applying boolean operations to lists, instead of rolling our own ones that cons and don't short-circuit. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-initial-macro-environment): * cl-macs.el (the): Rephrase the docstring, make its implementation when compiling files a little nicer. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * descr-text.el (unidata-initialize-unicodedata-database) (unidata-initialize-unihan-database, describe-char-unicode-data) (describe-char-unicode-data): Wrap calls to the database functions with (with-fboundp ...), avoiding byte compile warnings on builds without support for the database functions. (describe-char): (reduce #'max ...), not (apply #'max ...), no need to cons needlessly. (describe-char): Remove a redundant lambda wrapping #'extent-properties. (describe-char-unicode-data): Call #'nsubst when replacing "" with nil in the result of #'split-string, instead of consing inside mapcar. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * x-faces.el (x-available-font-sizes): * specifier.el (let-specifier): * package-ui.el (pui-add-required-packages): * msw-faces.el (mswindows-available-font-sizes): * modeline.el (modeline-minor-mode-menu): * minibuf.el (minibuf-directory-files): Replace the O2N (delq nil (mapcar (lambda (W) (and X Y)) Z)) with the ON (mapcan (lambda (W) (and X (list Y))) Z) in these files. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el (= < > <= >=): When these functions are handed more than two arguments, and those arguments have no side effects, transform to a series of two argument calls, avoiding funcall in the byte-compiled code. * mule/mule-cmds.el (finish-set-language-environment): Take advantage of this change in a function called 256 times at startup. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-function-form, byte-compile-quote) (byte-compile-quote-form): Warn at compile time, and error at runtime, if a (quote ...) or a (function ...) form attempts to quote more than one object. 2010-09-16 Aidan Kehoe <kehoea@parhasard.net> * byte-optimize.el (byte-optimize-apply): Transform (apply 'nconc (mapcar ...)) to (mapcan ...); warn about use of the first idiom. * update-elc.el (do-autoload-commands): * packages.el (packages-find-package-library-path): * frame.el (frame-list): * extents.el (extent-descendants): * etags.el (buffer-tag-table-files): * dumped-lisp.el (preloaded-file-list): * device.el (device-list): * bytecomp-runtime.el (proclaim-inline, proclaim-notinline) Use #'mapcan, not (apply #'nconc (mapcar ...) in all these files. * bytecomp-runtime.el (eval-when-compile, eval-and-compile): In passing, mention that these macros also evaluate the body when interpreted. tests/ChangeLog addition: 2011-02-07 Aidan Kehoe <kehoea@parhasard.net> * automated/lisp-tests.el: Test lexical scope for `block', `return-from'; add a Known-Bug-Expect-Failure for a contorted example that fails when byte-compiled.
author Aidan Kehoe <kehoea@parhasard.net>
date Mon, 07 Feb 2011 12:01:24 +0000
parents 6c6d78781d59
children 2aa9cd456ae7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* Image processing functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1998 Jareth Hein
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 This file is a part of XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 /* Synched up with: Not in FSF. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 /* Original author: Jareth Hein */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 /* Parts of this file are based on code from Sam Leffler's tiff library,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 with the original copyright displayed here:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 Copyright (c) 1988-1997 Sam Leffler
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 Copyright (c) 1991-1997 Silicon Graphics, Inc.
5169
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
30 Copyright (C) 2010 Ben Wing.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 Permission to use, copy, modify, distribute, and sell this software and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 its documentation for any purpose is hereby granted without fee, provided
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 that (i) the above copyright notices and this permission notice appear in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 all copies of the software and related documentation, and (ii) the names of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 Sam Leffler and Silicon Graphics may not be used in any advertising or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 publicity relating to the software without the specific, prior written
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 permission of Sam Leffler and Silicon Graphics. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 /* Quantizing code based off of the paper
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 Color Image Quantization for Frame Buffer Display, Paul Heckbert,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 Siggraph '82 proceedings, pp. 297-307 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 #include "imgproc.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 static void
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
49 get_histogram(quant_table *qt, Binbyte *pic,
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 int width, int height, Colorbox* box)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 {
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
52 register Binbyte *inptr;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 register int red, green, blue;
647
b39c14581166 [xemacs-hg @ 2001-08-13 04:45:47 by ben]
ben
parents: 428
diff changeset
54 register int j, i;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 box->rmin = box->gmin = box->bmin = 999;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 box->rmax = box->gmax = box->bmax = -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 box->total = width * height;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 inptr = pic;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 for (i = 0; i < height; i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 for (j = width; j-- > 0;)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 red = *inptr++ >> COLOR_SHIFT;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 green = *inptr++ >> COLOR_SHIFT;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 blue = *inptr++ >> COLOR_SHIFT;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 if (red < box->rmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 box->rmin = red;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 if (red > box->rmax)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 box->rmax = red;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 if (green < box->gmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 box->gmin = green;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 if (green > box->gmax)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 box->gmax = green;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 if (blue < box->bmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 box->bmin = blue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 if (blue > box->bmax)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 box->bmax = blue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 qt->histogram[red][green][blue]++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 static Colorbox *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 largest_box(quant_table *qt)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 register Colorbox *p, *b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 register int size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 b = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 size = -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 for (p = qt->usedboxes; p != NULL; p = p->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 if ((p->rmax > p->rmin || p->gmax > p->gmin ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 p->bmax > p->bmin) && p->total > size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 size = (b = p)->total;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 return (b);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 shrinkbox(quant_table *qt, Colorbox* box)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 register int *histp, ir, ig, ib;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 if (box->rmax > box->rmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 for (ir = box->rmin; ir <= box->rmax; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 for (ig = box->gmin; ig <= box->gmax; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 histp = &(qt->histogram[ir][ig][box->bmin]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 for (ib = box->bmin; ib <= box->bmax; ++ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 if (*histp++ != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 box->rmin = ir;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 goto have_rmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 have_rmin:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 if (box->rmax > box->rmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 for (ir = box->rmax; ir >= box->rmin; --ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 for (ig = box->gmin; ig <= box->gmax; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 histp = &(qt->histogram[ir][ig][box->bmin]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 ib = box->bmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 for (; ib <= box->bmax; ++ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 if (*histp++ != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 box->rmax = ir;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 goto have_rmax;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 have_rmax:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 if (box->gmax > box->gmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 for (ig = box->gmin; ig <= box->gmax; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 for (ir = box->rmin; ir <= box->rmax; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 histp = &(qt->histogram[ir][ig][box->bmin]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 for (ib = box->bmin; ib <= box->bmax; ++ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 if (*histp++ != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 box->gmin = ig;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 goto have_gmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 have_gmin:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 if (box->gmax > box->gmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 for (ig = box->gmax; ig >= box->gmin; --ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 for (ir = box->rmin; ir <= box->rmax; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 histp = &(qt->histogram[ir][ig][box->bmin]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 ib = box->bmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 for (; ib <= box->bmax; ++ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 if (*histp++ != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 box->gmax = ig;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 goto have_gmax;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 have_gmax:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 if (box->bmax > box->bmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 for (ib = box->bmin; ib <= box->bmax; ++ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 for (ir = box->rmin; ir <= box->rmax; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 histp = &(qt->histogram[ir][box->gmin][ib]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 for (ig = box->gmin; ig <= box->gmax; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 if (*histp != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 box->bmin = ib;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 goto have_bmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 histp += B_LEN;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 have_bmin:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 if (box->bmax > box->bmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 for (ib = box->bmax; ib >= box->bmin; --ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 for (ir = box->rmin; ir <= box->rmax; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 histp = &(qt->histogram[ir][box->gmin][ib]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 ig = box->gmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 for (; ig <= box->gmax; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 if (*histp != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 box->bmax = ib;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 goto have_bmax;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 histp += B_LEN;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 have_bmax:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 ;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 splitbox(quant_table *qt, Colorbox* ptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 int hist2[B_LEN];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 int first = 0, last = 0;
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
206 register Colorbox *new_;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 register int *iptr, *histp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 register int i, j;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 register int ir,ig,ib;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 register int sum, sum1, sum2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 enum { RED, GREEN, BLUE } axis;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 * See which axis is the largest, do a histogram along that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 * axis. Split at median point. Contract both new boxes to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 * fit points and return
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 i = ptr->rmax - ptr->rmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 if (i >= ptr->gmax - ptr->gmin && i >= ptr->bmax - ptr->bmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 axis = RED;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 else if (ptr->gmax - ptr->gmin >= ptr->bmax - ptr->bmin)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 axis = GREEN;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 axis = BLUE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 /* get histogram along longest axis */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 switch (axis)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 case RED:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 histp = &hist2[ptr->rmin];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 *histp = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 iptr = &(qt->histogram[ir][ig][ptr->bmin]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 *histp += *iptr++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 histp++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 first = ptr->rmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 last = ptr->rmax;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 case GREEN:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 histp = &hist2[ptr->gmin];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 *histp = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 iptr = &(qt->histogram[ir][ig][ptr->bmin]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 *histp += *iptr++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 histp++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 first = ptr->gmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 last = ptr->gmax;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 case BLUE:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 histp = &hist2[ptr->bmin];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 for (ib = ptr->bmin; ib <= ptr->bmax; ++ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 *histp = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 for (ir = ptr->rmin; ir <= ptr->rmax; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 iptr = &(qt->histogram[ir][ptr->gmin][ib]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 for (ig = ptr->gmin; ig <= ptr->gmax; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 *histp += *iptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 iptr += B_LEN;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 histp++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 first = ptr->bmin;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 last = ptr->bmax;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 /* find median point */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 sum2 = ptr->total / 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 histp = &hist2[first];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 sum = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 for (i = first; i <= last && (sum += *histp++) < sum2; ++i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 ;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 if (i == first)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 i++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 /* Create new box, re-allocate points */
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
290 new_ = qt->freeboxes;
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
291 qt->freeboxes = new_->next;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 if (qt->freeboxes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 qt->freeboxes->prev = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 if (qt->usedboxes)
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
295 qt->usedboxes->prev = new_;
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
296 new_->next = qt->usedboxes;
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
297 qt->usedboxes = new_;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 histp = &hist2[first];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 for (sum1 = 0, j = first; j < i; j++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 sum1 += *histp++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 for (sum2 = 0, j = i; j <= last; j++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 sum2 += *histp++;
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
304 new_->total = sum1;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 ptr->total = sum2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
307 new_->rmin = ptr->rmin;
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
308 new_->rmax = ptr->rmax;
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
309 new_->gmin = ptr->gmin;
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
310 new_->gmax = ptr->gmax;
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
311 new_->bmin = ptr->bmin;
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
312 new_->bmax = ptr->bmax;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 switch (axis)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 case RED:
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
316 new_->rmax = i-1;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 ptr->rmin = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 case GREEN:
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
320 new_->gmax = i-1;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 ptr->gmin = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 case BLUE:
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
324 new_->bmax = i-1;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 ptr->bmin = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 }
3025
facf3239ba30 [xemacs-hg @ 2005-10-25 11:16:19 by ben]
ben
parents: 2367
diff changeset
328 shrinkbox (qt, new_);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 shrinkbox (qt, ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 static C_cell *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 create_colorcell(quant_table *qt, int num_colors, int red, int green, int blue)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 register int ir, ig, ib, i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 register C_cell *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 int mindist, next_n;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 register int tmp, dist, n;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 ir = red >> (COLOR_DEPTH-C_DEPTH);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 ig = green >> (COLOR_DEPTH-C_DEPTH);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 ib = blue >> (COLOR_DEPTH-C_DEPTH);
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
344 ptr = xnew (C_cell);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 *(qt->ColorCells + ir*C_LEN*C_LEN + ig*C_LEN + ib) = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 ptr->num_ents = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 * Step 1: find all colors inside this cell, while we're at
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 * it, find distance of centermost point to furthest corner
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 mindist = 99999999;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 for (i = 0; i < num_colors; ++i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 if (qt->rm[i]>>(COLOR_DEPTH-C_DEPTH) != ir ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 qt->gm[i]>>(COLOR_DEPTH-C_DEPTH) != ig ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 qt->bm[i]>>(COLOR_DEPTH-C_DEPTH) != ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 ptr->entries[ptr->num_ents][0] = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 ptr->entries[ptr->num_ents][1] = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 ++ptr->num_ents;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 tmp = qt->rm[i] - red;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 if (tmp < (MAX_COLOR/C_LEN/2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 tmp = MAX_COLOR/C_LEN-1 - tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 dist = tmp*tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 tmp = qt->gm[i] - green;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 if (tmp < (MAX_COLOR/C_LEN/2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 tmp = MAX_COLOR/C_LEN-1 - tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 dist += tmp*tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 tmp = qt->bm[i] - blue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 if (tmp < (MAX_COLOR/C_LEN/2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 tmp = MAX_COLOR/C_LEN-1 - tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 dist += tmp*tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 if (dist < mindist)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 mindist = dist;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 * Step 3: find all points within that distance to cell.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 for (i = 0; i < num_colors; ++i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 if (qt->rm[i] >> (COLOR_DEPTH-C_DEPTH) == ir &&
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 qt->gm[i] >> (COLOR_DEPTH-C_DEPTH) == ig &&
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 qt->bm[i] >> (COLOR_DEPTH-C_DEPTH) == ib)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 dist = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 if ((tmp = red - qt->rm[i]) > 0 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 (tmp = qt->rm[i] - (red + MAX_COLOR/C_LEN-1)) > 0 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 dist += tmp*tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 if ((tmp = green - qt->gm[i]) > 0 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 (tmp = qt->gm[i] - (green + MAX_COLOR/C_LEN-1)) > 0 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 dist += tmp*tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 if ((tmp = blue - qt->bm[i]) > 0 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 (tmp = qt->bm[i] - (blue + MAX_COLOR/C_LEN-1)) > 0 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 dist += tmp*tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 if (dist < mindist)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 ptr->entries[ptr->num_ents][0] = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 ptr->entries[ptr->num_ents][1] = dist;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 ++ptr->num_ents;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 * Sort color cells by distance, use cheap exchange sort
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 for (n = ptr->num_ents - 1; n > 0; n = next_n)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 next_n = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 for (i = 0; i < n; ++i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 if (ptr->entries[i][1] > ptr->entries[i+1][1])
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 tmp = ptr->entries[i][0];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 ptr->entries[i][0] = ptr->entries[i+1][0];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 ptr->entries[i+1][0] = tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 tmp = ptr->entries[i][1];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 ptr->entries[i][1] = ptr->entries[i+1][1];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 ptr->entries[i+1][1] = tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 next_n = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 return (ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 map_colortable(quant_table *qt, int num_colors)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 register int *histp = &(qt->histogram[0][0][0]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 register C_cell *cell;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 register int j, tmp, d2, dist;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 int ir, ig, ib, i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 for (ir = 0; ir < B_LEN; ++ir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 for (ig = 0; ig < B_LEN; ++ig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 for (ib = 0; ib < B_LEN; ++ib, histp++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 if (*histp == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 *histp = -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 cell = *(qt->ColorCells +
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 (((ir>>(B_DEPTH-C_DEPTH)) << C_DEPTH*2) +
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 ((ig>>(B_DEPTH-C_DEPTH)) << C_DEPTH) +
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 (ib>>(B_DEPTH-C_DEPTH))));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 if (cell == NULL )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 cell = create_colorcell (qt, num_colors,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 ir << COLOR_SHIFT,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 ig << COLOR_SHIFT,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 ib << COLOR_SHIFT);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 if (cell == NULL) /* memory exhausted! punt! */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 dist = 9999999;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 for (i = 0; i < cell->num_ents &&
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 dist > cell->entries[i][1]; ++i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 j = cell->entries[i][0];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 d2 = qt->rm[j] - (ir << COLOR_SHIFT);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 d2 *= d2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 tmp = qt->gm[j] - (ig << COLOR_SHIFT);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 d2 += tmp*tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 tmp = qt->bm[j] - (ib << COLOR_SHIFT);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 d2 += tmp*tmp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 if (d2 < dist)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 dist = d2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 *histp = j;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 quant_table *
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
476 build_EImage_quantable(Binbyte *eimage, int width, int height, int num_colors)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 quant_table *qt;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 Colorbox *box_list, *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 int i,res;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 qt = (quant_table*)xmalloc_and_zero (sizeof(quant_table));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 if (qt == NULL) return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 assert (num_colors < 257 && num_colors > 2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 * STEP 1: create empty boxes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 qt->usedboxes = NULL;
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
490 box_list = qt->freeboxes = xnew_array (Colorbox, num_colors);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 qt->freeboxes[0].next = &(qt->freeboxes[1]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 qt->freeboxes[0].prev = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 for (i = 1; i < num_colors-1; ++i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 qt->freeboxes[i].next = &(qt->freeboxes[i+1]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 qt->freeboxes[i].prev = &(qt->freeboxes[i-1]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 qt->freeboxes[num_colors-1].next = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 qt->freeboxes[num_colors-1].prev = &(qt->freeboxes[num_colors-2]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 * STEP 2: get histogram, initialize first box
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 ptr = qt->freeboxes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 qt->freeboxes = ptr->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 if (qt->freeboxes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 qt->freeboxes->prev = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 ptr->next = qt->usedboxes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 qt->usedboxes = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 if (ptr->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 ptr->next->prev = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 get_histogram (qt, eimage, width, height, ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 * STEP 3: continually subdivide boxes until no more free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 * boxes remain or until all colors assigned.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 while (qt->freeboxes != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 ptr = largest_box(qt);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 if (ptr != NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 splitbox (qt, ptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 qt->freeboxes = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 * STEP 4: assign colors to all boxes
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 for (i = 0, ptr = qt->usedboxes; ptr != NULL; ++i, ptr = ptr->next)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 qt->rm[i] = ((ptr->rmin + ptr->rmax) << COLOR_SHIFT) / 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 qt->gm[i] = ((ptr->gmin + ptr->gmax) << COLOR_SHIFT) / 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 qt->bm[i] = ((ptr->bmin + ptr->bmax) << COLOR_SHIFT) / 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 qt->um[i] = ptr->total;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 qt->num_active_colors = i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 /* We're done with the boxes now */
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 3025
diff changeset
540 xfree (box_list);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 qt->freeboxes = qt->usedboxes = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 * STEP 5: scan histogram and map all values to closest color
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 /* 5a: create cell list as described in Heckbert */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 qt->ColorCells = (C_cell **)xmalloc_and_zero (C_LEN*C_LEN*C_LEN*sizeof (C_cell*));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 /* 5b: create mapping from truncated pixel space to color
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 table entries */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 res = map_colortable (qt, num_colors);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 /* 5c: done with ColorCells */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 for (i = 0; i < C_LEN*C_LEN*C_LEN; i++)
1726
a8d8f419b459 [xemacs-hg @ 2003-09-30 15:26:34 by james]
james
parents: 647
diff changeset
554 if (qt->ColorCells[i])
5169
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
555 {
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
556 xfree (qt->ColorCells[i]);
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
557 qt->ColorCells[i] = 0;
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
558 }
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 3025
diff changeset
559 xfree (qt->ColorCells);
5169
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
560 qt->ColorCells = 0;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 if (res)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 {
1726
a8d8f419b459 [xemacs-hg @ 2003-09-30 15:26:34 by james]
james
parents: 647
diff changeset
564 /* we failed in memory allocation, so clean up and leave */
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 3025
diff changeset
565 xfree (qt);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 return NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 return qt;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 }