如何增加/减少无符号字符?
我试图通过以数值方式增加/减少一定量的值来修改像素值(每个通道 RGBA 8 位)。我怎样才能在 Objective-C 或 C 中做到这一点?以下代码每次都会生成“错误:EXC_BAD_ACCESS”。
// Try to Increase RED by 50
for(int i = 0; i < myLength; i += 4) {
//NSLog prints the values FINE as integers
NSLog(@"(%i/%i/%i)", rawData[i], rawData[i+1], rawData[i+2]);
//But for some reason I cannot do this
rawData[i]+=50;
}
甚至
// Try to set RED to 50
for(int i = 0; i < myLength; i += 4) {
//I cannot even do this...
unsigned char newVal = 50;
rawData[i] = 50;
}
旁注: rawData 是 unsigned char 类型的数据缓冲区
I am trying to modify pixel values (8 bits per channel RGBA) by numerically increasing/decreasing the values by a certain amount. How can I do this in Objective-C or C? The following code generates a "Error: EXC_BAD_ACCESS" everytime.
// Try to Increase RED by 50
for(int i = 0; i < myLength; i += 4) {
//NSLog prints the values FINE as integers
NSLog(@"(%i/%i/%i)", rawData[i], rawData[i+1], rawData[i+2]);
//But for some reason I cannot do this
rawData[i]+=50;
}
and even
// Try to set RED to 50
for(int i = 0; i < myLength; i += 4) {
//I cannot even do this...
unsigned char newVal = 50;
rawData[i] = 50;
}
Sidenote: rawData is a data buffer of type unsigned char
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能超出了分配的缓冲区的末尾,这就是您遇到访问冲突的原因。这很可能意味着您的分配数学错误,或者您的 rawData 指针的类型错误。
如果您正在访问加载的 UIImage 的原始数据,它可能会以只读方式映射到内存中。您很可能需要将数据复制到您分配的缓冲区中。
It's possible that you're overrunning the end of your allocated buffer, and that's why you're getting the access violation. That most likely means that your math is wrong in the allocation, or your rawData pointer is of the wrong type.
If you are accessing the raw data of a loaded UIImage, it might be mapped into memory read-only. You'd need to copy the data into a buffer that you allocated, most likely.
嗯...什么是
原始数据
?也许它是一个您无法修改的const
类型?Hmm... What's
rawdata
? Maybe it's aconst
type which you can not modify?