Skip to content

Commit

Permalink
added rotate option
Browse files Browse the repository at this point in the history
  • Loading branch information
sehugg committed May 25, 2017
1 parent 55bdbfe commit 082076d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/gif.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gif.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gif.worker.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gif.worker.js.map

Large diffs are not rendered by default.

58 changes: 49 additions & 9 deletions src/GIFEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function GIFEncoder(width, height) {
this.sample = 10; // default sample interval for quantizer
this.dither = false; // default dithering
this.globalPalette = false;
this.rotate = 0; // no rotation

this.out = new ByteArray();
}
Expand Down Expand Up @@ -137,6 +138,14 @@ GIFEncoder.prototype.setTransparent = function(color) {
this.transparent = color;
};

/*
Rotates 90 degrees (1) or -90 degrees (-1)
*/
GIFEncoder.prototype.setRotate = function(r) {
this.rotate = r;
}


/*
Adds next GIF frame. The frame is not written immediately, but is
actually deferred until the next frame is received so that timing
Expand Down Expand Up @@ -261,12 +270,21 @@ GIFEncoder.prototype.analyzePixels = function() {
GIFEncoder.prototype.indexPixels = function(imgq) {
var nPix = this.pixels.length / 3;
this.indexedPixels = new Uint8Array(nPix);
var k = 0;
for (var j = 0; j < nPix; j++) {
var i = j*3;
if (this.rotate) {
var yy = (j % this.height);
var xx = Math.floor(j / this.height);
if (this.rotate > 0)
yy = this.height - 1 - yy;
else
xx = this.width - 1 - xx;
i = ((yy * this.width) + xx) * 3;
}
var index = this.findClosestRGB(
this.pixels[k++] & 0xff,
this.pixels[k++] & 0xff,
this.pixels[k++] & 0xff
this.pixels[i++] & 0xff,
this.pixels[i++] & 0xff,
this.pixels[i++] & 0xff
);
this.usedEntry[index] = true;
this.indexedPixels[j] = index;
Expand Down Expand Up @@ -333,6 +351,18 @@ GIFEncoder.prototype.ditherPixels = function(kernel, serpentine) {
for (var x = (direction == 1 ? 0 : width - 1), xend = (direction == 1 ? width : 0); x !== xend; x += direction) {

index = (y * width) + x;

// Rotate?
if (this.rotate) {
var yy = (index % height);
var xx = Math.floor(index / height);
if (this.rotate > 0)
yy = height - 1 - yy;
else
xx = width - 1 - xx;
index = (yy * width) + xx;
}

// Get original colour
var idx = index * 3;
var r1 = data[idx];
Expand Down Expand Up @@ -382,7 +412,7 @@ GIFEncoder.prototype.findClosestRGB = function(r, g, b, used) {
if (this.neuQuant && !used) {
return this.neuQuant.lookupRGB(r, g, b);
}

var c = b | (g << 8) | (r << 16);

var minpos = 0;
Expand Down Expand Up @@ -468,8 +498,13 @@ GIFEncoder.prototype.writeImageDesc = function() {
this.out.writeByte(0x2c); // image separator
this.writeShort(0); // image position x,y = 0,0
this.writeShort(0);
this.writeShort(this.width); // image size
this.writeShort(this.height);
if (this.rotate) {
this.writeShort(this.height); // image size
this.writeShort(this.width);
} else {
this.writeShort(this.width); // image size
this.writeShort(this.height);
}

// packed fields
if (this.firstFrame || this.globalPalette) {
Expand All @@ -492,8 +527,13 @@ GIFEncoder.prototype.writeImageDesc = function() {
*/
GIFEncoder.prototype.writeLSD = function() {
// logical screen size
this.writeShort(this.width);
this.writeShort(this.height);
if (this.rotate) {
this.writeShort(this.height); // image size
this.writeShort(this.width);
} else {
this.writeShort(this.width); // image size
this.writeShort(this.height);
}

// packed fields
this.out.writeByte(
Expand Down
2 changes: 2 additions & 0 deletions src/gif.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GIF extends EventEmitter
transparent: null
debug: false
dither: false # see GIFEncoder.js for dithering options
rotate: 0

frameDefaults =
delay: 500 # ms
Expand Down Expand Up @@ -188,6 +189,7 @@ class GIF extends EventEmitter
dither: @options.dither
globalPalette: @options.globalPalette
repeat: @options.repeat
rotate: @options.rotate
canTransfer: (browser.name is 'chrome')

if frame.data?
Expand Down
2 changes: 2 additions & 0 deletions src/gif.worker.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ GIFEncoder = require './GIFEncoder.js'
renderFrame = (frame) ->
encoder = new GIFEncoder frame.width, frame.height

encoder.setRotate frame.rotate

if frame.index is 0
encoder.writeHeader()
else
Expand Down

0 comments on commit 082076d

Please sign in to comment.