要在 C 中正确解析二进制 Netpbm 格式,您需要首先读取文件或流的前两个字符,以检测格式。其余的标头值都是非负整数,可以使用单个函数进行扫描,该函数也会跳过注释行。如果我们使用 C I/O 设施,那么我们可以使用单字符推回设施轻松编写该函数;在伪代码中,
Function pnm_value(stream):
Read one character from stream into c
Loop:
If c == EOF:
Premature end of input; fail.
If c == '#':
Loop:
Read one character from stream into c
If c is not EOF or '\n', break loop
End loop
Continue at the start of the outer loop
If c is a '\t', '\n', '\v', '\f', '\r', or ' ':
Read one character from stream into c
Continue at the start of the outer loop
Otherwise break loop
End loop
If c is not a digit:
Invalid input; fail
Value = 0
While c is a digit:
OldValue = Value
Value = 10*value + (value of digit c)
If (Value / 10 != OldValue):
Value is too large; fail
Read one character from stream into c
End While
If c is not EOF:
Push (unget) c back to stream
Return Value
End function
直方图均衡化 https://en.wikipedia.org/wiki/Histogram_equalization是一种操作,其中每个像素的每个颜色分量都单独缩放,使用一个简单的函数使直方图尽可能平坦。您可以找到更多实际示例和解释(例如this PDF https://www.math.uci.edu/icamp/courses/math77c/demos/hist_eq.pdf)与您最喜欢的搜索引擎。
当您读取像素的红色、绿色和蓝色分量并将它们缩放到 0..1048575 范围(含)时,您可以计算Y/Cb/Cr https://en.wikipedia.org/wiki/YCbCr and H/S/I https://en.wikipedia.org/wiki/HSI_color_space#Formal_derivation例如,使用各自维基百科文章中显示的公式。您可以使用整数或浮点数进行计算,但请记住,您需要确定直方图的大小(因此最终将每个分量转换为整数)。为了避免颜色转换中的量化错误,您应该在这些“临时”颜色空间中为每个组件使用更多的位 - 例如,24 位听起来不错。