{"id":2979,"date":"2018-03-01T00:01:37","date_gmt":"2018-03-01T08:01:37","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2979"},"modified":"2018-03-08T08:08:50","modified_gmt":"2018-03-08T16:08:50","slug":"increasing-brightness","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2979","title":{"rendered":"Increasing Brightness"},"content":{"rendered":"<p>Recently, I did some graphics programming. The task I gave myself was to highlight a JPEG by drawing a box around an interesting part of the image. The puzzle was which color to make the box so that it would stand out.<br \/>\n<!--more--><br \/>\nMy first color choice was black, but if the image is mostly black, the box wouldn&#8217;t show up. The same logic follows for white and other colors as well.<\/p>\n<p>For example, I tried bright green, but the results were ugly. I tried a box made of two rectangles, one white and one black, but on a larger image canvas, this box wasn&#8217;t that obvious. So my solution was instead to use existing pixels to draw the box, making each pixel brighter by 10 percent.<\/p>\n<p>In a JPEG image, pixels are stored as three values: red, green, and blue. Each value ranges from 0 to 255, which in C fits into an <em>unsigned char<\/em> variable. Ten percent of 255 is 25, so for each color value in the pixel, I would add 25 to increase brightness. (I&#8217;m sure that nerdy graphics programmers would claim that this method doesn&#8217;t increase brightness but <em>blah-blah-blah<\/em>. Whatever. Work with me here.)<\/p>\n<p>The problem, of course, is that you cannot increase a pixel&#8217;s color element value beyond 255; if you add 25 to a pixel value of 250, for example, you get 275. This result is an overflow. In C, adding 25 to an <em>unsigned char<\/em> value of 250 yields 19, not 275; the value wraps within the confines of the <em>unsigned char<\/em>.<\/p>\n<p>This month&#8217;s Exercise is to take a line of pixels stored in a structure and increase each value by 10 percent without going over 255. Use the code below as a skeleton to craft your solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    struct color {\r\n        unsigned char r;\r\n        unsigned char g;\r\n        unsigned char b;\r\n    };\r\n    struct color pixels[15] = {\r\n        {247, 63, 29},{251,100, 33},{250,146, 38},\r\n        {252,196, 46},{252,223, 51},{250,225, 56},\r\n        {196,225, 53},{134,252, 49},{ 66,221, 48},\r\n        { 63,251,158},{ 60,253,233},{ 54,236,250},\r\n        { 35,181,249},{  0,104,249},{  0, 54,251}\r\n    };\r\n\r\n<span class=\"comments\">\/* your solution here *\/<\/span>\r\n\r\n    return(0);\r\n}<\/pre>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2991\">Click here<\/a> to view my solution, but please work on this puzzle yourself before you see what I&#8217;ve done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can&#8217;t fill up a cup beyond its brim. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2979\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2979","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2979"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2979\/revisions"}],"predecessor-version":[{"id":3011,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2979\/revisions\/3011"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}