C++ memcpy 和 happy 访问冲突

发布于 2024-11-26 12:31:42 字数 1225 浏览 2 评论 0原文

由于某种原因,我无法想象我遇到了访问冲突。

memcpy_s (buffer, bytes_per_line * height, image, bytes_per_line * height);

这是整个函数:

int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)   
{   
    // this function is used to flip bottom-up .BMP images   

    UCHAR *buffer; // used to perform the image processing   
    int index;     // looping index   

    // allocate the temporary buffer   
    if (!(buffer = (UCHAR *) malloc (bytes_per_line * height)))   
        return(0);   

    // copy image to work area   
    //memcpy(buffer, image, bytes_per_line * height);   
    memcpy_s (buffer, bytes_per_line * height, image, bytes_per_line * height);

    // flip vertically   
    for (index = 0; index < height; index++)   
        memcpy(&image[((height - 1) - index) * bytes_per_line], &buffer[index * bytes_per_line], bytes_per_line);   

    // release the memory   
    free(buffer);   

    // return success   
    return(1);   

} // end Flip_Bitmap   

整个代码: http://pastebin.com/udRqgCfU

要运行此程序,您需要在源目录中使用 24 位位图。 这是较大代码的一部分,我正在尝试使 Load_Bitmap_File 函数正常工作...... 那么,有什么想法吗?

For some reason i can't figure i am getting access violation.

memcpy_s (buffer, bytes_per_line * height, image, bytes_per_line * height);

This is whole function:

int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)   
{   
    // this function is used to flip bottom-up .BMP images   

    UCHAR *buffer; // used to perform the image processing   
    int index;     // looping index   

    // allocate the temporary buffer   
    if (!(buffer = (UCHAR *) malloc (bytes_per_line * height)))   
        return(0);   

    // copy image to work area   
    //memcpy(buffer, image, bytes_per_line * height);   
    memcpy_s (buffer, bytes_per_line * height, image, bytes_per_line * height);

    // flip vertically   
    for (index = 0; index < height; index++)   
        memcpy(&image[((height - 1) - index) * bytes_per_line], &buffer[index * bytes_per_line], bytes_per_line);   

    // release the memory   
    free(buffer);   

    // return success   
    return(1);   

} // end Flip_Bitmap   

Whole code:
http://pastebin.com/udRqgCfU

To run this you'll need 24-bit bitmap, in your source directory.
This is a part of a larger code, i am trying to make Load_Bitmap_File function to work...
So, any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

上课铃就是安魂曲 2024-12-03 12:31:42

您遇到访问冲突是因为许多图像程序未正确设置biSizeImage。您正在使用的图像可能将 biSizeImage 设置为 0,因此您不会为图像数据分配任何内存(实际上,您可能会分配 4-16 个字节,因为大多数malloc 实现也会返回非 NULL 值。因此,当您复制数据时,您正在读取该数组的末尾,这会导致访问冲突。

忽略 biSizeImage 参数并自行计算图像大小。请记住,每个扫描线的大小必须是 4 字节的倍数,因此您需要向上舍入:

// Pseudocode
#define ROUNDUP(value, power_of_2) (((value) + (power_of_2) - 1) & (~((power_of_2) - 1)))
bytes_per_line = ROUNDUP(width * bits_per_pixel/8, 4)
image_size = bytes_per_line * height;

然后只需使用相同的图像大小来读取图像数据并翻转它。

You're getting an access violation because a lot of image programs don't set biSizeImage properly. The image you're using probably has biSizeImage set to 0, so you're not allocating any memory for the image data (in reality, you're probably allocating 4-16 bytes, since most malloc implementations will return a non-NULL value even when the requested allocation size is 0). So, when you go to copy the data, you're reading past the ends of that array, which results in the access violation.

Ignore the biSizeImage parameter and compute the image size yourself. Keep in mind that the size of each scan line must be a multiple of 4 bytes, so you need to round up:

// Pseudocode
#define ROUNDUP(value, power_of_2) (((value) + (power_of_2) - 1) & (~((power_of_2) - 1)))
bytes_per_line = ROUNDUP(width * bits_per_pixel/8, 4)
image_size = bytes_per_line * height;

Then just use the same image size for reading in the image data and for flipping it.

爱情眠于流年 2024-12-03 12:31:42

正如评论所说,图像数据不一定是 width*height*bytes_per_pixel

内存访问通常在 32 位边界上更快,并且在处理图像时速度通常很重要。因此,图像的行通常会移位到从 4 字节(32 位)边界开始

如果图像像素是 32 位(即 RGBA),这不是问题,但如果每个像素有 3 字节(24 位颜色),那么对于某些情况图像宽度,其中列数 * 3 不是 4 的倍数,则将在每行的 edn 处插入额外的空白字节。

图像格式可能有一个“步幅”宽度或 elemsize 值来告诉您这一点。

As the comments have said, the image data is not necessarily width*height*bytes_per_pixel

Memory access is generally faster on 32bit boundaries and when dealing with images speed generally matters. Because of this the rows of an image are often shifted to start on a 4byte (32bit) boundary

If the image pixels are 32bit (ie RGBA) this isn't a problem but if you have 3bytes per pixel (24bit colour) then for certain image widths, where the number of columns * 3 isn't a multiple of 4, then extra blank bytes will be inserted at the edn of each row.

The image format probably has a "stride" width or elemsize value to tell you this.

秋千易 2024-12-03 12:31:42

您为 image 分配了 bitmap->bitmapinfoheader.biSizeImage,但继续复制 bitmap->bitmapinfoheader.biWidth * (bitmap->bitmapinfoheader.biBitCount / 8 ) * bitmap->bitmapinfoheader.biHeight 字节数据。我敢打赌这两个数字不一样。

You allocate bitmap->bitmapinfoheader.biSizeImage for image but proceed to copy bitmap->bitmapinfoheader.biWidth * (bitmap->bitmapinfoheader.biBitCount / 8) * bitmap->bitmapinfoheader.biHeight bytes of data. I bet the two numbers aren't the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文