Skip to content

Commit

Permalink
Add durationPerFrame to Frameset and getFrameBoundsAt method
Browse files Browse the repository at this point in the history
  • Loading branch information
vkay94 committed Jan 6, 2021
1 parent fae67fb commit 3d9fbc8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ public List<Frameset> getFrames() throws ExtractionException {

for (int i = 1; i < spec.length; ++i) {
final String[] parts = spec[i].split("#");
if (parts.length != 8) {
if (parts.length != 8 || Integer.parseInt(parts[5]) == 0) {
continue;
}
final int frameWidth = Integer.parseInt(parts[0]);
Expand All @@ -1016,6 +1016,7 @@ public List<Frameset> getFrames() throws ExtractionException {
frameWidth,
frameHeight,
totalCount,
Integer.parseInt(parts[5]),
framesPerPageX,
framesPerPageY
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public final class Frameset {
private int frameWidth;
private int frameHeight;
private int totalCount;
private int durationPerFrame;
private int framesPerPageX;
private int framesPerPageY;

public Frameset(List<String> urls, int frameWidth, int frameHeight, int totalCount, int framesPerPageX, int framesPerPageY) {
public Frameset(List<String> urls, int frameWidth, int frameHeight, int totalCount, int durationPerFrame, int framesPerPageX, int framesPerPageY) {
this.urls = urls;
this.totalCount = totalCount;
this.durationPerFrame = durationPerFrame;
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.framesPerPageX = framesPerPageX;
Expand Down Expand Up @@ -61,4 +63,48 @@ public int getFrameWidth() {
public int getFrameHeight() {
return frameHeight;
}

/**
* @return duration per frame in millis
*/
public int getDurationPerFrame() {
return durationPerFrame;
}

/**
* Returns the information for the frame at stream position.
*
* @param position Position of the stream
* @return An <code>int</code>-array containing the bounds and url where the indexes are specified as
* followed:
*
* <ul>
* <li><code>0</code>: Index of the url</li>
* <li><code>1</code>: Left bound</li>
* <li><code>2</code>: Top bound</li>
* <li><code>3</code>: Right bound</li>
* <li><code>4</code>: Bottom bound</li>
* </ul>
*/
public int[] getFrameBoundsAt(long position) {
if (position < 0 || position > ((totalCount + 1) * durationPerFrame)) {
// Return the first frame as fallback
return new int[] { 0, 0, 0, frameWidth, frameHeight };
}

final int framesPerStoryboard = framesPerPageX * framesPerPageY;
final int absoluteFrameNumber = Math.min((int) (position / durationPerFrame), totalCount);

final int relativeFrameNumber = absoluteFrameNumber % framesPerStoryboard;

final int rowIndex = Math.floorDiv(relativeFrameNumber, framesPerPageX);
final int columnIndex = relativeFrameNumber % framesPerPageY;

return new int[] {
/* storyboardIndex */ Math.floorDiv(absoluteFrameNumber, framesPerStoryboard),
/* left */ columnIndex * frameWidth,
/* top */ rowIndex * frameHeight,
/* right */ columnIndex * frameWidth + frameWidth,
/* bottom */ rowIndex * frameHeight + frameHeight };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ public void testFrames() throws Exception {
assertIsValidUrl(url);
assertIsSecureUrl(url);
}
assertTrue(f.getDurationPerFrame() > 0);
assertEquals(f.getFrameBoundsAt(0)[3], f.getFrameWidth());
}
} else {
assertTrue(frames.isEmpty());
Expand Down

0 comments on commit 3d9fbc8

Please sign in to comment.