[英]How to flip Y axis of pixel data
我正在學習 C++ 和收集后從 glMapBuffer 到數組的數據我想在 y 軸上翻轉數據
unsigned char * Data = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER,
GL_READ_ONLY);
char firstarray[ length * width * 4] ;
memcpy( firstarray , Data , sizeof( firstarray ));
現在我想在 y 軸上翻轉 firstarray 的數據。
我確實嘗試過,但我無法正確計算數學。
好吧,如果能做到這一點會好得多。 你實際上只是產生了一個XY問題......
適當的訪問器函數可能如下所示:
unsigned char* getPixel(unsigned int row, unsigend int column)
{
return array + (row * width + column) * 4;
}
unsigned char* getSubPixel(unsigned int row, unsigend int column, unsigned int color)
{
return getPixel(row, column) + color;
}
我想在 y 軸上翻轉緩沖區
假設您要生成在 x 軸上鏡像的新圖像,您可以簡單地
std::swap(*getSubPixel(x, y, 0), *getSubPixel(x, width - y, 0))
// same for the other three sub-pixels
// if you decide to return references instead of pointers, you don't need
// to dereference (can skip the asterisks)
對於行的一半中的每個像素(必須是一半,否則您將交換所有值兩次,從而再次產生相同的圖像)和每一行。
在堆棧上分配的那個大小的數組很可能會發生stack-overflow
(雙關語),因此請改用向量。
同樣從* 4
(和維度)我認為您正在使用數組來存儲圖像,因此您可以創建一個結構來存儲最內部的維度,例如:
struct rgba //or color
{
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
};
然后創建一個包含顏色結構(在我的示例中為rgba
)的 std:: vector : std::vector<rgba> img;
你像這樣使用它:
int length = 1080;
int width = 1920;
std::vector<rgba> img(length * width);
for (int i = 0; i != length; i++)
for (int j = 0; j != width; j++)
img[i * width + j] = { 255, 255, 255, 255 };
或者您可以查看一些進行圖像處理的庫,例如OpenCV
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.