/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * 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 com.badlogic.gdx.graphics.glutils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class FileTextureData implements TextureData {
	static public boolean copyToPOT;

	final FileHandle file;
	int width = 0;
	int height = 0;
	Format format;
	Pixmap pixmap;
	boolean useMipMaps;
	boolean isPrepared = false;

	public FileTextureData (FileHandle file, Pixmap preloadedPixmap, Format format, boolean useMipMaps) {
		this.file = file;
		this.pixmap = preloadedPixmap;
		this.format = format;
		this.useMipMaps = useMipMaps;
		if (pixmap != null) {
			pixmap = ensurePot(pixmap);
			width = pixmap.getWidth();
			height = pixmap.getHeight();
			if (format == null) this.format = pixmap.getFormat();
		}
	}

	@Override
	public boolean isPrepared () {
		return isPrepared;
	}

	@Override
	public void prepare () {
		if (isPrepared) throw new GdxRuntimeException("Already prepared");
		if (pixmap == null) {
			if (file.extension().equals("cim"))
				pixmap = PixmapIO.readCIM(file);
			else
				pixmap = ensurePot(new Pixmap(file));
			width = pixmap.getWidth();
			height = pixmap.getHeight();
			if (format == null) format = pixmap.getFormat();
		}
		isPrepared = true;
	}

	private Pixmap ensurePot (Pixmap pixmap) {
		if (Gdx.gl20 == null && copyToPOT) {
			int pixmapWidth = pixmap.getWidth();
			int pixmapHeight = pixmap.getHeight();
			int potWidth = MathUtils.nextPowerOfTwo(pixmapWidth);
			int potHeight = MathUtils.nextPowerOfTwo(pixmapHeight);
			if (pixmapWidth != potWidth || pixmapHeight != potHeight) {
				Pixmap tmp = new Pixmap(potWidth, potHeight, pixmap.getFormat());
				tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmapWidth, pixmapHeight);
				pixmap.dispose();
				return tmp;
			}
		}
		return pixmap;
	}

	@Override
	public Pixmap consumePixmap () {
		if (!isPrepared) throw new GdxRuntimeException("Call prepare() before calling getPixmap()");
		isPrepared = false;
		Pixmap pixmap = this.pixmap;
		this.pixmap = null;
		return pixmap;
	}

	@Override
	public boolean disposePixmap () {
		return true;
	}

	@Override
	public int getWidth () {
		return width;
	}

	@Override
	public int getHeight () {
		return height;
	}

	@Override
	public Format getFormat () {
		return format;
	}

	@Override
	public boolean useMipMaps () {
		return useMipMaps;
	}

	@Override
	public boolean isManaged () {
		return true;
	}

	public FileHandle getFileHandle () {
		return file;
	}

	@Override
	public TextureDataType getType () {
		return TextureDataType.Pixmap;
	}

	@Override
	public void consumeCustomData (int target) {
		throw new GdxRuntimeException("This TextureData implementation does not upload data itself");
	}
}

@@ -1221,43 +1221,8 @@
 a %7B%0D
-%0A%09static public boolean copyToPOT;%0D
 %0A%0D%0A%09
@@ -1614,40 +1614,8 @@
  %7B%0D%0A
-%09%09%09pixmap = ensurePot(pixmap);%0D%0A
 %09%09%09w
@@ -2054,18 +2054,8 @@
 p = 
-ensurePot(
 new 
@@ -2066,17 +2066,16 @@
 ap(file)
-)
 ;%0D%0A%09%09%09wi
@@ -2191,16 +2191,16 @@
 ;%0D%0A%09%09%7D%0D%0A
+
 %09%09isPrep
@@ -2223,573 +2223,8 @@
 %0D%0A%0D%0A
-%09private Pixmap ensurePot (Pixmap pixmap) %7B%0D%0A%09%09if (Gdx.gl20 == null && copyToPOT) %7B%0D%0A%09%09%09int pixmapWidth = pixmap.getWidth();%0D%0A%09%09%09int pixmapHeight = pixmap.getHeight();%0D%0A%09%09%09int potWidth = MathUtils.nextPowerOfTwo(pixmapWidth);%0D%0A%09%09%09int potHeight = MathUtils.nextPowerOfTwo(pixmapHeight);%0D%0A%09%09%09if (pixmapWidth != potWidth %7C%7C pixmapHeight != potHeight) %7B%0D%0A%09%09%09%09Pixmap tmp = new Pixmap(potWidth, potHeight, pixmap.getFormat());%0D%0A%09%09%09%09tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmapWidth, pixmapHeight);%0D%0A%09%09%09%09pixmap.dispose();%0D%0A%09%09%09%09return tmp;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return pixmap;%0D%0A%09%7D%0D%0A%0D%0A
 %09@Ov

Remove obsolete code (#4832)