<commit_before>/*
 * Copyright 2018, <NAME>
 *
 * 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.fkeglevich.rawdumper.dng.dngsdk;

import android.util.Log;

import com.fkeglevich.rawdumper.debug.DebugFlag;
import com.fkeglevich.rawdumper.debug.PerfInfo;
import com.fkeglevich.rawdumper.raw.capture.CaptureInfo;
import com.fkeglevich.rawdumper.raw.capture.ExifInfo;
import com.fkeglevich.rawdumper.raw.data.RawImageSize;
import com.fkeglevich.rawdumper.raw.data.buffer.RawImageData;
import com.fkeglevich.rawdumper.raw.gain.GainMapOpcodeStacker;

import java.io.IOException;

public class DngWriter2
{
    private static final String TAG = "DngWriter2";
    private static final boolean LOG_ERRORS = true;

    static
    {
        System.loadLibrary("dng-writer");
    }

    public void write(CaptureInfo captureInfo, RawImageData imageData) throws IOException
    {
        DngNegative negative = new DngNegative();
        try
        {
            ExifInfo exifInfo = new ExifInfo();
            exifInfo.getExifDataFromCapture(captureInfo);

            writeMetadata(captureInfo, exifInfo, negative);

            exifInfo.writeInfoTo(negative);

            // Start writing image
            RawImageSize imageSize = imageData.getSize();
            PerfInfo.start("BufferTime");
            byte[] buffer = new byte[imageSize.getPaddedWidth() * imageSize.getPaddedHeight() * imageSize.getBytesPerPixel()];


            int paddedHeight = imageSize.getPaddedHeight();
            int widthBytes = imageSize.getPaddedWidth() * imageSize.getBytesPerPixel();

            for (int row = 0; row < paddedHeight; row++)
                imageData.copyValidRowToBuffer(captureInfo.shouldInvertRows() ? (paddedHeight - 1 - row): row, buffer, widthBytes * row);

            PerfInfo.end("BufferTime");

            PerfInfo.start("SaveAndCompress");
            negative.writeImageToFile(captureInfo.destinationRawFilename,
                    imageSize.getPaddedWidth(), imageSize.getPaddedHeight(), buffer, false);
            PerfInfo.end("SaveAndCompress");
            // End writing image
        }
        finally
        {
            negative.dispose();
        }
    }

    private void writeMetadata(CaptureInfo captureInfo, ExifInfo exifInfo, DngNegative negative)
    {
        Log.i("RawSettings", captureInfo.rawSettings.toString());

        negative.setImageSizeAndOrientation(captureInfo.imageSize.getPaddedWidth(), captureInfo.imageSize.getPaddedHeight(), captureInfo.rawSettings.getOrientationCode(captureInfo));

        captureInfo.camera.getSensor().writeInfoTo(negative, exifInfo, captureInfo.shouldInvertRows());
        captureInfo.camera.writeInfoTo(negative);
        captureInfo.writeInfoTo(negative);

        //captureInfo.date.writeTiffTags(tiffWriter);
        captureInfo.camera.getColor().writeInfoTo(negative, captureInfo);
        captureInfo.camera.getNoise().writeInfo(negative);
        captureInfo.whiteBalanceInfo.writeInfoTo(negative);

        if (!DebugFlag.dontUseGainMaps())
        {
            if (captureInfo.camera.getGainMapCollection() != null)
                GainMapOpcodeStacker.write(captureInfo, negative);
            else if (captureInfo.camera.getOpcodes() != null && captureInfo.camera.getOpcodes().length >= 1)
                captureInfo.camera.getOpcodes()[0].writeInfoTo(negative);
        }
    }
}<commit_msg>Minor code cleanup<commit_after>/*
 * Copyright 2018, <NAME>
 *
 * 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.fkeglevich.rawdumper.dng.dngsdk;

import android.util.Log;

import com.fkeglevich.rawdumper.debug.DebugFlag;
import com.fkeglevich.rawdumper.debug.PerfInfo;
import com.fkeglevich.rawdumper.raw.capture.CaptureInfo;
import com.fkeglevich.rawdumper.raw.capture.ExifInfo;
import com.fkeglevich.rawdumper.raw.data.RawImageSize;
import com.fkeglevich.rawdumper.raw.data.buffer.RawImageData;
import com.fkeglevich.rawdumper.raw.gain.GainMapOpcodeStacker;

import java.io.IOException;

public class DngWriter2
{
    private static final String TAG = "DngWriter2";

    static
    {
        System.loadLibrary("dng-writer");
    }

    public void write(CaptureInfo captureInfo, RawImageData imageData) throws IOException
    {
        DngNegative negative = new DngNegative();
        try
        {
            ExifInfo exifInfo = new ExifInfo();
            exifInfo.getExifDataFromCapture(captureInfo);

            writeMetadata(captureInfo, exifInfo, negative);

            exifInfo.writeInfoTo(negative);

            // Start writing image
            RawImageSize imageSize = imageData.getSize();
            PerfInfo.start("BufferTime");
            byte[] buffer = new byte[imageSize.getPaddedWidth() * imageSize.getPaddedHeight() * imageSize.getBytesPerPixel()];


            int paddedHeight = imageSize.getPaddedHeight();
            int widthBytes = imageSize.getPaddedWidth() * imageSize.getBytesPerPixel();

            for (int row = 0; row < paddedHeight; row++)
                imageData.copyValidRowToBuffer(captureInfo.shouldInvertRows() ? (paddedHeight - 1 - row): row, buffer, widthBytes * row);

            PerfInfo.end("BufferTime");

            PerfInfo.start("SaveAndCompress");
            negative.writeImageToFile(captureInfo.destinationRawFilename, imageSize.getPaddedWidth(), imageSize.getPaddedHeight(), buffer, false);
            PerfInfo.end("SaveAndCompress");
            // End writing image
        }
        finally
        {
            negative.dispose();
        }
    }

    private void writeMetadata(CaptureInfo captureInfo, ExifInfo exifInfo, DngNegative negative)
    {
        Log.i("RawSettings", captureInfo.rawSettings.toString());

        negative.setImageSizeAndOrientation(captureInfo.imageSize.getPaddedWidth(), captureInfo.imageSize.getPaddedHeight(), captureInfo.rawSettings.getOrientationCode(captureInfo));

        captureInfo.camera.getSensor().writeInfoTo(negative, exifInfo, captureInfo.shouldInvertRows());
        captureInfo.camera.writeInfoTo(negative);
        captureInfo.writeInfoTo(negative);

        captureInfo.camera.getColor().writeInfoTo(negative, captureInfo);
        captureInfo.camera.getNoise().writeInfo(negative);
        captureInfo.whiteBalanceInfo.writeInfoTo(negative);

        if (!DebugFlag.dontUseGainMaps())
        {
            if (captureInfo.camera.getGainMapCollection() != null)
                GainMapOpcodeStacker.write(captureInfo, negative);
            else if (captureInfo.camera.getOpcodes() != null && captureInfo.camera.getOpcodes().length >= 1)
                captureInfo.camera.getOpcodes()[0].writeInfoTo(negative);
        }
    }
}
{'fraction_non_alphanumeric': 0.09962234665972132, 'fraction_numerical': 0.0036463081130355514, 'mean_word_length': 3.180729450190528, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1089425', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2142, 'n_words': 447}