source: trunk/libdjvu/GRect.h @ 17

Last change on this file since 17 was 17, checked in by Eugene Romanenko, 16 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

File size: 14.0 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: GRect.h,v 1.11 2006/02/21 16:10:29 docbill Exp $
55// $Name:  $
56
57#ifndef _GRECT_H_
58#define _GRECT_H_
59#ifdef HAVE_CONFIG_H
60#include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma interface
64#endif
65
66
67/** @name GRect.h
68    Files #"GRect.h"# and #"GRect.cpp"# implement basic operations on
69    rectangles. Class \Ref{GRect} is used to represent rectangles.  Class
70    \Ref{GRectMapper} represent the correspondence between points relative to
71    given rectangles.  Class \Ref{GRatio} is used to represent scaling factors
72    as rational numbers.
73    @memo
74    Rectangle manipulation class.
75    @author
76    L\'eon Bottou <leonb@research.att.com> -- initial implementation.
77    @version
78    #$Id: GRect.h,v 1.11 2006/02/21 16:10:29 docbill Exp $# */
79//@{
80
81#include "DjVuGlobal.h"
82
83#ifdef HAVE_NAMESPACES
84namespace DJVU {
85# ifdef NOT_DEFINED // Just to fool emacs c++ mode
86}
87#endif
88#endif
89
90
91/* Flag to indicate that this djvulibre version
92   gets rid of all the crap about orientation bits.
93   All rotation code has been fixed and consistently
94   implements counter-clockwise rotations. */
95
96#define GRECT_WITHOUT_ORIENTATION_BITS 1
97
98
99/** @name Point Coordinates vs. Pixel Coordinates
100
101    The DjVu technology relies on the accurate superposition of images at
102    different resolutions.  Such an accuracy cannot be reached with the usual
103    assumption that pixels are small enough to be considered infinitesimally
104    small.  We must distinguish very precisely ``points'' and ``pixels''.
105    This distinction is essential for performing scaling operations.
106
107    The pixels of an image are identified by ``pixel coordinates''.  The
108    bottom-left corner pixel has coordinates #(0,0)# and the top-right corner
109    pixel has coordinates #(w-1,h-1)# where #w# and #h# are the image size.
110    Pixel coordinates are necessarily integers since pixels never overlap.
111
112    An infinitesimally small point is identified by its ``point coordinates''.
113    There may be fractional point coordinates, although this library does not
114    make use of them.  Points with integer coordinates are located {\em on the
115    corners of each pixel}.  They are not located on the pixel centers.  The
116    center of the pixel with pixel coordinates #(i,j)# is located at point
117    coordinates #(i+1/2,j+1/2)#.  In other words, the pixel #(i,j)# extends
118    from point #(i,j)# to point #(i+1,j+1)#.
119
120    Therefore, the point located on the bottom left corner of an image has
121    coordinates #(0,0)#.  This point is in fact the bottom left corner of the
122    bottom left pixel of the image.  The point located on the top right corner
123    of an image has coordinates #(w,h)# where #w# and #h# are the image size.
124    This is in fact the top right corner of pixel #(w-1,h-1)# which is the
125    image pixel with the highest coordinates.
126*/
127//@{
128//@}
129
130
131
132/** Rectangle class.  Each instance of this class represents a rectangle whose
133    sides are parallel to the axis. Such a rectangle represents all the points
134    whose coordinates lies between well defined minimal and maximal values.
135    Member functions can combine several rectangles by computing the
136    intersection of rectangles (\Ref{intersect}) or the smallest rectangle
137    enclosing two rectangles (\Ref{recthull}).  */
138
139class GRect
140{
141public:
142  /** Constructs an empty rectangle */
143  GRect();
144  /** Constructs a rectangle given its minimal coordinates #xmin# and #ymin#,
145      and its measurements #width# and #height#. Setting #width# or #height# to zero
146      produces an empty rectangle.  */
147  GRect(int xmin, int ymin, unsigned int width=0, unsigned int height=0);
148  /** Returns the rectangle width. */
149  int  width() const;
150  /** Returns the rectangle height. */
151  int  height() const;
152  /** Returns the area of the rectangle. */
153  int  area() const;
154  /** Returns true if the rectangle is empty. */
155  bool  isempty() const;
156  /** Returns true if the rectangle contains pixel (#x#,#y#).  A rectangle
157      contains all pixels with horizontal pixel coordinates in range #xmin#
158      (inclusive) to #xmax# (exclusive) and vertical coordinates #ymin#
159      (inclusive) to #ymax# (exclusive). */
160  int  contains(int x, int y) const;
161  /** Returns true if this rectangle contains the passed rectangle #rect#.
162      The function basically checks, that the intersection of this rectangle
163      with #rect# is #rect#. */
164  int  contains(const GRect & rect) const;
165  /** Returns true if rectangles #r1# and #r2# are equal. */
166  friend int operator==(const GRect & r1, const GRect & r2);
167  /** Returns true if rectangles #r1# and #r2# are not equal. */
168  friend int operator!=(const GRect & r1, const GRect & r2);
169  /** Resets the rectangle to the empty rectangle */
170  void clear();
171  /** Fatten the rectangle. Both vertical sides of the rectangle are pushed
172      apart by #dx# units. Both horizontal sides of the rectangle are pushed
173      apart by #dy# units. Setting arguments #dx# (resp. #dy#) to a negative
174      value reduces the rectangle horizontal (resp. vertical) size. */
175  int  inflate(int dx, int dy);
176  /** Translate the rectangle. The new rectangle is composed of all the points
177      of the old rectangle translated by #dx# units horizontally and #dy#
178      units vertically. */
179  int  translate(int dx, int dy);
180  /** Sets the rectangle to the intersection of rectangles #rect1# and #rect2#.
181      This function returns true if the intersection rectangle is not empty. */
182  int  intersect(const GRect &rect1, const GRect &rect2);
183  /** Sets the rectangle to the smallest rectangle containing the points of
184      both rectangles #rect1# and #rect2#. This function returns true if the
185      created rectangle is not empty. */
186  int  recthull(const GRect &rect1, const GRect &rect2);
187  /** Multiplies xmin, ymin, xmax, ymax by factor and scales the rectangle*/
188  void scale(float factor);
189  /** Multiplies xmin, xmax by xfactor and ymin, ymax by yfactor and scales the rectangle*/
190  void scale(float xfactor, float yfactor);
191  /** Minimal horizontal point coordinate of the rectangle. */
192  int xmin;
193  /** Minimal vertical point coordinate of the rectangle. */
194  int ymin;
195  /** Maximal horizontal point coordinate of the rectangle. */
196  int xmax;
197  /** Maximal vertical point coordinate of the rectangle. */
198  int ymax;
199};
200
201
202/** Maps points from one rectangle to another rectangle.  This class
203    represents a relation between the points of two rectangles. Given the
204    coordinates of a point in the first rectangle (input rectangle), function
205    \Ref{map} computes the coordinates of the corresponding point in the
206    second rectangle (the output rectangle).  This function actually implements
207    an affine transform which maps the corners of the first rectangle onto the
208    matching corners of the second rectangle. The scaling operation is
209    performed using integer fraction arithmetic in order to maximize
210    accuracy. */
211class GRectMapper
212{
213public:
214  /** Constructs a rectangle mapper. */
215  GRectMapper();
216  /** Resets the rectangle mapper state. Both the input rectangle
217      and the output rectangle are marked as undefined. */
218  void clear();
219  /** Sets the input rectangle. */
220  void set_input(const GRect &rect);
221  /** Returns the input rectangle. */
222  GRect get_input();
223  /** Sets the output rectangle. */
224  void set_output(const GRect &rect);
225  /** Returns the output rectangle. */
226  GRect get_output();
227  /** Composes the affine transform with a rotation of #count# quarter turns
228      counter-clockwise.  This operation essentially is a modification of the
229      match between the corners of the input rectangle and the corners of the
230      output rectangle. */
231  void rotate(int count=1);
232  /** Composes the affine transform with a symmetry with respect to the
233      vertical line crossing the center of the output rectangle.  This
234      operation essentially is a modification of the match between the corners
235      of the input rectangle and the corners of the output rectangle. */
236  void mirrorx();
237  /** Composes the affine transform with a symmetry with respect to the
238      horizontal line crossing the center of the output rectangle.  This
239      operation essentially is a modification of the match between the corners
240      of the input rectangle and the corners of the output rectangle. */
241  void mirrory();
242  /** Maps a point according to the affine transform.  Variables #x# and #y#
243      initially contain the coordinates of a point. This operation overwrites
244      these variables with the coordinates of a second point located in the
245      same position relative to the corners of the output rectangle as the
246      first point relative to the matching corners of the input rectangle.
247      Coordinates are rounded to the nearest integer. */
248  void map(int &x, int &y);
249  /** Maps a rectangle according to the affine transform. This operation
250      consists in mapping the rectangle corners and reordering the corners in
251      the canonical rectangle representation.  Variable #rect# is overwritten
252      with the new rectangle coordinates. */
253  void map(GRect &rect);
254  /** Maps a point according to the inverse of the affine transform.
255      Variables #x# and #y# initially contain the coordinates of a point. This
256      operation overwrites these variables with the coordinates of a second
257      point located in the same position relative to the corners of input
258      rectangle as the first point relative to the matching corners of the
259      input rectangle. Coordinates are rounded to the nearest integer. */
260  void unmap(int &x, int &y);
261  /** Maps a rectangle according to the inverse of the affine transform. This
262      operation consists in mapping the rectangle corners and reordering the
263      corners in the canonical rectangle representation.  Variable #rect# is
264      overwritten with the new rectangle coordinates. */
265  void unmap(GRect &rect);
266public:
267  // GRatio
268  struct GRatio {
269    GRatio ();
270    GRatio (int p, int q);
271    int p;
272    int q;
273  };
274private:
275  // Data
276  GRect rectFrom;
277  GRect rectTo;
278  int   code;
279  // Helper
280  void  precalc();
281  friend int operator*(int n, GRatio r ); 
282  friend int operator/(int n, GRatio r ); 
283  GRatio rw;
284  GRatio rh;
285};
286
287
288//@}
289
290
291
292// ---- INLINES
293
294inline
295GRect::GRect()
296: xmin(0), ymin(0), xmax(0), ymax(0)
297{
298}
299
300inline 
301GRect::GRect(int xmin, int ymin, unsigned int width, unsigned int height)
302: xmin(xmin), ymin(ymin), xmax(xmin+width), ymax(ymin+height)
303{
304}
305
306inline int 
307GRect::width() const
308{
309  return xmax - xmin;
310}
311
312inline int 
313GRect::height() const
314{
315  return ymax - ymin;
316}
317
318inline bool 
319GRect::isempty() const
320{
321  return (xmin>=xmax || ymin>=ymax);
322}
323
324inline int 
325GRect::area() const
326{
327  return isempty() ? 0 : (xmax-xmin)*(ymax-ymin);
328}
329
330inline int
331GRect::contains(int x, int y) const
332{
333  return (x>=xmin && x<xmax && y>=ymin && y<ymax);
334}
335 
336inline void 
337GRect::clear()
338{
339  xmin = xmax = ymin = ymax = 0;
340}
341
342inline int
343operator!=(const GRect & r1, const GRect & r2)
344{
345   return !(r1==r2);
346}
347
348// ---- THE END
349
350#ifdef HAVE_NAMESPACES
351}
352# ifndef NOT_USING_DJVU_NAMESPACE
353using namespace DJVU;
354# endif
355#endif
356#endif
Note: See TracBrowser for help on using the repository browser.