source: trunk/libdjvu/GRect.h @ 269

Last change on this file since 269 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

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