0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LZW解凍

Last updated at Posted at 2024-02-05
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class LZWDecoder {

    public static byte[] decompress(byte[] compressedData) {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        int dictSize = 256;
        int dictSizeBits = 8;
        int[] dictionary = new int[4096];

        // Initialize dictionary with all possible bytes
        for (int i = 0; i < 256; i++) {
            dictionary[i] = i;
        }

        int currentCode = 0;
        int currentBits = 0;

        for (byte compressedByte : compressedData) {
            currentCode = (currentCode << 8) | (compressedByte & 0xFF);
            currentBits += 8;

            while (currentBits >= dictSizeBits) {
                int outputCode = currentCode >> (currentBits - dictSizeBits);
                currentCode &= (1 << (currentBits - dictSizeBits)) - 1;
                currentBits -= dictSizeBits;

                if (outputCode == 256) {
                    break; // End of data
                } else if (outputCode < dictSize) {
                    // Output code to result
                    try {
                        result.write(dictionary[outputCode]);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    // Add new entry to the dictionary
                    if (dictSize < 4096) {
                        dictionary[dictSize++] = currentCode << 8 | dictionary[outputCode];
                    }
                } else {
                    // Handle special case for clear code
                    if (outputCode == 257) {
                        // Reset dictionary
                        dictSize = 258;
                        dictSizeBits = 9;
                        for (int i = 0; i < 256; i++) {
                            dictionary[i] = i;
                        }
                    }
                }
            }
        }

        return result.toByteArray();
    }

    public static void main(String[] args) {
        // Example usage
        byte[] compressedData = /* Your compressed data here */;
        byte[] decompressedData = decompress(compressedData);

        // Now, decompressedData contains the decompressed image data
    }
}
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class Main {

    public static void main(String[] args) {
        // ファイルからデータを読み込む
        File inputFile = new File("/foo/てぃふ.tiff");
        try {
            byte[] compressedData = Files.readAllBytes(inputFile.toPath());

            // 解凍
            byte[] decompressedData = LZWDecoder.decompress(compressedData);

            // これ以降、decompressedDataを適切な処理に使用できます
            // 例: 他のライブラリを使用して画像に変換するなど
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

public class ImageReaderExample {
    public static void main(String[] args) {
        File file = new File("your_image_path");

        try {
            // Get ImageReader for the file
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("your_image_format");
            ImageReader reader = readers.next();
            reader.setInput(ImageIO.createImageInputStream(file));

            // Configure ImageReadParam to handle specific properties
            ImageReadParam param = reader.getDefaultReadParam();
            // Set additional parameters if needed

            // Read the image using the specified parameters
            BufferedImage image = reader.read(0, param);

            // Now 'image' contains the loaded image
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package cola.foo;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class ImageReaderExample {
	public static BufferedImage decodeLZWImage(File compressedFile) throws IOException {
        // ImageIOのサービスプロバイダを追加
        ImageIO.scanForPlugins();
        
        // ImageIOからTIFFImageReaderを取得
        Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("TIFF");
        ImageReader reader = null;
        while (readers.hasNext()) {
            reader = readers.next();
//            if (reader instanceof TIFFImageReaderSpi) {
//                break;
//            }
        }

        if (reader == null) {
            throw new IllegalArgumentException("TIFF ImageReader not found");
        }

        // LZW圧縮画像ファイルを解凍し、BufferedImageに格納
        ImageInputStream inputStream = ImageIO.createImageInputStream(compressedFile);
        reader.setInput(inputStream);
        BufferedImage image = reader.read(0);

        // ストリームをクローズ
        inputStream.close();
        
        return image;
    }

//    public static void main(String[] args) {
//        File compressedFile = new File("C:\\Users\\hayaj\\OneDrive\\デスクトップ\\仕事\\tif.tif");
//        try {
//            BufferedImage image = decodeLZWImage(compressedFile);
//            // 画像の利用例
//            // ここに、imageを使った処理を記述
//            System.out.println(image.getTransparency());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
	
//	public static void main(String[] args) {
//        String[] formatNames = ImageIO.getReaderFormatNames();
//        System.out.println("Supported Image Formats:");
//        for (String format : formatNames) {
//            String[] extensions = ImageIO.getReaderFileSuffixes();
//            StringBuilder extensionList = new StringBuilder();
//            for (String ext : extensions) {
//                if (ext != null && format.equalsIgnoreCase(ext)) {
//                    if (extensionList.length() > 0) {
//                        extensionList.append(", ");
//                    }
//                    extensionList.append(ext);
//                }
//            }
//            System.out.println(format + ": " + extensionList.toString());
//        }
//    }
}
public enum EnumSample {
    INSTANCE1,
    INSTANCE2,
    INSTANCE3;

    public EnumSample[] method(EnumSample first) {
        EnumSample[] array = new EnumSample[EnumSample.values().length];
        array[0] = first;
        int index = 1;
        for (EnumSample instance : EnumSample.values()) {
            if (instance != first) {
                array[index++] = instance;
            }
        }
        return array;
    }
}
# WordファイルとプロセスIDの対応表を一覧表示する

# Wordプロセスを取得
$wordProcesses = Get-Process | Where-Object { $_.ProcessName -eq "WINWORD" }

if ($wordProcesses) {
    Write-Host "WordファイルとプロセスIDの対応表:"
    foreach ($process in $wordProcesses) {
        $files = $process.MainWindowTitle
        $processId = $process.Id
        Write-Host "ファイル: $files, プロセスID: $processId"
    }
} else {
    Write-Host "実行中のWordプロセスはありません。"
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?