KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > common > ReaderUtil


1 /*
2  * @(#)ReaderUtil.java 1.1 05/11/16
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.common;
9
10 import java.awt.Point JavaDoc;
11 import java.awt.Rectangle JavaDoc;
12
13 /**
14  * This class contains utility methods that may be useful to ImageReader
15  * plugins. Ideally these methods would be in the ImageReader base class
16  * so that all subclasses could benefit from them, but that would be an
17  * addition to the existing API, and it is not yet clear whether these methods
18  * are universally useful, so for now we will leave them here.
19  */

20 public class ReaderUtil {
21
22     // Helper for computeUpdatedPixels method
23
private static void computeUpdatedPixels(int sourceOffset,
24                                              int sourceExtent,
25                                              int destinationOffset,
26                                              int dstMin,
27                                              int dstMax,
28                                              int sourceSubsampling,
29                                              int passStart,
30                                              int passExtent,
31                                              int passPeriod,
32                                              int[] vals,
33                                              int offset)
34     {
35         // We need to satisfy the congruences:
36
// dst = destinationOffset + (src - sourceOffset)/sourceSubsampling
37
//
38
// src - passStart == 0 (mod passPeriod)
39
// src - sourceOffset == 0 (mod sourceSubsampling)
40
//
41
// subject to the inequalities:
42
//
43
// src >= passStart
44
// src < passStart + passExtent
45
// src >= sourceOffset
46
// src < sourceOffset + sourceExtent
47
// dst >= dstMin
48
// dst <= dstmax
49
//
50
// where
51
//
52
// dst = destinationOffset + (src - sourceOffset)/sourceSubsampling
53
//
54
// For now we use a brute-force approach although we could
55
// attempt to analyze the congruences. If passPeriod and
56
// sourceSubsamling are relatively prime, the period will be
57
// their product. If they share a common factor, either the
58
// period will be equal to the larger value, or the sequences
59
// will be completely disjoint, depending on the relationship
60
// between passStart and sourceOffset. Since we only have to do this
61
// twice per image (once each for X and Y), it seems cheap enough
62
// to do it the straightforward way.
63

64         boolean gotPixel = false;
65         int firstDst = -1;
66         int secondDst = -1;
67         int lastDst = -1;
68
69         for (int i = 0; i < passExtent; i++) {
70             int src = passStart + i*passPeriod;
71             if (src < sourceOffset) {
72                 continue;
73             }
74             if ((src - sourceOffset) % sourceSubsampling != 0) {
75                 continue;
76             }
77             if (src >= sourceOffset + sourceExtent) {
78                 break;
79             }
80
81             int dst = destinationOffset +
82                 (src - sourceOffset)/sourceSubsampling;
83             if (dst < dstMin) {
84                 continue;
85             }
86             if (dst > dstMax) {
87                 break;
88             }
89
90             if (!gotPixel) {
91                 firstDst = dst; // Record smallest valid pixel
92
gotPixel = true;
93             } else if (secondDst == -1) {
94                 secondDst = dst; // Record second smallest valid pixel
95
}
96             lastDst = dst; // Record largest valid pixel
97
}
98
99         vals[offset] = firstDst;
100
101         // If we never saw a valid pixel, set width to 0
102
if (!gotPixel) {
103             vals[offset + 2] = 0;
104         } else {
105             vals[offset + 2] = lastDst - firstDst + 1;
106         }
107
108         // The period is given by the difference of any two adjacent pixels
109
vals[offset + 4] = Math.max(secondDst - firstDst, 1);
110     }
111
112     /**
113      * A utility method that computes the exact set of destination
114      * pixels that will be written during a particular decoding pass.
115      * The intent is to simplify the work done by readers in combining
116      * the source region, source subsampling, and destination offset
117      * information obtained from the <code>ImageReadParam</code> with
118      * the offsets and periods of a progressive or interlaced decoding
119      * pass.
120      *
121      * @param sourceRegion a <code>Rectangle</code> containing the
122      * source region being read, offset by the source subsampling
123      * offsets, and clipped against the source bounds, as returned by
124      * the <code>getSourceRegion</code> method.
125      * @param destinationOffset a <code>Point</code> containing the
126      * coordinates of the upper-left pixel to be written in the
127      * destination.
128      * @param dstMinX the smallest X coordinate (inclusive) of the
129      * destination <code>Raster</code>.
130      * @param dstMinY the smallest Y coordinate (inclusive) of the
131      * destination <code>Raster</code>.
132      * @param dstMaxX the largest X coordinate (inclusive) of the destination
133      * <code>Raster</code>.
134      * @param dstMaxY the largest Y coordinate (inclusive) of the destination
135      * <code>Raster</code>.
136      * @param sourceXSubsampling the X subsampling factor.
137      * @param sourceYSubsampling the Y subsampling factor.
138      * @param passXStart the smallest source X coordinate (inclusive)
139      * of the current progressive pass.
140      * @param passYStart the smallest source Y coordinate (inclusive)
141      * of the current progressive pass.
142      * @param passWidth the width in pixels of the current progressive
143      * pass.
144      * @param passHeight the height in pixels of the current progressive
145      * pass.
146      * @param passPeriodX the X period (horizontal spacing between
147      * pixels) of the current progressive pass.
148      * @param passPeriodY the Y period (vertical spacing between
149      * pixels) of the current progressive pass.
150      *
151      * @return an array of 6 <code>int</code>s containing the
152      * destination min X, min Y, width, height, X period and Y period
153      * of the region that will be updated.
154      */

155     public static int[] computeUpdatedPixels(Rectangle JavaDoc sourceRegion,
156                                              Point JavaDoc destinationOffset,
157                                              int dstMinX,
158                                              int dstMinY,
159                                              int dstMaxX,
160                                              int dstMaxY,
161                                              int sourceXSubsampling,
162                                              int sourceYSubsampling,
163                                              int passXStart,
164                                              int passYStart,
165                                              int passWidth,
166                                              int passHeight,
167                                              int passPeriodX,
168                                              int passPeriodY)
169     {
170         int[] vals = new int[6];
171         computeUpdatedPixels(sourceRegion.x, sourceRegion.width,
172                              destinationOffset.x,
173                              dstMinX, dstMaxX, sourceXSubsampling,
174                              passXStart, passWidth, passPeriodX,
175                              vals, 0);
176         computeUpdatedPixels(sourceRegion.y, sourceRegion.height,
177                              destinationOffset.y,
178                              dstMinY, dstMaxY, sourceYSubsampling,
179                              passYStart, passHeight, passPeriodY,
180                              vals, 1);
181         return vals;
182     }
183 }
184
Popular Tags