


js decodes the image base64 encoded string and outputs the image example_javascript skills
< script>
/////////////////////////////
//base64 encoded GIF image decoding
//By Mozart0
//2005/10/29
////////////////////
//Create a GIF class object
/ /Class GIF is defined inside this function
// str64: Base64 encoded string of gif file
// Returns the created GIF object on success
// Returns null on failure
function getGif(str64){
var bytes=decodeBase64(str64);
if(!bytes){
alert("Error: Invalid Base64 encoding");
return null;
}
var gif =new GIF();
for(var i=0;i<6;i )
gif.version =String.fromCharCode(bytes[i]);
if(gif.version.slice( 0,3)!="GIF"){
alert("Error: non-Gif image format");
return null;
}
gif.width=bytes[i]|(bytes [i 1]<<8);
gif.height=bytes[i 2]|(bytes[i 3]<<8);
var f=bytes[i 4];
gif.colorResolution=(f>>4&0x7) 1;
gif.sorted=(f&0x8)?true:false;
gif.backgroundIndex=bytes[i 5];
gif.pixelAspectRadio= bytes[i 6];
if(f&0x80){
gif.globalPalette=[];
i =getPalette(i 7,bytes,gif.globalPalette,2<<(f&0x7));
}
i =7;
for(var j=i;j
break;
if(j==bytes.length){
for(;i
break;
if(i==bytes.length){
alert("Error: Image data not found");
return null;
}
var f=new GIF_Frame();
if(!getSingleFrame(i,f))
return null;
else
gif.frames.push(f);
}
else{
i=j;
do{
var f=new GIF_Frame();
var t=getSingleFrame(i,f);
if(!t)
return null;
gif.frames.push (f);
for(i =t;i
break;
}
while(i
return gif;
//Internal process, generate color table
function getPalette(pos,s,d,len) {
len*=3;
for(var i=pos;i
(s[i 1]<=0xf?"0":"") s[i 1].toString(16)
(s [i 2]<=0xf?"0":"") s[i 2].toString(16));
return len;
}
//Internal process, integration Data segment
function getBlock(pos,s,d){
var p=pos;
while(len=s[p]){
for(var i=0;i
p =len;
}
return p-pos;
}
//Internal process, get One frame of data
function getSingleFrame(pos,frame){
var i=pos;
if(bytes[i]==0x21){
i =3;
if(bytes[ i]&1)
frame.transparentIndex=bytes[i 3];
frame.delay=bytes[i 1]|(bytes[i 2]<<8);
for(i = 5;i
alert("Error: Image identifier not found");
return 0;
}
}
frame.offsetX=bytes[i 1]|(bytes[i 2]<<8);
frame.offsetY=bytes[i 3]| (bytes[i 4]<<8);
frame.width=bytes[i 5]|(bytes[i 6]<<8);
frame.height=bytes[i 7 ]|(bytes[i 8]<<8);
var f=bytes[i 9];
i =10;
if(f&0x40)
frame.interlace=true;
if(f&0x20)
frame.sorted=true;
if(f&0x80){
frame.colorResolution=(f&0x7) 1;
frame.localPalette=[];
i =getPalette(i,bytes,frame.localPalette,1<
else{
frame.colorResolution=gif.colorResolution;
frame.localPalette=gif.globalPalette ;
}
var lzwLen=bytes[i ] 1;
i =getBlock(i,bytes,frame.data);
frame.data=decodeLzw(frame.data,lzwLen);
return frame.data?i-pos:0;
}
//Define the data structure for storing GIF files
//Provide method showInfo and return image information
function GIF( ){
this.version=""; //Version number
this.width=0; //Logical screen width
this.height=0; //Logical screen height
this.colorResolution =0; //Color depth
this.sorted=false; //Global color table classification flag
this.globalPalette=null; //Global color table
this.backgroundIndex=-1; //Background Color index
this.pixelAspectRadio=0; //Pixel aspect ratio
this.frames=[]; //For each frame of the image, see GIF_Frame
this.showInfo=function(sep){ //Display Image information, sep is the line separator
if(!sep)
sep="n";
var s="Gif infomation:" sep "------------ -------";
s =subInfo(this) sep;
for(var i=0;i
return s;
function subInfo(o){
var s="";
for (var i in o){
if(i=="showInfo"||i=="draw")
continue;
s =sep i ":";
if(typeof( o[i])=="object")
s =(o[i]?o[i].length:"null");
else
s =o[i];
}
return s;
}
}
}
//Define the data structure to store a frame of image
//Provide method draw, drawing
function GIF_Frame (){
this.offsetX=0; //X-direction offset
this.offsetY=0; //Y-direction offset
this.width=0; //Image width
this.height=0; //Image height
this.localPalette=null; //Local color table
this.colorResolution=0; //Color depth
this.interlace=false; / /Interlaced flag
this.sorted=false; //Local color table classification flag
this.data=[]; //Image data, storing the integer index of each pixel color
this.transparentIndex=-1 ; //Transparent color index
this.delay=0; //Frame delay
this.draw=function(parent,zoom){
if(!this.data.length)
return ;
if(!parent)
parent=document.body;
if(!zoom)
zoom=1;
if(parent.clientWidth
if(parent.clientHeight
var id=" ImgDefaultDraw";
var img=document.getElementById(id);
if(img)
delete parent.removeChild(img);
img=document.createElement("DIV");
img.id=id;
parent.appendChild(img);
img.style.position="absolute";
var t=document.createElement("DIV");
t. style.overflow="hidden";
t.style.position="absolute";
defLayout(this.data,this.localPalette,this.width,this.height,img,t,zoom);
delete t;
}
}
}
//Base64 decoding
//strIn, input string
//returns an array successfully, each element Contains one byte of information
// Returns null on failure
function decodeBase64(strIn){
if(!strIn.length||strIn.length%4)
return null;
var str64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /=";
var index64=[];
for(var i=0;i
var c0,c1,c2,c3,b0,b1,b2;
var len=strIn.length;
var len1=len;
if(strIn.charAt(len-1 )=='=')
len1-=4;
var result=[];
for(var i=0,j=0;i
c1=index64[strIn.charAt(i 1)];
c2=index64[strIn.charAt(i 2)];
c3=index64 [strIn.charAt(i 3)];
b0=(c0<<2)|(c1>>4);
b1=(c1<<4)|(c2>>2 );
b2=(c2<<6)|c3;
result.push(b0&0xff);
result.push(b1&0xff);
result.push(b2&0xff);
}
if(len1!=len){
c0=index64[strIn.charAt(i)];
c1=index64[strIn.charAt(i 1)];
c2=strIn. charAt(i 2);
b0=(c0<<2)|(c1>>4);
result.push(b0&0xff);
if(c2!='='){
c2=index64[c2];
b1=(c1<<4)|(c2>>2);
result.push(b1&0xff);
}
}
return result;
}
//LZW decoding function for GIF
//arrBytes is the source data, nBits is the initial encoding number of bits
//Successfully returns the array, each Elements include a color index
// Returns null on failure
function decodeLzw(arrBytes,nBits){
var cc=1<<(nBits-1);
var eoi=cc 1;
var table=[],mask=[],result=[];
for(var i=0;i
(i>>4&0xf).toString(16) (i&0xf).toString(16);
for(i=2,mask[1]=1;i<13;i )
mask[i]=mask[i-1]<<1|1;
var bc=nBits;
var pos=0,temp=0,tleft=0,code=0 ,old=0;
while(true){
while(tleft
}
code=temp&mask[bc];
tleft-=bc;
temp>>=bc;
if(code==eoi)
break;
if (code==cc){
table.length=cc 2;
bc=nBits;
old=code;
continue;
}
var t="";
if(code
if(old!=cc)
table.push(table[old] t.slice(0,3) );
}
else if(old
table.push(t);
}
else{
alert("Error: Invalid image data");
return null;
}
old=code;
for(var i=0; i
if(table.length==1<
}
return result;
}
//According to the byte array data layout, complete the drawing with the least number of divs
function defLayout(data,palette,width ,height,image,block,zoom){
var map=new Array(height);
for(var i=0;i
for(var j=0;j
}
var i,j, i1,i2,j1,j2,c;
for(i=0;i
j ;
continue;
}
c=map[i][j];
for(i1=i 1;i1
}
for(i2=i;i2
var x=block.cloneNode(true);
x.style.left=j*zoom;
x.style.top=i*zoom;
x.style.width=(j1-j)*zoom;
x.style.height=(i1-i)*zoom;
x.style.backgroundColor=palette[c];
image.appendChild(x);
j=j1;
}
}
<script> <br>function main(){ <br>var t=new Date().getTime(); <br>var xmldom=document.getElementById("imgData"); <br>var gif=getGif("R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="); <br>var info=document.getElementById("info"); <br>info.innerHTML=gif.showInfo("<br>"); <br>t=new Date().getTime(); <br>gif.frames[0].draw(document.getElementById("canvas"),1); <br>info.innerHTML ="<br>绘图耗时" (new Date().getTime()-t) "ms"; <br>} <br></SCRIPT>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Let's say you have a binary image file that you want to transfer over the network. You're surprised that the other party didn't receive the file correctly - it just contains weird characters! Well, it looks like you're trying to send the file in raw bits and bytes format, while the media you're using is designed for streaming text. What are the solutions to avoid such problems? The answer is Base64 encoding. In this article, I will show you how to encode and decode binary images using Python. The program is explained as a standalone local program, but you can apply the concept to different applications, such as sending encoded images from a mobile device to a server and many other applications. What is Base64? Before diving into this article, let’s define Base6

In modern computer programming, C language is one of the most commonly used programming languages. Although the C language itself does not directly support Chinese encoding and decoding, we can use some technologies and libraries to achieve this function. This article will introduce how to implement Chinese encoding and decoding in C language programming software. First, to implement Chinese encoding and decoding, we need to understand the basic concepts of Chinese encoding. Currently, the most commonly used Chinese encoding scheme is Unicode encoding. Unicode encoding assigns a unique numeric value to each character so that when calculating

Are you troubled by Chinese garbled characters in Eclipse? To try these solutions, you need specific code examples 1. Background introduction With the continuous development of computer technology, Chinese plays an increasingly important role in software development. However, many developers encounter garbled code problems when using Eclipse for Chinese development, which affects work efficiency. Then, this article will introduce some common garbled code problems and give corresponding solutions and code examples to help readers solve the Chinese garbled code problem in Eclipse. 2. Common garbled code problems and solution files

What are the techniques for byte encoding and decoding in Python? Byte encoding and decoding are problems we often encounter when processing text data. In Python, there are many built-in functions and modules that help us perform byte encoding and decoding operations. This article will introduce several common byte encoding and decoding techniques and give corresponding code examples. Byte Encoding Using the encode() Function The encode() function is a method in Python used to encode a Unicode string into a sequence of bytes. its general

The purpose of this article is to implement a program to decode a given string by removing recurring characters. As you know what a string is, a string is nothing but a collection of characters. Additionally, there is no limit to the number of times characters can be repeated in a string. The same character can appear multiple times in a string. In this article, we will find a way to decode a given encoded string str by removing duplicate occurrences. The goal is to decode the provided string str, which has been encoded with one occurrence of 'a', two occurrences of 'b', three occurrences of 'c', four occurrences of 'd', up to 26 occurrences of 'z' . Problem Statement Implement a program to decode a given string by removing duplicate occurrences. Note − Do not ignore spaces that may be included in the letter

In PHP, base64 encoding can be used to convert binary data into printable ASCII characters, thus facilitating data transmission and storage. The base64_decode() function can decode the base64-encoded string into original binary data. The following will provide specific PHP code examples to introduce how to use this function to decode strings. First, you can base64 encode the string using the following code: $str="HelloWorl

A pin code is a security measure used to protect personal information and devices. However, sometimes we may forget the PIN or need to unlock it. This article will discuss some methods to unlock your PIN code. First, if you have forgotten your device's PIN, you may consider using the reset option provided by your device. For example, with a phone or tablet, you can try to trigger the reset option by entering the wrong PIN multiple times. The specific method may vary depending on the device. You can consult the user manual of the device or find specific reset steps online.

This article will explain in detail how PHP outputs GD2 images to a browser or file. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Output GD2 images to browser or file The GD2 library in PHP provides rich functionality for creating, editing and outputting images. Here's how to output a GD2 image to a browser or file: Output to Browser Create an image: Use the imagecreate() function to create a new canvas. Drawing content: Use functions such as imagestring() and imageline() to draw text, line segments and other contents. Set header information: Use the header() function to set the correct M
