<commit_before>/*
 * Image/J Plugins
 * Copyright (C) 2002-2010 <NAME>
 * Author's email: jsacha at users dot sourceforge dot net
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Latest release available at http://sourceforge.net/projects/ij-plugins/
 */

package net.sf.ij_plugins.io.vtk;

import ij.IJ;
import ij.ImagePlus;
import ij.io.ImageWriter;
import ij.measure.Calibration;

import java.io.*;


/**
 * Save image in <a HREF="http://public.kitware.com/VTK/">VTK</a> format. Supported image types:
 * GRAY8, GRAY16, GRAY32.
 *
 * @author <NAME>
 */

public final class VtkEncoder {

    private static final String TAG_SEPARATOR = " ";
    private static final String VTK_FILE_VERSION = "3.0";


    private VtkEncoder() {

    }


    private static String createHeader(final ImagePlus imp, final boolean asciiFormat) {

        final StringBuffer header = new StringBuffer();
        header.append(VtkTag.DATA_FILE_VERSION).append(VTK_FILE_VERSION).append("\n");

        header.append(imp.getTitle()).append("\n");

        header.append(asciiFormat ? VtkDataFormat.ASCII : VtkDataFormat.BINARY);
        header.append("\n");

        header.append(VtkTag.DATASET).append(TAG_SEPARATOR).append(VtkDataSetType.STRUCTURED_POINTS).append("\n");

        final int width = imp.getWidth();
        final int height = imp.getHeight();
        final int depth = imp.getStackSize();
        header.append(VtkTag.DIMENSIONS).append(TAG_SEPARATOR).append(width).append(" ")
                .append(height).append(" ").append(depth).append("\n");

        final Calibration c = imp.getCalibration();
        header.append(VtkTag.SPACING).append(TAG_SEPARATOR).append(c.pixelWidth).append(" ")
                .append(c.pixelHeight).append(" ").append(c.pixelDepth).append("\n");

        header.append(VtkTag.ORIGIN).append(TAG_SEPARATOR).append(c.xOrigin).append(" ")
                .append(c.yOrigin).append(" ").append(c.zOrigin).append("\n");

        header.append(VtkTag.POINT_DATA).append(TAG_SEPARATOR).append(width * height * depth)
                .append("\n");

        final String scalarName;
        switch (imp.getType()) {
            case ImagePlus.GRAY8:
                scalarName = VtkScalarType.UNSIGNED_CHAR.toString();
                break;
            case ImagePlus.GRAY16:
                scalarName = VtkScalarType.UNSIGNED_SHORT.toString();
                break;
            case ImagePlus.GRAY32:
                scalarName = VtkScalarType.FLOAT.toString();
                break;
            // BEG KEESH RGB UPDATE
            case ImagePlus.COLOR_RGB:
                scalarName = VtkScalarType.UNSIGNED_CHAR.toString();
                break;
            // END KEESH RGB UPDATE
            default:
                throw new IllegalArgumentException("Unsupported image type. "
                        + "Only images of types: GRAY8, GRAY16, and GRAY32 are supported.");
        }
        // BEG KEESH RGB UPDATE
        if (imp.getType() != ImagePlus.COLOR_RGB) {
            header.append(VtkTag.SCALARS).append(TAG_SEPARATOR).append("volume_scalars ")
                    .append(scalarName).append(" 1\n");
        } else {
            header.append(VtkTag.COLOR_SCALARS).append(TAG_SEPARATOR).append("volume_scalars ")
                    .append("3\n");  // no scalar name as per VTK spec
        }
        // Writing LUT tag for RGB images incorrectly offsets image data
        if (imp.getType() != ImagePlus.COLOR_RGB) {
            header.append(VtkTag.LOOKUP_TABLE).append(TAG_SEPARATOR).append("default\n");
        }
        // END KEESH RGB UPDATE

        return header.toString();
    }


    private static void saveAsVtkBinary(final String fileName, final ImagePlus imp) throws IOException {

        final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
        try {
            final String header = createHeader(imp, false);
            bos.write(header.getBytes());

            final ImageWriter imageWriter = new ImageWriter(imp.getFileInfo());
            imageWriter.write(bos);
        } finally {
            bos.close();
        }
    }


    public static void save(final String fileName, final ImagePlus imp) throws IOException {
        save(fileName, imp, false);
    }


    public static void save(final String fileName, final ImagePlus imp, final boolean saveAsAscii) throws IOException {
        if (saveAsAscii) {
            VtkEncoder.saveAsVtkAscii(fileName, imp);
        } else {
            VtkEncoder.saveAsVtkBinary(fileName, imp);
        }
    }<commit_msg>Add javadoc comments.<commit_after>/*
 * Image/J Plugins
 * Copyright (C) 2002-2011 <NAME>
 * Author's email: jsacha at users dot sourceforge dot net
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Latest release available at http://sourceforge.net/projects/ij-plugins/
 */

package net.sf.ij_plugins.io.vtk;

import ij.IJ;
import ij.ImagePlus;
import ij.io.ImageWriter;
import ij.measure.Calibration;

import java.io.*;


/**
 * Save image in <a HREF="http://public.kitware.com/VTK/">VTK</a> format. Supported image types:
 * GRAY8, GRAY16, GRAY32.
 *
 * @author <NAME>
 */

public final class VtkEncoder {

