All Downloads are FREE. Search and download functionalities are using the official Maven repository.

boofcv.io.wrapper.images.JpegByteImageSequence Maven / Gradle / Ivy

Go to download

BoofCV is an open source Java library for real-time computer vision and robotics applications.

There is a newer version: 1.1.7
Show newest version
/*
 * Copyright (c) 2021, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.io.wrapper.images;

import boofcv.io.image.ConvertBufferedImage;
import boofcv.io.image.SimpleImageSequence;
import boofcv.struct.image.ImageBase;
import boofcv.struct.image.ImageType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Create a sequence from an array of jpeg images in byte[] array format. Each image is decompressed
 * as need
 *
 * @author Peter Abeles
 */
@SuppressWarnings({"NullAway.Init"})
public class JpegByteImageSequence> implements SimpleImageSequence {

	int index;
	List jpegData = new ArrayList<>();

	// type of image it outputs
	ImageType imageType;

	BufferedImage imageGUI;
	T output;

	BufferedImage imageNext;

	// loop back and forth in the sequence
	boolean loop = false;
	// is it traversing in the forwards or backwards direction
	boolean forward = true;

	public JpegByteImageSequence(ImageType imageType, List jpegData, boolean loop) {
		this.imageType = imageType;
		this.jpegData = jpegData;
		this.loop = loop;

		output = imageType.createImage(1,1);
		loadNext();
	}

	public JpegByteImageSequence(Class imageType, List jpegData, boolean loop) {
		this(ImageType.single((Class)imageType), jpegData,loop);
	}

	@Override
	public int getWidth() {
		return imageNext.getWidth();
	}

	@Override
	public int getHeight() {
		return imageNext.getHeight();
	}

	@Override
	public boolean hasNext() {
		return loop || index < jpegData.size();
	}

	@Override
	public T next() {
		imageGUI = imageNext;
		output.reshape(imageGUI.getWidth(),imageGUI.getHeight());
		ConvertBufferedImage.convertFrom(imageGUI, output, true);

		if(forward) {
			index++;
			if( loop && index >= jpegData.size() ) {
				index = jpegData.size()-1;
				forward = false;
			}
		} else {
			index--;
			if( loop && index < 0) {
				index = 1;
				forward = true;
			}
		}
		if( hasNext())
			loadNext();
		return getImage();
	}

	@Override
	public T getImage() {
		return output;
	}

	private void loadNext() {
		try {
			imageNext = ImageIO.read(new ByteArrayInputStream(jpegData.get(index)));
		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}

	@Override
	public void setLoop(boolean loop) {
		this.loop = loop;
	}

	@Override
	public BufferedImage getGuiImage() {
		return imageGUI;
	}

	@Override
	public void close() {
	}

	@Override
	public int getFrameNumber() {
		return index;
	}

	@Override
	public ImageType getImageType() {
		return imageType;
	}

	@Override
	public void reset() {
		index = 0;
		forward = true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy