package com.xxx.imageDeal;
import java.io.*;
import java.awt.image.*;
import java.io.OutputStream;
import java.io.IOException;
import ij.*;
import ij.gui.*;
import ij.process.*;
/**
* Class AnimatedGifEncoder - Encodes a GIF file consisting of one or more
* frames.
*
* <pre>
*
*
* Extensively Modified for ImagePlus
* Extended to handle 8 bit Images with more complex Color lookup tables with transparency index
*
* Ryan Raz March 2002
* [email protected]
* Version 1.01
* * Extensively Modified for ImagePlus
* Extended to handle 8 bit Images with more complex Color lookup tables with transparency index
*
* Ryan Raz March 2002
* [email protected]
* Version 1.01 Please report any bugs
*
* Operation Manual
*
*
* 1) Load stack with 8 bit or RGB images it is possible to use the animated gif reader but because the color
* table is lost it is best to also load a separate copy of the first image in the series this will allow
* extraction of the original image color look up table (see 1below)
* 2)Check the option list to bring up the option list.
* 3)Experiment with the option list. I usually use a global color table to save space, set to do not dispose if
* each consecutive image is overlayed on the previous image.
* 4)Color table can be imported from another image or extracted from 8bit stack images or loaded as the
* first 256 RGB triplets from a RGB images, the last mode takes either a imported image or current
* stack and creates the color table from scratch.
*
*
* To do list
*
* 1) Modify existing Animated Gif reader plug in to import in 8 bit mode (currently only works in
* RGB mode. Right now the best way to alter an animated gif is to save the first image separately
* and then read the single gif and use the plugin animated reader to read the animated gif to the
* stack. Let this plugin encode the stack using the single gif's color table.
* 2) Add support for background colors easy but I have no use for them
* 3) RGB to 8 bit converter is a linear search. Needs to be replaced with sorted list and fast search. But
* this update could cause problems with some types of gifs. Easy fix get a faster computer.
* 4) Try updating NN color converter seems to be heavily weighted towards quantity of pixels.
* example:
* if there is 90% of the image covered in shades of one color or grey the 10% of other colors tend
* to be poorly represented it over fits the shades and under fits the others. Works well if the
* distribution is balanced.
* 5) Add support for all sizes of Color Look Up tables.
* 6) Re-code to be cleaner. This is my second Java program and I started with some code with too
* many global variables and I added more switches so its a bit hard to follow.
*
* Credits for the base conversion codes
* No copyright asserted on the source code of this class. May be used
* for any purpose, however, refer to the Unisys LZW patent for restrictions
* on use of the associated LZWEncoder class. Please forward any corrections
* to [email protected].
*
* @author Kevin Weiner, FM Software
* @version 1.0 December 2000
*
*
* Example:
* AnimatedGifEncoder e = new AnimatedGifEncoder();
* e.start(outputFileName);
* e.addFrame(image1);
* e.addFrame(image2);
* " " "
* e.finish();
* </pre>
*
*
*/
public class GifEncoder {
protected int width; // image size
protected int height;
protected boolean transparent = false; // transparent color if given
protected int transIndex; // transparent index in color table
protected int repeat = -1; // no repeat
protected int delay = 50; // frame delay (hundredths)
protected boolean started = false; // ready to output frames
protected OutputStream out;
protected ImagePlus image; // current frame
protected byte[] pixels; // BGR byte array from frame
protected byte[] indexedPixels; // converted frame indexed to palette
protected int colorDepth; // number of bit planes
protected byte[] colorTab; // RGB palette
protected int lctSize = 7; // local color table size (bits-1)
protected int dispose = 0; // disposal code (-1 = use default)
protected boolean closeStream = false; // close stream when finished
protected boolean firstFrame = true;
protected boolean sizeSet = false; // if false, get size from first frame
protected int sample = 2; // default sample interval for quantizer
// distance should be small for small icons
protected byte[] gct = null; // Global color table
protected boolean gctused = false; // Set to true to use Global color table
protected boolean autotransparent = false; // Set True if transparency
// index coming from image 8 bit
// only
protected boolean GCTextracted = false; // Set if global color table
// extracted from rgb image
protected boolean GCTloadedExternal = false; // Set if global color table
// loaded directly from
// external image
protected int GCTred = 0; // Transparent Color
protected int GCTgrn = 0; // green
protected int GCTbl = 0; // blue
protected int GCTcindex = 0; // index into color table
protected boolean GCTsetTransparent = false; // If true then Color table
// transparency index is set
protected boolean GCToverideIndex = false; // If true Transparent index is
// set to index with closest
// colors
protected boolean GCToverideColor = false; // if true Color at Transparent
// index is set to GCTred,
// GCTgrn GCTbl
/**
* Adds next GIF frame. The frame is not written immediately, but is
* actually deferred until the next frame is received so that timing data
* can be inserted. Invoking <code>finish()</code> flushes all frames. If
* <code>setSize</code> was not invoked, the size of the first image is
* used for all subsequent frames.
*
* @param im
* containing frame to write.
* @return true if successful.
*/
public boolean addFrame(ImagePlus image) {
if ((image == null) || !started)
return false;
boolean ok = true;
try {
if (firstFrame) {
if (!sizeSet) {
// use first frame's size
setSize(image.getWidth(), image.getHeight());
}
if (gctused)
writeLSDgct(); // logical screen descriptior
if (GCTloadedExternal) { // Using external image as color
// table
colorTab = gct;
TransparentIndex(colorTab); // check transparency color
writePalette(); // write global color table
if (repeat >= 0)
writeNetscapeExt(); // use NS app extension to indicate
// reps
}
if (!gctused) {
writeLSD();
if (repeat >= 0)
writeNetscapeExt(); // use NS app extension to indicate
// reps
}
firstFrame = false;
}
int type = image.getType();
// If indexed byte image then format does not need changing
if ((type == 0) || (type == 3)) { // 8 bit images
Process8bitCLT(image);
}
else if (type == 4) { // 4 for RGB
packrgb(image);
OverRideQuality(image.getWidth() * image.getHeight());
if (gctused && (gct == null)) { // quality should not depend on
// image size
analyzePixels(); // build global color table & map pixels
colorTab = gct;
TransparentIndex(colorTab); // check transparency color
writePalette(); // write global color table
if (repeat >= 0)
writeNetscapeExt(); // use NS app extension to indicate
// reps
} else
analyzePixels(); // build color table & map pixe
image_deal.rar_ImageDeal_image web java_web_segmention_加水印_缩小
版权申诉
172 浏览量
2022-09-24
14:49:21
上传
评论
收藏 974KB RAR 举报
APei
- 粉丝: 64
- 资源: 1万+
最新资源
- 88-520告白(520气球).zip
- HTML+CSS+JS精品网页模板H126.rar
- n.cpp
- jdk-8u411-windows-x64下载安装可用
- vgg模型-图像分类算法对水果识别-不含数据集图片-含逐行注释和说明文档.zip
- KMP算法(Knuth-Morris-Pratt算法
- vgg模型-python语言pytorch框架训练识别化妆品分类-不含数据集图片-含逐行注释和说明文档.zip
- KMP算法(Knuth-Morris-Pratt算法
- shufflenet模型-基于人工智能的卷积网络训练识别狗的表情-不含数据集图片-含逐行注释和说明文档.zip
- EPIC Fantasy Town 1.0
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