{"id":2991,"date":"2018-03-08T00:01:26","date_gmt":"2018-03-08T08:01:26","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2991"},"modified":"2018-03-03T12:37:36","modified_gmt":"2018-03-03T20:37:36","slug":"increasing-brightness-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2991","title":{"rendered":"Increasing Brightness &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2979\">this month&#8217;s Exercise<\/a> is to fill a cup without overflowing it. That cup is an <em>unsigned char<\/em> value, which goes only to 255 maximum. If your goal is increase the value of an <em>unsigned char<\/em> variable by 10 percent, any value over <code>255 - (255\/10)<\/code>, or <code>220<\/code>, must be set to 255.<br \/>\n<!--more--><br \/>\nObviously, some sore of <em>if<\/em> test is required to ensure that you don&#8217;t overflow the pixel value beyond 255. For my solution, I used the tertiary operator, <code>?:<\/code>. Here&#8217;s a sample statement:<\/p>\n<p><code>pixels[i].r = pixels[i].r+br &gt; 255 ? 255 : pixels[i].r+br<\/code><\/p>\n<p>Variable <code>pixels[i].r<\/code> represents the red pixel of element <code>i<\/code> in the <code>pixels<\/code> structure array. Its value is set to its current value plus the value of <code>br<\/code>, calculated as:<\/p>\n<p><code>br = 255\/10;<\/code><\/p>\n<p>The first part of the tertiary operator compares the result of adding 10 percent to the pixel&#8217;s value with 255: <code>pixels[i].r+br&gt;255<\/code>. This operation does not add <code>br<\/code> to the pixel&#8217;s value; it&#8217;s only compared.<\/p>\n<p>After the <code>?<\/code> operator you see the TRUE and FALSE conditions, respectively. If the result of adding <code>br<\/code> to the pixel&#8217;s value is greater than 255, the value 255 is assigned to <code>pixels[i].r<\/code>. That value fits into the range for an <em>unsigned char<\/em> variable &mdash; no overflow.<\/p>\n<p>If the result of adding <code>br<\/code> to the pixel&#8217;s value is less than 255, meaning the comparison is false, the value of <code>br<\/code> is added to the pixel&#8217;s value and assigned as the pixel&#8217;s value.<\/p>\n<p>The net effect of both operations is to either maximize the pixel&#8217;s value to 255 or increase it by 10 percent. This process is repeated for the blue and green pixel members of the <code>pixels[]<\/code> structure array.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/02\/03exercise.c\">Click here<\/a> to view my code. You&#8217;ll see that I used the direct result of the tertiary operator instead of assigning the values to the structure array.<\/p>\n<p>Here&#8217;s output from a sample run:<\/p>\n<pre><code>247  63  29 -> 255  88  54\r\n251 100  33 -> 255 125  58\r\n250 146  38 -> 255 171  63\r\n252 196  46 -> 255 221  71\r\n252 223  51 -> 255 248  76\r\n250 225  56 -> 255 250  81\r\n196 225  53 -> 221 250  78\r\n134 252  49 -> 159 255  74\r\n 66 221  48 ->  91 246  73\r\n 63 251 158 ->  88 255 183\r\n 60 253 233 ->  85 255 255\r\n 54 236 250 ->  79 255 255\r\n 35 181 249 ->  60 206 255\r\n  0 104 249 ->  25 129 255\r\n  0  54 251 ->  25  79 255<\/code><\/pre>\n<p>You don&#8217;t see any values greater than 255 in any of the columns, which means it worked.<\/p>\n<p>Of course, the real test is to see how this solution succeeds graphically, as applied to an image. In Figure 1, I used the same equation from my solution in code that draws a highlight box around the sun in the sunset image.<\/p>\n<div id=\"attachment_2993\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2993\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/02\/ventura-box.jpg\" alt=\"\" width=\"550\" height=\"413\" class=\"size-full wp-image-2993\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/02\/ventura-box.jpg 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/02\/ventura-box-300x225.jpg 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/02\/ventura-box-400x300.jpg 400w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-2993\" class=\"wp-caption-text\">Figure 1. The results of highlighting a box in a JPEG image.<\/p><\/div>\n<p>Of course, you can see that the value 10 percent could be increased to further highlight the box. Also, this 10-percent effect might not work when the image is mostly white or saturated. Still, this was the optimal solution for the exercise I was given.<\/p>\n<p>If your solution yields similar results, great! An <em>if-else<\/em> comparison also works. To be honest, the primary reason I chose the tertiary operator for my solution is that I could easily copy and paste it on a single line, which made it easier to manipulate all the rows and columns of pixels.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to fill a cup without overflowing it. That cup is an unsigned char value, which goes only to 255 maximum. If your goal is increase the value of an unsigned char variable by &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2991\">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":[5],"tags":[],"class_list":["post-2991","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2991","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=2991"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2991\/revisions"}],"predecessor-version":[{"id":3009,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2991\/revisions\/3009"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}