Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Implemented Restore To Background disposal method, fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
stil committed Feb 19, 2015
1 parent 3f79c10 commit 04e09dd
Show file tree
Hide file tree
Showing 7 changed files with 889 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class Renderer
*/
protected $frameCurrent = null;

/**
* @var Frame
*/
protected $framePrevious = null;

/**
* @param Decoder $decoder
*/
Expand Down Expand Up @@ -35,19 +40,32 @@ protected function render(Frame $frame, $index)
{
if ($index == 0) {
$screenSize = $this->decoder->getScreenSize();
$this->frameCurrent = imagecreatetruecolor(
$screenSize->getWidth(),
$screenSize->getHeight()
);
$im = imagecreatetruecolor($screenSize->getWidth(), $screenSize->getHeight());
imagealphablending($im, false);
imagesavealpha($im, true);

$transColor = imagecolortransparent($im, imagecolorallocatealpha($im, 255, 255, 255, 127));
imagefill($im, 0, 0, $transColor);

$this->frameCurrent = $im;
$this->framePrevious = $frame;
$this->copyFrameToBuffer($frame);

return $this->frameCurrent;
}

$disposalMethod = $frame->getDisposalMethod();
imagepalettetotruecolor($this->frameCurrent);
$disposalMethod = $this->framePrevious->getDisposalMethod();
if ($disposalMethod === 0 || $disposalMethod === 1) {
$this->copyFrameToBuffer($frame);
} elseif ($disposalMethod === 2) {
$this->restoreToBackground($this->framePrevious, imagecolortransparent($this->frameCurrent));
$this->copyFrameToBuffer($frame);
} else {
throw new \RuntimeException("Disposal method $disposalMethod is not implemented.");
}

$this->framePrevious = $frame;
return $this->frameCurrent;
}

Expand All @@ -67,4 +85,23 @@ protected function copyFrameToBuffer(Frame $frame)
$frame->getSize()->getHeight()
);
}