    private static final String TAG_SEPARATOR = " ";
    private static final String VTK_FILE_VERSION = "3.0";


    private VtkEncoder() {

    }


    private static String createHeader(final ImagePlus imp, final boolean asciiFormat) {

        final StringBuffer header = new StringBuffer();
        header.append(VtkTag.DATA_FILE_VERSION).append(VTK_FILE_VERSION).append("\n");

        header.append(imp.getTitle()).append("\n");

        header.append(asciiFormat ? VtkDataFormat.ASCII : VtkDataFormat.BINARY);
        header.append("\n");

        header.append(VtkTag.DATASET).append(TAG_SEPARATOR).append(VtkDataSetType.STRUCTURED_POINTS).append("\n");

        final int width = imp.getWidth();
        final int height = imp.getHeight();
        final int depth = imp.getStackSize();
        header.append(VtkTag.DIMENSIONS).append(TAG_SEPARATOR).append(width).append(" ")
                .append(height).append(" ").append(depth).append("\n");

        final Calibration c = imp.getCalibration();
        header.append(VtkTag.SPACING).append(TAG_SEPARATOR).append(c.pixelWidth).append(" ")
                .append(c.pixelHeight).append(" ").append(c.pixelDepth).append("\n");

        header.append(VtkTag.ORIGIN).append(TAG_SEPARATOR).append(c.xOrigin).append(" ")
                .append(c.yOrigin).append(" ").append(c.zOrigin).append("\n");

        header.append(VtkTag.POINT_DATA).append(TAG_SEPARATOR).append(width * height * depth)
                .append("\n");

        final String scalarName;
        switch (imp.getType()) {
            case ImagePlus.GRAY8:
                scalarName = VtkScalarType.UNSIGNED_CHAR.toString();
                break;
            case ImagePlus.GRAY16:
                scalarName = VtkScalarType.UNSIGNED_SHORT.toString();
                break;
            case ImagePlus.GRAY32:
                scalarName = VtkScalarType.FLOAT.toString();
                break;
            // BEG KEESH RGB UPDATE
            case ImagePlus.COLOR_RGB:
                scalarName = VtkScalarType.UNSIGNED_CHAR.toString();
                break;
            // END KEESH RGB UPDATE
            default:
                throw new IllegalArgumentException("Unsupported image type. "
                        + "Only images of types: GRAY8, GRAY16, and GRAY32 are supported.");
        }
        // BEG KEESH RGB UPDATE
        if (imp.getType() != ImagePlus.COLOR_RGB) {
            header.append(VtkTag.SCALARS).append(TAG_SEPARATOR).append("volume_scalars ")
                    .append(scalarName).append(" 1\n");
        } else {
            header.append(VtkTag.COLOR_SCALARS).append(TAG_SEPARATOR).append("volume_scalars ")
                    .append("3\n");  // no scalar name as per VTK spec
        }
        // Writing LUT tag for RGB images incorrectly offsets image data
        if (imp.getType() != ImagePlus.COLOR_RGB) {
            header.append(VtkTag.LOOKUP_TABLE).append(TAG_SEPARATOR).append("default\n");
        }
        // END KEESH RGB UPDATE

        return header.toString();
    }


    private static void saveAsVtkBinary(final String fileName, final ImagePlus imp) throws IOException {

        final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
        try {
            final String header = createHeader(imp, false);
            bos.write(header.getBytes());

            final ImageWriter imageWriter = new ImageWriter(imp.getFileInfo());
            imageWriter.write(bos);
        } finally {
            bos.close();
        }
    }


    /**
     * Save image in VTK format using binary encoding.
     *
     * @param fileName output file name.
     * @param imp      image to save
     * @throws IOException in case of I/O error.
     * @see #save(String, ij.ImagePlus, boolean)
     */
    public static void save(final String fileName, final ImagePlus imp) throws IOException {
        save(fileName, imp, false);
    }


    /**
     * Save image in VTK format.
     *
     * @param fileName    output file name.
     * @param imp         image to save
     * @param saveAsAscii if {@code true} save in text format, otherwise in binary
     * @throws IOException in case of I/O error.
     */
    public static void save(final String fileName, final ImagePlus imp, final boolean saveAsAscii) throws IOException {
        if (saveAsAscii) {
            VtkEncoder.saveAsVtkAscii(fileName, imp);
        } else {
            VtkEncoder.saveAsVtkBinary(fileName, imp);
        }
    }
{'fraction_non_alphanumeric': 0.10654998195597257, 'fraction_numerical': 0.007758931793576326, 'mean_word_length': 2.488042794210195, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2757121', 'n_tokens_mistral': 3253, 'n_tokens_neox': 3066, 'n_words': 837}