|
| 1 | +package com.tumuyan.ncnn.realsr; |
| 2 | + |
| 3 | + |
| 4 | +import java.io.BufferedInputStream; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +public class GifUntils { |
| 10 | + |
| 11 | + public static int getGifFrameCount(String filePath) { |
| 12 | + try (FileInputStream fis = new FileInputStream(new File(filePath)); |
| 13 | + BufferedInputStream bis = new BufferedInputStream(fis)) { |
| 14 | + // 检查GIF文件头 |
| 15 | + byte[] header = new byte[6]; |
| 16 | + if (bis.read(header) != header.length || !isGifHeader(header)) { |
| 17 | + return -1; |
| 18 | + } |
| 19 | + |
| 20 | + // 读取GIF文件的逻辑屏幕描述符 |
| 21 | + byte[] lsd = new byte[7]; |
| 22 | + if (bis.read(lsd) != lsd.length) { |
| 23 | + return -1; |
| 24 | + } |
| 25 | + |
| 26 | + // 获取帧数 |
| 27 | + int width = getLowByte(lsd[0]) | (getHighByte(lsd[0]) << 8); |
| 28 | + int height = getLowByte(lsd[1]) | (getHighByte(lsd[1]) << 8); |
| 29 | + int flags = lsd[3]; |
| 30 | + boolean hasGlobalColorTable = (flags & 0x80) != 0; |
| 31 | + int globalColorTableSize = hasGlobalColorTable ? (int) Math.pow(2, (flags & 0x07) + 1) : 0; |
| 32 | + |
| 33 | + // 跳过全局颜色表 |
| 34 | + bis.skip(globalColorTableSize * 3); |
| 35 | + |
| 36 | + int frameCount = 0; |
| 37 | + while (true) { |
| 38 | + int block = bis.read(); |
| 39 | + if (block == 0x2C) { // 图像数据块 |
| 40 | + frameCount++; |
| 41 | + // 解析图像数据块 |
| 42 | + byte[] imageDescriptor = new byte[9]; |
| 43 | + if (bis.read(imageDescriptor) != imageDescriptor.length) { |
| 44 | + return frameCount; |
| 45 | + } |
| 46 | + int localColorTableSize = (imageDescriptor[3] & 0x07) + 1; |
| 47 | + bis.skip(localColorTableSize * 3); // 跳过局部颜色表 |
| 48 | + skipSubBlocks(bis); |
| 49 | + } else if (block == 0x21) { // 扩展块 |
| 50 | + int extensionLabel = bis.read(); |
| 51 | + if (extensionLabel == 0xF9) { // 图形控制扩展块 |
| 52 | + bis.skip(4); // 跳过图形控制扩展块 |
| 53 | + skipSubBlocks(bis); |
| 54 | + } else { |
| 55 | + skipSubBlocks(bis); |
| 56 | + } |
| 57 | + } else if (block == 0x3B) { // 文件尾 |
| 58 | + return frameCount; |
| 59 | + } else { |
| 60 | + return -1; // 未知块类型 |
| 61 | + } |
| 62 | + } |
| 63 | + } catch (IOException e) { |
| 64 | + e.printStackTrace(); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static boolean isGifAnimation(String filePath) { |
| 70 | + try (FileInputStream fis = new FileInputStream(new File(filePath)); |
| 71 | + BufferedInputStream bis = new BufferedInputStream(fis)) { |
| 72 | + // 检查GIF文件头 |
| 73 | + byte[] header = new byte[6]; |
| 74 | + if (bis.read(header) != header.length || !isGifHeader(header)) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + // 读取GIF文件的逻辑屏幕描述符 |
| 79 | + byte[] lsd = new byte[7]; |
| 80 | + if (bis.read(lsd) != lsd.length) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + // 获取帧数 |
| 85 | + int width = getLowByte(lsd[0]) | (getHighByte(lsd[0]) << 8); |
| 86 | + int height = getLowByte(lsd[1]) | (getHighByte(lsd[1]) << 8); |
| 87 | + int flags = lsd[3]; |
| 88 | + boolean hasGlobalColorTable = (flags & 0x80) != 0; |
| 89 | + int globalColorTableSize = hasGlobalColorTable ? (int) Math.pow(2, (flags & 0x07) + 1) : 0; |
| 90 | + |
| 91 | + // 跳过全局颜色表 |
| 92 | + bis.skip(globalColorTableSize * 3); |
| 93 | + |
| 94 | + int frameCount = 0; |
| 95 | + while (true) { |
| 96 | + int block = bis.read(); |
| 97 | + if (block == 0x2C) { // 图像数据块 |
| 98 | + frameCount++; |
| 99 | + if(frameCount>1) |
| 100 | + return true; |
| 101 | + // 解析图像数据块 |
| 102 | + byte[] imageDescriptor = new byte[9]; |
| 103 | + if (bis.read(imageDescriptor) != imageDescriptor.length) { |
| 104 | + return frameCount>1; |
| 105 | + } |
| 106 | + int localColorTableSize = (imageDescriptor[3] & 0x07) + 1; |
| 107 | + bis.skip(localColorTableSize * 3); // 跳过局部颜色表 |
| 108 | + skipSubBlocks(bis); |
| 109 | + } else if (block == 0x21) { // 扩展块 |
| 110 | + int extensionLabel = bis.read(); |
| 111 | + if (extensionLabel == 0xF9) { // 图形控制扩展块 |
| 112 | + bis.skip(4); // 跳过图形控制扩展块 |
| 113 | + skipSubBlocks(bis); |
| 114 | + } else { |
| 115 | + skipSubBlocks(bis); |
| 116 | + } |
| 117 | + } else if (block == 0x3B) { // 文件尾 |
| 118 | + return frameCount>1; |
| 119 | + } else { |
| 120 | + return false; // 未知块类型 |
| 121 | + } |
| 122 | + } |
| 123 | + } catch (IOException e) { |
| 124 | + e.printStackTrace(); |
| 125 | + return false; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static boolean isGifHeader(byte[] header) { |
| 130 | + return header[0] == 'G' && header[1] == 'I' && header[2] == 'F'; |
| 131 | + } |
| 132 | + |
| 133 | + private static int getLowByte(byte b) { |
| 134 | + return b & 0xFF; |
| 135 | + } |
| 136 | + |
| 137 | + private static int getHighByte(byte b) { |
| 138 | + return b & 0xFF; |
| 139 | + } |
| 140 | + |
| 141 | + private static void skipSubBlocks(BufferedInputStream bis) throws IOException { |
| 142 | + int blockSize; |
| 143 | + do { |
| 144 | + blockSize = bis.read(); |
| 145 | + bis.skip(blockSize); |
| 146 | + } while (blockSize > 0); |
| 147 | + } |
| 148 | +} |
0 commit comments