/**
* @param Frame $frame
* @param int $backgroundColor
*/
protected function restoreToBackground(Frame $frame, $backgroundColor)
{
$offset = $frame->getOffset();
$size = $frame->getSize();

imagefilledrectangle(
$this->frameCurrent,
$offset->getX(),
$offset->getY(),
$offset->getX() + $size->getWidth() - 1,
$offset->getY() + $size->getHeight() - 1,
$backgroundColor
);
}
}
22 changes: 22 additions & 0 deletions tests/gifs/test1.render.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
"3b54f524edd9a2bc1a41180d50687e5b4eda72fe",
"3679a08a21435c3622143ff0b88113bcec7b8b2a",
"b3ea5433be4f50c5a8348562ca9b06f5c0f7b3c2",
"99d8d61b1a697f785556e8d7df73e5b750234e5f",
"5fee1ed9e408cf09b86a5245dfb219b533deb467",
"be05bf80969cc7e0d42fec95277da0829476b7a9",
"0ce2c8eb3e60212af0a544ccbc3d6d57663a46e9",
"ac6fcbf14b746526acb9f516995484083563c806",
"a8decc3d6035624b31368639ed75fa39d9bd418b",
"62985953aeb459121b86815273501041b3e75cb7",
"555e8fc4aeb8f4a47e54eb61e6dd0bcefe1b0091",
"0ff1472df67264e6b8a9369bacc2be9fe87a4ee5",
"53ecfae62a97e47e6b8e42397b3a4b22c68d25b6",
"be713b7ca26a5c8e01da37a4ca936179895b8938",
"ad6a15b5f94ee6036473b71bc743b96d8e849acf",
"a4bb8b4cac3905d033494d8127db0b6b51cf2199",
"60db7695314f5aa5511f05326d0c0047ba67736b",
"b0e6a85a2cc166fe6bd0c5cd74e5f64ca3a01fbe",
"41d6b86939070007e5b132b782b43a4bf59944f0",
"74ffb88c45deff8b11bd614b86682eaa13177a02"
]
22 changes: 22 additions & 0 deletions tests/gifs/test2.render.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
"3b54f524edd9a2bc1a41180d50687e5b4eda72fe",
"3679a08a21435c3622143ff0b88113bcec7b8b2a",
"b3ea5433be4f50c5a8348562ca9b06f5c0f7b3c2",
"99d8d61b1a697f785556e8d7df73e5b750234e5f",
"5fee1ed9e408cf09b86a5245dfb219b533deb467",
"be05bf80969cc7e0d42fec95277da0829476b7a9",
"0ce2c8eb3e60212af0a544ccbc3d6d57663a46e9",
"ac6fcbf14b746526acb9f516995484083563c806",
"a8decc3d6035624b31368639ed75fa39d9bd418b",
"62985953aeb459121b86815273501041b3e75cb7",
"555e8fc4aeb8f4a47e54eb61e6dd0bcefe1b0091",
"0ff1472df67264e6b8a9369bacc2be9fe87a4ee5",
"53ecfae62a97e47e6b8e42397b3a4b22c68d25b6",
"be713b7ca26a5c8e01da37a4ca936179895b8938",
"ad6a15b5f94ee6036473b71bc743b96d8e849acf",
"a4bb8b4cac3905d033494d8127db0b6b51cf2199",
"60db7695314f5aa5511f05326d0c0047ba67736b",
"b0e6a85a2cc166fe6bd0c5cd74e5f64ca3a01fbe",
"41d6b86939070007e5b132b782b43a4bf59944f0",
"74ffb88c45deff8b11bd614b86682eaa13177a02"
]
18 changes: 18 additions & 0 deletions tests/gifs/test3.render.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
"044e1fbd021fdd9c2a499a12cdb99d544925f15b",
"f4bf4b72ced3228855349e83316b3c595d53b35b",
"40a926318bd573f6f9659f62113069903980b0d5",
"29d2985b66263d0c2087f31d0a00ead75a2ffba1",
"f5fc9834c47c04a2498c10e2c296749980e62b44",
"f08ac3040ebca2ed7d056f33f6ffdfd38621bb2a",
"f829752510fc87896db3bd8b4c0abd27a6707b7c",
"485a7110ce3f81b596d47e346ab8eba4b1278e6f",
"64dfd3383aacc43722ea067123de3c84eaa16b1b",
"e07e470fad05139bd969737228f1b7e417185714",
"936669fb503af192bd1166c708e9a06144889f2c",
"523cac25f94b5f692827b72ea792f77fe8d0dc7b",
"78a3f12b2b1ed9b8191cddc10fe32282a90d635f",
"7ed5c4176a01d416954032637f48bd5b897f0702",
"4501c946431debc69184ab21e8a8aeed647cf813",
"eb76df749b207797807800309d20fbd04e7ccff5"
]
169 changes: 169 additions & 0 deletions tests/gifs/test4.render.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
[
"1203bbc2bb15c9e6f96b4a852812ae0f77f36c8e",
"66d10de6a373747421280a092110f3ccacada6ed",
"a4ee902f9af470e91ea8e69bf061c25e894aa30d",
"5b8967e1267a68a93acdb9d39ecf6ed008c57cd9",
"8478c6f539b2eac63bb4e9ab1a8f43a0cfdce5ff",
"d96fa24a0b867eddc81985dca2f007643c7ca2d0",
"4935a69c4c5b0a74e8d9f6cbca69ba9b2e4a47df",
"aa8b067c3a8cbedc1fbe09b24a2599bd611b2666",
"7cd4993c570d5f46e1c64a9c397ee512eb8a6eaf",
"21733a8c3a4db4330e17c7e5e394395ca43abe55",
"6715ac720ae8cc451f6ba204c3499bbf0b02cdf8",
"6715ac720ae8cc451f6ba204c3499bbf0b02cdf8",
"2b9fb6210f37faae31d0dbdf052e2984974a8587",
"ca9b117bf011c5b10d207d23937fd4a2c03e6228",
"ca9b117bf011c5b10d207d23937fd4a2c03e6228",
"c0401f49c966963e98ed22706779124285a8d6b8",
"4de2295d38a33dacc4a1560b6c562a993daef4c1",
"06b50fc453f17484567c2eb46ac9633f307d9d39",
"06b50fc453f17484567c2eb46ac9633f307d9d39",
"06b50fc453f17484567c2eb46ac9633f307d9d39",
"b7a53f3e483fd6cc67e330ef06f7d3d0ba31e9bb",
"93d90c3f4b92f44b69158029e924a6d094edc871",
"93d90c3f4b92f44b69158029e924a6d094edc871",
"93d90c3f4b92f44b69158029e924a6d094edc871",
"a92a4e04f798b9aa970063b465007dbd446c057a",
"278f11b9587a5c82554bce98814a597342dfcba7",
"2ea0dac7f430e35dd189dfb31d56574e05c324a9",
"a1596475974e9343f62d102a62e00f872bfa82d3",
"66999e0b740bd6e312343dd951dc06c7a8b9e1e7",
"01554042226f08994f6582a6694be19c41d0c1ce",
"882295ab01685a4f99587caa9ee75d96364a761a",
"28f3c56ed636d9f55cc28d2637aaa0ab6dc3436c",
"487f7e88781e6514bd4d68ccfe209ac3774c0e1e",
"aed0eecf2ee4ada2b008820648dfbb0cc7380721",
"0e0b008e6de30d9bc973b8bf9945090dc95efbaf",
"7ef1e7353ed951e8cab25698af30d50a0aca64a0",
"73488c3526445a5174b97e556eb387eb5a505532",
"0925a3eb6e518af56488120e05daac4bba103867",
"76541a4d8a59e3e81a73c72141a92b0b485a1ecf",
"8a394f8e66d2a1d3e3df3adbc1415cea14fec9d8",
"66a7bc9a176403cb0f2c55f2e97afaf65c22c8e6",
"f105c8d3c5c55d350509cc80f79e05b591d71214",
"e6a1c42b942b7f66fa773db39b7716ed0dacd0da",
"d068eb7bc402aa6d810f725f8e163089e65344fd",
"2f1f22180a51a298d3ec7ab9cb68d8f0f9dd9ab7",
"f13ec5266a367629fe93fa2d5a9056ab6ac31cbf",
"4ddc75bbe7a9f76f09604981916ee212e255f979",
"76be707d9139f2a001c49deafa4b0987f7bf10a2",
"f43b994ae7ac352b5691900f5c6dd5e98b9fd5a7",
"c9471d89aff08dc951c6a80788716808b7f2abcd",
"aefb5d49e86d36d5dcab8220c4b6aad5ebdafcb3",
"feb84208f2bb72709233d7dbd80d37a6dfffac84",
"feb84208f2bb72709233d7dbd80d37a6dfffac84",
"d1de17b693704edf4cf95cd533b087399d94b294",
"6b8d21641a10f65c6230ac9003da6d7dd3179ef9",
"2f3ee265f4dce10529c517c10c0321588549b02c",
"320d005b63171e34066438357229cbef3a40f20f",
"295167ad92bb768d90ed79897904c490be317ef0",
"5c3083a116bfb42db17c87756df8ed792a50bd51",
"1b62d3458b8aaaec76d1c979cdc8424a9965341a",
"e82e6d44fc742324235c352fad239759d44694d6",
"b009aa8ae24d28210d43831bef48d5bc9d2fb4a8",
"b009aa8ae24d28210d43831bef48d5bc9d2fb4a8",
"b009aa8ae24d28210d43831bef48d5bc9d2fb4a8",
"3696be443cd302526496ee6d110575fb9146be54",
"ecba455f20be73790d69fbb7d56963114ae6e7fc",
"dc8b7368559538932b8035019b46cb36a6d95b38",
"8115dc5fe5f3e221067ace67130816150a3143a4",
"65c4d243afff80b38cfe0f193f7ebbbc0dbde3d3",
"5503a68cfdb6f51d28f0ce1fcac28197b7512591",
"18a27a37164c5dc2d4222651c67e9adcb63ae0f0",
"d1576cd972a3aa1bcf0b73e2b0a4ca0539f73a76",
"3eed2397cfe05686d1232539db697a8c02f1e03e",
"308d734b107a88178c121e7a44e0bfae389d8803",
"1b78ee9cfdaa6dadf8a734f3660d018af808c8e1",
"b0ef6beeff5daa5eff80bd141552765fd005ea82",
"b63c853a8b38965f7cbea69a0f431728a5e7c988",
"c1642822071428cef9e9aea54315c79d5884f020",
"03b97b44b9a1ab237d677e6ea6163472f4ba1b3b",
"b25cc9e40abe3d87c9fc0d17d5b475a7b947428a",
"ef4771aef33b92705d8ae494ad03741a04402cac",
"c1c928680931e0779437ec00015c70920d440eb4",
"641ffa6455030313b6a50fa70307daaa9ae2e1bf",
"c11e9f37d998cd5bc4f913bf39e0503e3e2ab650",
"f18916080eee679ceed9f6d376c97bd3e6b22d62",
"70f02f56f7757114d323c131cb224fa182099980",
"07f6bb196c510d5a31b2057dbb6a9500d8cec274",
"e504315fb06145f0f2c640f351a1d8679840f917",
"ac6074dd4b45606d4efa03061199d6bf1c8e83c0",
"e333cd8089f8abc0ab3e5caf0c98f306176d525b",
"a10e858d97aae60bf26a05be4dd4196a319f5c7e",
"05bb307bb5472bef71b6ba9d9d8f82a60375f730",
"53e91381cf251663482337b67edbc20f903d276a",
"71c4e480e0765dffe6c391e6e6c43f15442a1e81",
"cbe58256951deb82fd8aacb8ea420e98513a9bc1",
"eef8fb890822a81a12338235b109f262d7da7aed",
"0a7777395a57474bed134d00dbccdad040e26028",
"4e125197741970bba331d980c5add1f5c6b286b7",
"4a430f3551738bef4fea6c86c9efc283b84b277e",
"ff6fbe78b9656abf2120c5b287269311a99b1713",
"0ddc5be269cf2049a570f1f8a79d632a581418ea",
"dcd616978b356113d6e22b290002bc1bc9a043ee",
"c0bfdad6569413e83e0c18a3b11fba8d630af68a",
"a2c53b2a5048fadb4f528876329f14b9c9c47b25",
"8f5588ce88646480c5671c92ef482623104c7db6",
"647bb50a9f922f3b4b3cb3b596a6acc352a8d4c1",
"8e17b066b3300a7343f81c980f841b389a39a57c",
"75405e4a59832a836e3230ff84ee21acb8633a94",
"f0541fe9cf70f693c67302d20ade5ee195d13690",
"5e5b43931aee065bb5345286e09b781dc9a148f1",
"fe91d92717942c166f93c8b362a5172e3299d5ee",
"ff36ef2121e0a80a52c266a5af15982960e636a3",
"0bdebff88ab1a15bb49dc7ce25a8285c1dfef37f",
"cc0aadaaef1394cc39fd7ba406b311c195ddcece",
"ccd09e2f72dd03c8b52198241ecb49f84d1fdbd5",
"fd90724c73da1bb2f2c3dd344d17eeeb112c3234",
"41ea0938a4bed0e16ce00e159c1dfd4adec72fef",
"05c85c3869b0eebbfd4f7c47311a5f9ece9c8c43",
"812eca551620b71483a6a56aa6c63e40f2051121",
"c00401244a55128e276ed968908dd52a72d2e774",
"7aef18c069f6377a3c9b3c1f182c1152611c5e77",
"bc0e7c84909606e7fd87e46e2cc2aad56d353cec",
"52e0616275f5c32d36015fb4f44d910e77e442c7",
"4a0912f9e8cc61ff94b6ee052412ad92e3b64c65",
"d4d6a7e3741e499238a13c8d3f778a0f2a1ba43f",
"856648ccf90756fc35a4fd9457d50da9e5cc6c4f",
"4e31260ede28f2c2dd2cad8644e79cbe730ddad8",
"888e4878eec3ecca3e5c984edbb4ab1f1eadbf2c",
"c6e736d8d7f1816b4104669230119a9fae84b6b9",
"5700ee7da27384c7463f1d7c8e81d8c9122cae78",
"af6845beccef5d554efd3832b1e6725f719e4a07",
"579a65379c8903e95e2a702313baed2de416a578",
"58ff1bea4749169a3d1ec1f8c16f3d44b2cf427e",
"b8e4f8359a7628f211829e43012264854d8e084e",
"22815460a367cad6d14fb43af572b367efd4e90a",
"1e713434ada3d11a3c053f0e4e4029d1155848aa",
"f51925a34d218af1eb588cb435018442f20f2f58",
"f637cee4b1a884f9d959f931b03e83c735f3c983",
"b3801dd282c97159ddca728561aa363cf4d59f15",
"616b5e8c37ae13692730afade65504963be6b199",
"6d72cd727d7e447ae2282e862475bdde86cce769",
"eb6964beef88cf1f9e51e494b5ae9279671a85bd",
"eb6964beef88cf1f9e51e494b5ae9279671a85bd",
"eb6964beef88cf1f9e51e494b5ae9279671a85bd",
"6813c1b12484517d5d09da7abe48c0148cdf2e2b",
"cc3c7db469467aa335941f057bcdb12806c1de36",
"cc3c7db469467aa335941f057bcdb12806c1de36",
"cc3c7db469467aa335941f057bcdb12806c1de36",
"42bef8dfdab4990701015b69db8842fa7f1823ff",
"3a1e8506dde95970891c354398792c251f86d7aa",
"c2989b75f03aab746657615e89e2d7341340f8c5",
"f6347195c1f30b4b83f6c303e8b18a45cea945c7",
"d5d03130dbaca52465e8f1942bd7f99ce3959a70",
"5f1c334ca34385253241431ff4577afef297ac23",
"c0f3502cc96e4ade9aa14ce1602a9e33df4546e9",
"9b3818d3984d5493b6b8affafad00fc492c03d67",
"5b25266848db63b914de0a431250cef562d56e5b",
"f55b5018a49de9a9e17cd73a1bf9fc1cd10ead4e",
"6fdd67359659e51be1a058291ba7cce38fd3b939",
"eb8b3483ae0ec6c8ee8e0d36a42095d0f823ce02",
"878f37a9012bb3f66079711c8722f862b7ef7e58",
"12deb34620df948428d2dd3c60cfae9783b38997",
"66c32e0e92b60b1bccf45901dcf2be5e857dd147",
"66c32e0e92b60b1bccf45901dcf2be5e857dd147",
"66c32e0e92b60b1bccf45901dcf2be5e857dd147",
"9718c6fe87141cd5d5d707cf449051a84a6834d2",
"52e16eafde75dcdd2821adc3b07e544f3ac86fa7"
]
Loading

0 comments on commit 04e09dd

Please sign in to comment.