Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Well, I have solved the issue with the same look graphs. The issue was that the offset was applied not to the histogram data index when the data for each channel was gathered into separate arrays. And that resulted in wrong data being stored. I have added a change in the question. The graphs look much better now, however, it still looks somewhat incorrect. Mine looks a lot more smooth, and in Photoshop, it looks a bit rougher. I have also added the new histogram output image to the question.