{"action":"created","comment":{"url":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/comments/96611036","pull_request_review_id":17196703,"id":96611036,"diff_hunk":"@@ -347,27 +271,74 @@ STATIC mp_obj_t microbit_image_make_new(const mp_obj_type_t *type_in, mp_uint_t\n     }\n }\n \n-STATIC microbit_image_obj_t *image_crop(microbit_image_obj_t *img, mp_int_t x, mp_int_t y, mp_int_t w, mp_int_t h) {\n+static void clear_rect(greyscale_t *img, mp_int_t x0, mp_int_t y0,mp_int_t x1, mp_int_t y1) {\n+    for (int i = x0; i < x1; ++i) {\n+        for (int j = y0; j < y1; ++j) {\n+            img->setPixelValue(i, j, 0);\n+        }\n+    }\n+}\n+\n+STATIC void image_blit(microbit_image_obj_t *src, greyscale_t *dest, mp_int_t x, mp_int_t y, mp_int_t w, mp_int_t h, mp_int_t xdest, mp_int_t ydest) {\n     if (w < 0)\n         w = 0;\n     if (h < 0)\n         h = 0;\n-    greyscale_t *result = greyscale_new(w, h);\n-    mp_int_t intersect_x0 = max(0, x);   \n-    mp_int_t intersect_y0 = max(0, y);\n-    mp_int_t intersect_x1 = min(img->width(), x+w);\n-    mp_int_t intersect_y1 = min(img->height(), y+h);\n-    /* If the cropped image is larger than the intersection then\n-     * make sure that all other pixels are set to 0 */\n-    if (w > intersect_x1 - intersect_x0 || h > intersect_y1 - intersect_y0) {\n-        result->clear();\n-    }\n-    for (int i = intersect_x0; i < intersect_x1; ++i) {\n-        for (int j = intersect_y0; j < intersect_y1; ++j) {\n-            int val = img->getPixelValue(i, j);\n-            result->setPixelValue(i-x, j-y, val);\n+    mp_int_t intersect_x0 = max(max(0, x), -xdest);\n+    mp_int_t intersect_y0 = max(max(0, y), -ydest);\n+    mp_int_t intersect_x1 = min(min(dest->width+x-xdest, src->width()), x+w);\n+    mp_int_t intersect_y1 = min(min(dest->height+y-ydest, src->height()), y+h);\n+    mp_int_t xstart, xend, ystart, yend, xdel, ydel;\n+    mp_int_t clear_x0 = max(0, xdest);\n+    mp_int_t clear_y0 = max(0, ydest);\n+    mp_int_t clear_x1 = min(dest->width, xdest+w);\n+    mp_int_t clear_y1 = min(dest->height, ydest+h);\n+    if (intersect_x0 >= intersect_x1 || intersect_y0 >= intersect_y1) {\n+        // Nothing to copy\n+        clear_rect(dest, clear_x0, clear_y0, clear_x1, clear_y1);\n+        return;\n+    }\n+    if (x > xdest) {\n+        xstart = intersect_x0; xend = intersect_x1; xdel = 1;\n+    } else {\n+        xstart = intersect_x1-1; xend = intersect_x0-1; xdel = -1;\n+    }\n+    if (y > ydest) {\n+        ystart = intersect_y0; yend = intersect_y1; ydel = 1;\n+    } else {\n+        ystart = intersect_y1-1; yend = intersect_y0-1; ydel = -1;\n+    }\n+    for (int i = xstart; i != xend; i += xdel) {\n+        for (int j = ystart; j != yend; j += ydel) {\n+            int val = src->getPixelValue(i, j);\n+            dest->setPixelValue(i+xdest-x, j+ydest-y, val);\n         }\n     }\n+    // Adjust intersection rectange to dest\n+    intersect_x0 += xdest-x;\n+    intersect_y0 += ydest-y;\n+    intersect_x1 += xdest-x;\n+    intersect_y1 += ydest-y;\n+    // Clear four rectangles in the cleared area surrounding the copied area.\n+    clear_rect(dest, clear_x0, clear_y0, intersect_x0, intersect_y1);","path":"source/microbit/microbitimage.cpp","position":151,"original_position":151,"commit_id":"0767fd27e08b0dc86a34c08642103ff9e20fcba7","original_commit_id":"0767fd27e08b0dc86a34c08642103ff9e20fcba7","user":{"login":"markshannon","id":9448417,"avatar_url":"https://avatars.githubusercontent.com/u/9448417?v=3","gravatar_id":"","url":"https://api.github.com/users/markshannon","html_url":"https://github.com/markshannon","followers_url":"https://api.github.com/users/markshannon/followers","following_url":"https://api.github.com/users/markshannon/following{/other_user}","gists_url":"https://api.github.com/users/markshannon/gists{/gist_id}","starred_url":"https://api.github.com/users/markshannon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markshannon/subscriptions","organizations_url":"https://api.github.com/users/markshannon/orgs","repos_url":"https://api.github.com/users/markshannon/repos","events_url":"https://api.github.com/users/markshannon/events{/privacy}","received_events_url":"https://api.github.com/users/markshannon/received_events","type":"User","site_admin":false},"body":"Not if you are blitting an image onto itself.","created_at":"2017-01-18T11:05:45Z","updated_at":"2017-01-18T11:05:45Z","html_url":"https://github.com/bbcmicrobit/micropython/pull/401#discussion_r96611036","pull_request_url":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/401","_links":{"self":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/comments/96611036"},"html":{"href":"https://github.com/bbcmicrobit/micropython/pull/401#discussion_r96611036"},"pull_request":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/401"}}},"pull_request":{"url":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/401","id":101607462,"html_url":"https://github.com/bbcmicrobit/micropython/pull/401","diff_url":"https://github.com/bbcmicrobit/micropython/pull/401.diff","patch_url":"https://github.com/bbcmicrobit/micropython/pull/401.patch","issue_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/401","number":401,"state":"open","locked":false,"title":"Add blit method to image class.","user":{"login":"markshannon","id":9448417,"avatar_url":"https://avatars.githubusercontent.com/u/9448417?v=3","gravatar_id":"","url":"https://api.github.com/users/markshannon","html_url":"https://github.com/markshannon","followers_url":"https://api.github.com/users/markshannon/followers","following_url":"https://api.github.com/users/markshannon/following{/other_user}","gists_url":"https://api.github.com/users/markshannon/gists{/gist_id}","starred_url":"https://api.github.com/users/markshannon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markshannon/subscriptions","organizations_url":"https://api.github.com/users/markshannon/orgs","repos_url":"https://api.github.com/users/markshannon/repos","events_url":"https://api.github.com/users/markshannon/events{/privacy}","received_events_url":"https://api.github.com/users/markshannon/received_events","type":"User","site_admin":false},"body":"I've also simplified the rest of the image code by implementing crop and shift_xxx in terms of blit.\r\n\r\nFixes https://github.com/bbcmicrobit/micropython/issues/395\r\n","created_at":"2017-01-15T19:52:51Z","updated_at":"2017-01-18T11:05:45Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a089f824afa2058a7c120171c2b183b0a582e848","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/401/commits","review_comments_url":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/401/comments","review_comment_url":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/401/comments","statuses_url":"https://api.github.com/repos/bbcmicrobit/micropython/statuses/0767fd27e08b0dc86a34c08642103ff9e20fcba7","head":{"label":"markshannon:image-blit","ref":"image-blit","sha":"0767fd27e08b0dc86a34c08642103ff9e20fcba7","user":{"login":"markshannon","id":9448417,"avatar_url":"https://avatars.githubusercontent.com/u/9448417?v=3","gravatar_id":"","url":"https://api.github.com/users/markshannon","html_url":"https://github.com/markshannon","followers_url":"https://api.github.com/users/markshannon/followers","following_url":"https://api.github.com/users/markshannon/following{/other_user}","gists_url":"https://api.github.com/users/markshannon/gists{/gist_id}","starred_url":"https://api.github.com/users/markshannon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markshannon/subscriptions","organizations_url":"https://api.github.com/users/markshannon/orgs","repos_url":"https://api.github.com/users/markshannon/repos","events_url":"https://api.github.com/users/markshannon/events{/privacy}","received_events_url":"https://api.github.com/users/markshannon/received_events","type":"User","site_admin":false},"repo":{"id":44632019,"name":"micropython","full_name":"markshannon/micropython","owner":{"login":"markshannon","id":9448417,"avatar_url":"https://avatars.githubusercontent.com/u/9448417?v=3","gravatar_id":"","url":"https://api.github.com/users/markshannon","html_url":"https://github.com/markshannon","followers_url":"https://api.github.com/users/markshannon/followers","following_url":"https://api.github.com/users/markshannon/following{/other_user}","gists_url":"https://api.github.com/users/markshannon/gists{/gist_id}","starred_url":"https://api.github.com/users/markshannon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markshannon/subscriptions","organizations_url":"https://api.github.com/users/markshannon/orgs","repos_url":"https://api.github.com/users/markshannon/repos","events_url":"https://api.github.com/users/markshannon/events{/privacy}","received_events_url":"https://api.github.com/users/markshannon/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/markshannon/micropython","description":"Port of MicroPython for the BBC micro:bit","fork":true,"url":"https://api.github.com/repos/markshannon/micropython","forks_url":"https://api.github.com/repos/markshannon/micropython/forks","keys_url":"https://api.github.com/repos/markshannon/micropython/keys{/key_id}","collaborators_url":"https://api.github.com/repos/markshannon/micropython/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/markshannon/micropython/teams","hooks_url":"https://api.github.com/repos/markshannon/micropython/hooks","issue_events_url":"https://api.github.com/repos/markshannon/micropython/issues/events{/number}","events_url":"https://api.github.com/repos/markshannon/micropython/events","assignees_url":"https://api.github.com/repos/markshannon/micropython/assignees{/user}","branches_url":"https://api.github.com/repos/markshannon/micropython/branches{/branch}","tags_url":"https://api.github.com/repos/markshannon/micropython/tags","blobs_url":"https://api.github.com/repos/markshannon/micropython/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/markshannon/micropython/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/markshannon/micropython/git/refs{/sha}","trees_url":"https://api.github.com/repos/markshannon/micropython/git/trees{/sha}","statuses_url":"https://api.github.com/repos/markshannon/micropython/statuses/{sha}","languages_url":"https://api.github.com/repos/markshannon/micropython/languages","stargazers_url":"https://api.github.com/repos/markshannon/micropython/stargazers","contributors_url":"https://api.github.com/repos/markshannon/micropython/contributors","subscribers_url":"https://api.github.com/repos/markshannon/micropython/subscribers","subscription_url":"https://api.github.com/repos/markshannon/micropython/subscription","commits_url":"https://api.github.com/repos/markshannon/micropython/commits{/sha}","git_commits_url":"https://api.github.com/repos/markshannon/micropython/git/commits{/sha}","comments_url":"https://api.github.com/repos/markshannon/micropython/comments{/number}","issue_comment_url":"https://api.github.com/repos/markshannon/micropython/issues/comments{/number}","contents_url":"https://api.github.com/repos/markshannon/micropython/contents/{+path}","compare_url":"https://api.github.com/repos/markshannon/micropython/compare/{base}...{head}","merges_url":"https://api.github.com/repos/markshannon/micropython/merges","archive_url":"https://api.github.com/repos/markshannon/micropython/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/markshannon/micropython/downloads","issues_url":"https://api.github.com/repos/markshannon/micropython/issues{/number}","pulls_url":"https://api.github.com/repos/markshannon/micropython/pulls{/number}","milestones_url":"https://api.github.com/repos/markshannon/micropython/milestones{/number}","notifications_url":"https://api.github.com/repos/markshannon/micropython/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/markshannon/micropython/labels{/name}","releases_url":"https://api.github.com/repos/markshannon/micropython/releases{/id}","deployments_url":"https://api.github.com/repos/markshannon/micropython/deployments","created_at":"2015-10-20T20:07:34Z","updated_at":"2016-02-20T23:14:47Z","pushed_at":"2017-01-15T21:59:24Z","git_url":"git://github.com/markshannon/micropython.git","ssh_url":"git@github.com:markshannon/micropython.git","clone_url":"https://github.com/markshannon/micropython.git","svn_url":"https://github.com/markshannon/micropython","homepage":"","size":16550,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"bbcmicrobit:master","ref":"master","sha":"37315cbba85b84a91c8cc6275b0261517c3fd7f6","user":{"login":"bbcmicrobit","id":15104236,"avatar_url":"https://avatars.githubusercontent.com/u/15104236?v=3","gravatar_id":"","url":"https://api.github.com/users/bbcmicrobit","html_url":"https://github.com/bbcmicrobit","followers_url":"https://api.github.com/users/bbcmicrobit/followers","following_url":"https://api.github.com/users/bbcmicrobit/following{/other_user}","gists_url":"https://api.github.com/users/bbcmicrobit/gists{/gist_id}","starred_url":"https://api.github.com/users/bbcmicrobit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbcmicrobit/subscriptions","organizations_url":"https://api.github.com/users/bbcmicrobit/orgs","repos_url":"https://api.github.com/users/bbcmicrobit/repos","events_url":"https://api.github.com/users/bbcmicrobit/events{/privacy}","received_events_url":"https://api.github.com/users/bbcmicrobit/received_events","type":"Organization","site_admin":false},"repo":{"id":40358251,"name":"micropython","full_name":"bbcmicrobit/micropython","owner":{"login":"bbcmicrobit","id":15104236,"avatar_url":"https://avatars.githubusercontent.com/u/15104236?v=3","gravatar_id":"","url":"https://api.github.com/users/bbcmicrobit","html_url":"https://github.com/bbcmicrobit","followers_url":"https://api.github.com/users/bbcmicrobit/followers","following_url":"https://api.github.com/users/bbcmicrobit/following{/other_user}","gists_url":"https://api.github.com/users/bbcmicrobit/gists{/gist_id}","starred_url":"https://api.github.com/users/bbcmicrobit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbcmicrobit/subscriptions","organizations_url":"https://api.github.com/users/bbcmicrobit/orgs","repos_url":"https://api.github.com/users/bbcmicrobit/repos","events_url":"https://api.github.com/users/bbcmicrobit/events{/privacy}","received_events_url":"https://api.github.com/users/bbcmicrobit/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bbcmicrobit/micropython","description":"Port of MicroPython for the BBC micro:bit","fork":false,"url":"https://api.github.com/repos/bbcmicrobit/micropython","forks_url":"https://api.github.com/repos/bbcmicrobit/micropython/forks","keys_url":"https://api.github.com/repos/bbcmicrobit/micropython/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bbcmicrobit/micropython/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bbcmicrobit/micropython/teams","hooks_url":"https://api.github.com/repos/bbcmicrobit/micropython/hooks","issue_events_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/events{/number}","events_url":"https://api.github.com/repos/bbcmicrobit/micropython/events","assignees_url":"https://api.github.com/repos/bbcmicrobit/micropython/assignees{/user}","branches_url":"https://api.github.com/repos/bbcmicrobit/micropython/branches{/branch}","tags_url":"https://api.github.com/repos/bbcmicrobit/micropython/tags","blobs_url":"https://api.github.com/repos/bbcmicrobit/micropython/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bbcmicrobit/micropython/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bbcmicrobit/micropython/git/refs{/sha}","trees_url":"https://api.github.com/repos/bbcmicrobit/micropython/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bbcmicrobit/micropython/statuses/{sha}","languages_url":"https://api.github.com/repos/bbcmicrobit/micropython/languages","stargazers_url":"https://api.github.com/repos/bbcmicrobit/micropython/stargazers","contributors_url":"https://api.github.com/repos/bbcmicrobit/micropython/contributors","subscribers_url":"https://api.github.com/repos/bbcmicrobit/micropython/subscribers","subscription_url":"https://api.github.com/repos/bbcmicrobit/micropython/subscription","commits_url":"https://api.github.com/repos/bbcmicrobit/micropython/commits{/sha}","git_commits_url":"https://api.github.com/repos/bbcmicrobit/micropython/git/commits{/sha}","comments_url":"https://api.github.com/repos/bbcmicrobit/micropython/comments{/number}","issue_comment_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/comments{/number}","contents_url":"https://api.github.com/repos/bbcmicrobit/micropython/contents/{+path}","compare_url":"https://api.github.com/repos/bbcmicrobit/micropython/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bbcmicrobit/micropython/merges","archive_url":"https://api.github.com/repos/bbcmicrobit/micropython/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bbcmicrobit/micropython/downloads","issues_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues{/number}","pulls_url":"https://api.github.com/repos/bbcmicrobit/micropython/pulls{/number}","milestones_url":"https://api.github.com/repos/bbcmicrobit/micropython/milestones{/number}","notifications_url":"https://api.github.com/repos/bbcmicrobit/micropython/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bbcmicrobit/micropython/labels{/name}","releases_url":"https://api.github.com/repos/bbcmicrobit/micropython/releases{/id}","deployments_url":"https://api.github.com/repos/bbcmicrobit/micropython/deployments","created_at":"2015-08-07T11:58:53Z","updated_at":"2017-01-11T00:01:10Z","pushed_at":"2017-01-17T10:04:27Z","git_url":"git://github.com/bbcmicrobit/micropython.git","ssh_url":"git@github.com:bbcmicrobit/micropython.git","clone_url":"https://github.com/bbcmicrobit/micropython.git","svn_url":"https://github.com/bbcmicrobit/micropython","homepage":"","size":16349,"stargazers_count":196,"watchers_count":196,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":91,"mirror_url":null,"open_issues_count":72,"forks":91,"open_issues":72,"watchers":196,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/401"},"html":{"href":"https://github.com/bbcmicrobit/micropython/pull/401"},"issue":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/issues/401"},"comments":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/issues/401/comments"},"review_comments":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/401/comments"},"review_comment":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/401/commits"},"statuses":{"href":"https://api.github.com/repos/bbcmicrobit/micropython/statuses/0767fd27e08b0dc86a34c08642103ff9e20fcba7"}}}}
{"actor":{"display_login":"markshannon"}}