001/*
002 * $RCSfile: ImgReader.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:14 $
005 * $State: Exp $
006 *
007 * Class:                   ImgReader
008 *
009 * Description:             Generic interface for image readers (from
010 *                          file or other resource)
011 *
012 *
013 *
014 * COPYRIGHT:
015 *
016 * This software module was originally developed by Raphaël Grosbois and
017 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
018 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
019 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
020 * Centre France S.A) in the course of development of the JPEG2000
021 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
022 * software module is an implementation of a part of the JPEG 2000
023 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
024 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
025 * Partners) agree not to assert against ISO/IEC and users of the JPEG
026 * 2000 Standard (Users) any of their rights under the copyright, not
027 * including other intellectual property rights, for this software module
028 * with respect to the usage by ISO/IEC and Users of this software module
029 * or modifications thereof for use in hardware or software products
030 * claiming conformance to the JPEG 2000 Standard. Those intending to use
031 * this software module in hardware or software products are advised that
032 * their use may infringe existing patents. The original developers of
033 * this software module, JJ2000 Partners and ISO/IEC assume no liability
034 * for use of this software module or modifications thereof. No license
035 * or right to this software module is granted for non JPEG 2000 Standard
036 * conforming products. JJ2000 Partners have full right to use this
037 * software module for his/her own purpose, assign or donate this
038 * software module to any third party and to inhibit third parties from
039 * using this software module for non JPEG 2000 Standard conforming
040 * products. This copyright notice must be included in all copies or
041 * derivative works of this software module.
042 *
043 * Copyright (c) 1999/2000 JJ2000 Partners.
044 */
045package jj2000.j2k.image.input;
046
047import jj2000.j2k.image.*;
048import jj2000.j2k.*;
049import java.io.*;
050
051import java.awt.Point;
052
053/**
054 * This is the generic interface to be implemented by all image file (or other
055 * resource) readers for different image file formats.
056 *
057 * <p>An ImgReader behaves as an ImgData object. Whenever image data is
058 * requested through the getInternCompData() or getCompData() methods, the
059 * image data will be read (if it is not buffered) and returned. Implementing
060 * classes should not buffer large amounts of data, so as to reduce memory
061 * usage.</p>
062 *
063 * <p>This class sets the image origin to (0,0). All default implementations
064 * of the methods assume this.</p>
065 *
066 * <p>This class provides default implementations of many methods. These
067 * default implementations assume that there is no tiling (i.e., the only tile
068 * is the entire image), that the image origin is (0,0) in the canvas system
069 * and that there is no component subsampling (all components are the same
070 * size), but they can be overloaded by the implementating class if need
071 * be.</p>
072 * */
073public abstract class ImgReader implements BlkImgDataSrc {
074
075    /** The width of the image */
076    protected int w;
077
078    /** The height of the image */
079    protected int h;
080
081    /** The number of components in the image */
082    protected int nc;
083
084    /**
085     * Closes the underlying file or network connection from where the
086     * image data is being read.
087     *
088     * @exception IOException If an I/O error occurs.
089     */
090    public abstract void close() throws IOException;
091
092    /**
093     * Returns the width of the current tile in pixels, assuming there is
094     * no-tiling. Since no-tiling is assumed this is the same as the width of
095     * the image. The value of <tt>w</tt> is returned.
096     *
097     * @return The total image width in pixels.
098     * */
099    public int getTileWidth() {
100        return w;
101    }
102
103    /**
104     * Returns the overall height of the current tile in pixels, assuming
105     * there is no-tiling. Since no-tiling is assumed this is the same as the
106     * width of the image. The value of <tt>h</tt> is returned.
107     *
108     * @return The total image height in pixels.  */
109    public int getTileHeight() {
110        return h;
111    }
112
113    /** Returns the nominal tiles width */
114    public int getNomTileWidth() {
115        return w;
116    }
117
118    /** Returns the nominal tiles height */
119    public int getNomTileHeight() {
120        return h;
121    }
122
123    /**
124     * Returns the overall width of the image in pixels. This is the image's
125     * width without accounting for any component subsampling or tiling. The
126     * value of <tt>w</tt> is returned.
127     *
128     * @return The total image's width in pixels.
129     * */
130    public int getImgWidth() {
131        return w;
132    }
133
134    /**
135     * Returns the overall height of the image in pixels. This is the image's
136     * height without accounting for any component subsampling or tiling. The
137     * value of <tt>h</tt> is returned.
138     *
139     * @return The total image's height in pixels.
140     * */
141    public int getImgHeight() {
142        return h;
143    }
144
145    /**
146     * Returns the number of components in the image. The value of <tt>nc</tt>
147     * is returned.
148     *
149     * @return The number of components in the image.
150     * */
151    public int getNumComps() {
152        return nc;
153    }
154
155    /**
156     * Returns the component subsampling factor in the horizontal direction,
157     * for the specified component. This is, approximately, the ratio of
158     * dimensions between the reference grid and the component itself, see the
159     * 'ImgData' interface desription for details.
160     *
161     * @param c The index of the component (between 0 and C-1)
162     *
163     * @return The horizontal subsampling factor of component 'c'
164     *
165     * @see jj2000.j2k.image.ImgData
166     * */
167    public int getCompSubsX(int c) {
168        return 1;
169    }
170
171    /**
172     * Returns the component subsampling factor in the vertical direction, for
173     * the specified component. This is, approximately, the ratio of
174     * dimensions between the reference grid and the component itself, see the
175     * 'ImgData' interface desription for details.
176     *
177     * @param c The index of the component (between 0 and C-1)
178     *
179     * @return The vertical subsampling factor of component 'c'
180     *
181     * @see jj2000.j2k.image.ImgData
182     * */
183    public int getCompSubsY(int c) {
184        return 1;
185    }
186
187    /**
188     * Returns the width in pixels of the specified tile-component. This
189     * default implementation assumes no tiling and no component subsampling
190     * (i.e., all components, or components, have the same dimensions in
191     * pixels).
192     *
193     * @param t Tile index
194     *
195     * @param c The index of the component, from 0 to C-1.
196     *
197     * @return The width in pixels of component <tt>c</tt> in tile<tt>t</tt>.
198     * */
199    public int getTileCompWidth(int t,int c) {
200        if(t!=0) {
201            throw new Error("Asking a tile-component width for a tile index"+
202                            " greater than 0 whereas there is only one tile");
203        }
204        return w;
205    }
206
207    /**
208     * Returns the height in pixels of the specified tile-component. This
209     * default implementation assumes no tiling and no component subsampling
210     * (i.e., all components, or components, have the same dimensions in
211     * pixels).
212     *
213     * @param t The tile index
214     *
215     * @param c The index of the component, from 0 to C-1.
216     *
217     * @return The height in pixels of component <tt>c</tt> in tile
218     * <tt>t</tt>.
219     * */
220    public int getTileCompHeight(int t,int c) {
221        if(t!=0) {
222            throw new Error("Asking a tile-component width for a tile index"+
223                            " greater than 0 whereas there is only one tile");
224        }
225        return h;
226    }
227
228    /**
229     * Returns the width in pixels of the specified component in the overall
230     * image. This default implementation assumes no component, or component,
231     * subsampling (i.e. all components have the same dimensions in pixels).
232     *
233     * @param c The index of the component, from 0 to C-1.
234     *
235     * @return The width in pixels of component <tt>c</tt> in the overall
236     * image.
237     * */
238    public int getCompImgWidth(int c) {
239        return w;
240    }
241
242    /**
243     * Returns the height in pixels of the specified component in the overall
244     * image. This default implementation assumes no component, or component,
245     * subsampling (i.e. all components have the same dimensions in pixels).
246     *
247     * @param c The index of the component, from 0 to C-1.
248     *
249     * @return The height in pixels of component <tt>c</tt> in the overall
250     * image.
251     * */
252    public int getCompImgHeight(int c) {
253        return h;
254    }
255
256    /**
257     * Changes the current tile, given the new coordinates. An
258     * IllegalArgumentException is thrown if the coordinates do not correspond
259     * to a valid tile. This default implementation assumes no tiling so the
260     * only valid arguments are x=0, y=0.
261     *
262     * @param x The horizontal coordinate of the tile.
263     *
264     * @param y The vertical coordinate of the new tile.
265     * */
266    public void setTile(int x, int y) {
267        if (x!=0 || y != 0) {
268            throw new IllegalArgumentException();
269        }
270    }
271
272    /**
273     * Advances to the next tile, in standard scan-line order (by rows then
274     * columns). A NoNextElementException is thrown if the current tile is the
275     * last one (i.e. there is no next tile). This default implementation
276     * assumes no tiling, so NoNextElementException() is always thrown.
277     * */
278    public void nextTile() {
279        throw new NoNextElementException();
280    }
281
282    /**
283     * Returns the coordinates of the current tile. This default
284     * implementation assumes no-tiling, so (0,0) is returned.
285     *
286     * @param co If not null this object is used to return the information. If
287     * null a new one is created and returned.
288     *
289     * @return The current tile's coordinates.
290     * */
291    public Point getTile(Point co) {
292        if (co != null) {
293            co.x = 0;
294            co.y = 0;
295            return co;
296        }
297        else {
298            return new Point(0,0);
299        }
300    }
301
302    /**
303     * Returns the index of the current tile, relative to a standard scan-line
304     * order. This default implementations assumes no tiling, so 0 is always
305     * returned.
306     *
307     * @return The current tile's index (starts at 0).
308     * */
309    public int getTileIdx() {
310        return 0;
311    }
312
313    /**
314     * Returns the horizontal coordinate of the upper-left corner of the
315     * specified component in the current tile.
316     *
317     * @param c The component index.
318     * */
319    public int getCompULX(int c) {
320        return 0;
321    }
322
323    /**
324     * Returns the vertical coordinate of the upper-left corner of the
325     * specified component in the current tile.
326     *
327     * @param c The component index.
328     * */
329    public int getCompULY(int c) {
330        return 0;
331    }
332
333    /** Returns the horizontal tile partition offset in the reference grid */
334    public int getTilePartULX() {
335        return 0;
336    }
337
338    /** Returns the vertical tile partition offset in the reference grid */
339    public int getTilePartULY() {
340        return 0;
341    }
342
343    /**
344     * Returns the horizontal coordinate of the image origin, the top-left
345     * corner, in the canvas system, on the reference grid.
346     *
347     * @return The horizontal coordinate of the image origin in the canvas
348     * system, on the reference grid.
349     * */
350    public int getImgULX() {
351        return 0;
352    }
353
354    /**
355     * Returns the vertical coordinate of the image origin, the top-left
356     * corner, in the canvas system, on the reference grid.
357     *
358     * @return The vertical coordinate of the image origin in the canvas
359     * system, on the reference grid.
360     * */
361    public int getImgULY() {
362        return 0;
363    }
364
365    /**
366     * Returns the number of tiles in the horizontal and vertical
367     * directions. This default implementation assumes no tiling, so (1,1) is
368     * always returned.
369     *
370     * @param co If not null this object is used to return the information. If
371     * null a new one is created and returned.
372     *
373     * @return The number of tiles in the horizontal (Point.x) and vertical
374     * (Point.y) directions.
375     * */
376    public Point getNumTiles(Point co) {
377        if (co != null) {
378            co.x = 1;
379            co.y = 1;
380            return co;
381        }
382        else {
383            return new Point(1,1);
384        }
385    }
386
387    /**
388     * Returns the total number of tiles in the image. This default
389     * implementation assumes no tiling, so 1 is always returned.
390     *
391     * @return The total number of tiles in the image.
392     * */
393    public int getNumTiles() {
394        return 1;
395    }
396
397    /**
398     * Returns true if the data read was originally signed in the specified
399     * component, false if not.
400     *
401     * @param c The index of the component, from 0 to C-1.
402     *
403     * @return true if the data was originally signed, false if not.
404     * */
405    public abstract boolean isOrigSigned(int c);
406
407}