Java Code Examples for com.badlogic.gdx.graphics.g2d.TextureAtlas#TextureAtlasData

The following examples show how to use com.badlogic.gdx.graphics.g2d.TextureAtlas#TextureAtlasData . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageAmountMetadataProcessor.java    From gdx-texture-packer-gui with Apache License 2.0 6 votes vote down vote up
@Override
public void processPackage(PackProcessingNode node) throws Exception {
    PackModel pack = node.getPack();
    ProjectModel project = node.getProject();

    FileHandle packFileHandle = Gdx.files.absolute(pack.getOutputDir()).child(pack.getCanonicalFilename());
    FileHandle imagesDirFileHandle = Gdx.files.absolute(pack.getOutputDir());
    TextureAtlas.TextureAtlasData atlasData = new TextureAtlas.TextureAtlasData(
            packFileHandle,
            imagesDirFileHandle,
            false);

    int pageAmount = atlasData.getPages().size;
    node.addMetadata(PackProcessingNode.META_ATLAS_PAGES, pageAmount);
    System.out.println(pageAmount + " pages");
}
 
Example 2
Source File: FileSizeMetadataProcessor.java    From gdx-texture-packer-gui with Apache License 2.0 6 votes vote down vote up
@Override
public void processPackage(PackProcessingNode node) throws Exception {
    PackModel pack = node.getPack();
    ProjectModel project = node.getProject();

    FileHandle packFileHandle = Gdx.files.absolute(pack.getOutputDir()).child(pack.getCanonicalFilename());
    FileHandle imagesDirFileHandle = Gdx.files.absolute(pack.getOutputDir());
    TextureAtlas.TextureAtlasData atlasData = new TextureAtlas.TextureAtlasData(
            packFileHandle,
            imagesDirFileHandle,
            false);

    long totalSize = 0; // Bytes
    totalSize += packFileHandle.length();
    for (TextureAtlas.TextureAtlasData.Page page : atlasData.getPages()) {
        long pageSize = page.textureFile.length();
        totalSize += pageSize;
    }
    node.addMetadata(PackProcessingNode.META_FILE_SIZE, totalSize);

    System.out.println("Total pack files size is " + totalSize + " bytes");
}
 
Example 3
Source File: TextureUnpackerDialogController.java    From gdx-texture-packer-gui with Apache License 2.0 6 votes vote down vote up
@LmlAction("launchUnpackProcess") void launchUnpackProcess() {
    try {
        Gdx.app.log(TAG, "Start texture unpacking");

        FileHandle outputDir = Gdx.files.absolute(edtOutputDir.getText());
        FileHandle atlasFile = FileUtils.obtainIfExists(edtAtlasPath.getText());
        if (atlasFile == null) throw new IllegalStateException("Atlas file does not exist: " + edtAtlasPath.getText());

        TextureAtlas.TextureAtlasData atlasData = new TextureAtlas.TextureAtlasData(atlasFile, atlasFile.parent(), false);

        TextureUnpacker textureUnpacker = new TextureUnpacker();
        textureUnpacker.splitAtlas(atlasData, outputDir.path());

        Gdx.app.log(TAG, "Texture unpacking successfully finished");
        showSuccessfulDialog(outputDir);
    } catch (Exception e) {
        Gdx.app.log(TAG, "Texture unpacking failed with error", e);
        showErrorDialog(e);
    }
}
 
Example 4
Source File: TinifyCompressingProcessor.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
@Override
public void processPackage(PackProcessingNode node) throws Exception {
    PackModel pack = node.getPack();
    ProjectModel project = node.getProject();

    if (project.getFileType().getClass() != PngFileTypeModel.class) return;

    PngFileTypeModel fileType = (PngFileTypeModel) project.getFileType();

    if (fileType.getCompression() == null || fileType.getCompression().getType() != PngCompressionType.TINY_PNG) return;

    System.out.println("Tinify compression started");

    String apiKey = tinifyService.getApiKey();
    if (apiKey == null || apiKey.trim().isEmpty()) {
        throw new IllegalStateException("An API key is not specified. Please go to TinyPNG/TinyJPG compression settings and set up the API key.");
    }

    TextureAtlas.TextureAtlasData atlasData = new TextureAtlas.TextureAtlasData(
                    Gdx.files.absolute(pack.getOutputDir()).child(pack.getCanonicalFilename()),
                    Gdx.files.absolute(pack.getOutputDir()), false);

    float compressionRateSum = 0f;
    for (TextureAtlas.TextureAtlasData.Page page : atlasData.getPages()) {
        long preCompressedSize = page.textureFile.length();

        tinifyService.compressImageSync(page.textureFile);

        long postCompressedSize = page.textureFile.length();
        float pageCompression = ((postCompressedSize-preCompressedSize) / (float)preCompressedSize);
        compressionRateSum += pageCompression;

        System.out.println(String.format(Locale.US, "%s compressed for %+.2f%%", page.textureFile.name(), pageCompression*100f));
    }
    node.addMetadata(PackProcessingNode.META_COMPRESSION_RATE, compressionRateSum / atlasData.getPages().size);

    System.out.println("Tinify compression finished");
}
 
Example 5
Source File: ZopfliCompressingProcessor.java    From gdx-texture-packer-gui with Apache License 2.0 4 votes vote down vote up
@Override
public void processPackage(PackProcessingNode node) throws Exception {
    PackModel pack = node.getPack();
    ProjectModel project = node.getProject();

    if (project.getFileType().getClass() != PngFileTypeModel.class) return;

    PngFileTypeModel fileType = (PngFileTypeModel) project.getFileType();

    if (fileType.getCompression() == null || fileType.getCompression().getType() != PngCompressionType.ZOPFLI) return;

    System.out.println("Zopfli compression started");

    ZopfliCompressionModel compModel = fileType.getCompression();
    PngOptimizer pngOptimizer = new PngOptimizer(LOG_LEVEL);
    pngOptimizer.setCompressor("zopfli", compModel.getIterations());

    // Compression section
    {
        TextureAtlas.TextureAtlasData atlasData = new TextureAtlas.TextureAtlasData(
                        Gdx.files.absolute(pack.getOutputDir()).child(pack.getCanonicalFilename()),
                        Gdx.files.absolute(pack.getOutputDir()), false);

        for (TextureAtlas.TextureAtlasData.Page page : atlasData.getPages()) {
            PngImage image = new PngImage(page.textureFile.file().getAbsolutePath(), LOG_LEVEL);
            pngOptimizer.optimize(
                    image,
                    page.textureFile.file().getAbsolutePath(),
                    false,
                    compModel.getLevel());
        }
    }

    // Compute compression rate for metadata
    {
        float compressionRate = 0f;
        for (PngOptimizer.OptimizerResult optimizerResult : pngOptimizer.getResults()) {
            float localCompressionRate = (optimizerResult.getOptimizedFileSize() - optimizerResult.getOriginalFileSize()) / (float) optimizerResult.getOriginalFileSize();
            compressionRate += localCompressionRate / pngOptimizer.getResults().size();
        }
        node.addMetadata(PackProcessingNode.META_COMPRESSION_RATE, compressionRate);
    }

    System.out.println("Zopfli compression finished");
}