如何解决在 SDL2 中垂直翻转表面
我得到的最接近的是这个:
for dist in data["dcode"].unique():
d_data = data[data["dcode"] == dist]
#print(dist)
for block in d_data["mandal"].unique():
prev_rain = 0
prev_to_date = "01/12/2022"
for each in rain_dev_input:
#[(rain_dev_input["TERM"] == "DEFICIT RAINFALL") & (rain_dev_input["disT_CODE"] == 240)]:
#print(each)
distcode = each["disT_CODE"]
#print(distcode)
term = str(each["TERM"])
if (distcode == dist) & (term == "EXCESS RAINFALL"):
start_date = each["FROM_PERIOD"]
end_date = each["TO_PERIOD"]
s_date = datetime.datetime.strptime(start_date,"%d/%m/%y")
e_date = datetime.datetime.strptime(end_date,"%d/%m/%y")
#s_date = start_date.strftime(start_date,"%Y-%m-%d")
#e_date = end_date.strftime(end_date,"%Y-%m-%d")
#count1 = daterange(s_date,e_date)
#print(s_date,": ",e_date)
m_data = d_data[d_data["mandal"] == block]
p_data = m_data.loc[start_date:end_date]
for singledate in daterange(s_date,e_date):
#print("Inside Excess Rain")
from_date = datetime.datetime.strftime(singledate,"%Y-%m-%d")
to_date = datetime.datetime.strftime(
(singledate + timedelta(2)),"%Y-%m-%d"
)
total_rain = p_data.loc[from_date:to_date]["rain"].sum()
#print(total_rain)
range1 = float(each["RANGE1"])
range2 = float(each["RANGE2"])
if (total_rain >= range1) & (total_rain < range2):
#print("inside write to file")
if (from_date <= prev_to_date <= to_date) & (prev_rain <= total_rain):
temp["Max"] = total_rain;
prev_rain = total_rain
prev_to_date = to_date
temp = dict()
temp["district"] = each["disT_NAME"]
temp["mandal"] = block
temp[
"category"
] = "excess rainfall for 3 consecutive days,cumulative"
temp["rainfall"] = total_rain
temp["from_date"] = from_date
temp["to_date"] = to_date
temp["phase"] = each["PHASE"]
#temp["insurance"] = insurance_excessrain(each,total_rain)
temp["insurance"] = (total_rain - range1)* each["PAYOUT"]
excess_rainfall.append(temp)
# %%
# output excess rainfall rain_dev_list data
excess_rain = pd.DataFrame(excess_rainfall)
excess_rain.to_csv(str(output_folder) + "/excess_rainfall_2020_h2.csv",index=False)
但它并没有保持透明度。我如何才能真正实现这一目标?
解决方法
我不知道错误到底在哪里,我在我的机器上尝试了你的代码,它在我使用的图像上运行良好。我怀疑您的代码确实保留了透明度,但稍后在您的实现中将其删除。
无论如何,如果我可以建议对您的代码进行改进:您不需要如此复杂的操作来垂直翻转表面。 SDL_Surface
结构以行优先顺序存储像素数据,这意味着 pixels
数组是一系列行,其中每一行的大小为 pitch
字节。因此,要垂直翻转您的表面,您可以简单地遍历行并交换它们。这种方法的优点是不需要了解像素格式,因此可以对所有图像类型(alpha 通道或非 alpha 通道)实现,并且实现起来非常简单。
这是一个您可以编译和试验的最小示例:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
void flip_surface(SDL_Surface* surface)
{
SDL_LockSurface(surface);
int pitch = surface->pitch; // row size
char* temp = new char[pitch]; // intermediate buffer
char* pixels = (char*) surface->pixels;
for(int i = 0; i < surface->h / 2; ++i) {
// get pointers to the two rows to swap
char* row1 = pixels + i * pitch;
char* row2 = pixels + (surface->h - i - 1) * pitch;
// swap rows
memcpy(temp,row1,pitch);
memcpy(row1,row2,pitch);
memcpy(row2,temp,pitch);
}
delete[] temp;
SDL_UnlockSurface(surface);
}
int main(int argc,char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface* surface = IMG_Load("image.png");
flip_surface(surface);
IMG_SavePNG(surface,"result.png");
SDL_Quit();
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。