载入中...
搜索中...
未找到
UvPaintRegion.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <utility>
5#include "image/ImageData.h"
7
8// UvPaintRegion.cpp
9namespace eve::image {
10
11Result<void> UvPaintRegion::prepareResult(int width, int height, float centerU, float centerV, float brushRadiusU,
12 float brushRadiusV, float red, float green, float blue, float alpha,
13 bool flipV) {
14 auto prepared = prepareUvPaintRegionResult(width, height, centerU, centerV, brushRadiusU, brushRadiusV, red, green,
15 blue, alpha, flipV);
16 if (!prepared.ok()) return Result<void>::failure(prepared.status());
17 *this = std::move(prepared).takeValue();
18 return Result<void>::success();
19}
20
21Result<UvPaintRegion> prepareUvPaintRegionResult(int targetWidth, int targetHeight, float u, float v, float radiusU,
22 float radiusV, float r, float g, float b, float a, bool flipV) {
23 const auto invalid = [](const char* message, const char* path) {
26 };
27 if (targetWidth <= 0 || targetHeight <= 0) return invalid("paint target dimensions must be positive", "targetSize");
28 if (!std::isfinite(u) || !std::isfinite(v) || !std::isfinite(radiusU) || !std::isfinite(radiusV) || radiusU < 0.f ||
29 radiusV < 0.f)
30 return invalid("UV center and radii must be finite; radii must be non-negative", "brush");
31 if (!std::isfinite(r) || !std::isfinite(g) || !std::isfinite(b) || !std::isfinite(a) || r < 0.f || r > 1.f ||
32 g < 0.f || g > 1.f || b < 0.f || b > 1.f || a < 0.f || a > 1.f)
33 return invalid("paint color channels must be finite values in [0,1]", "color");
34
36 region.targetWidth = targetWidth;
37 region.targetHeight = targetHeight;
38 region.u = std::clamp(u, 0.f, 1.f);
39 region.v = std::clamp(v, 0.f, 1.f);
40 region.radiusU = radiusU;
41 region.radiusV = radiusV;
42 region.r = r;
43 region.g = g;
44 region.b = b;
45 region.a = a;
46
47 const float pixelU = region.u * float(targetWidth - 1);
48 const float mappedV = flipV ? 1.f - region.v : region.v;
49 const float pixelV = mappedV * float(targetHeight - 1);
50 region.centerX = static_cast<int>(std::lround(pixelU));
51 region.centerY = static_cast<int>(std::lround(pixelV));
52 region.minX = std::clamp(static_cast<int>(std::floor(pixelU - radiusU * targetWidth)), 0, targetWidth - 1);
53 region.maxX = std::clamp(static_cast<int>(std::ceil(pixelU + radiusU * targetWidth)), 0, targetWidth - 1);
54 region.minY = std::clamp(static_cast<int>(std::floor(pixelV - radiusV * targetHeight)), 0, targetHeight - 1);
55 region.maxY = std::clamp(static_cast<int>(std::ceil(pixelV + radiusV * targetHeight)), 0, targetHeight - 1);
57}
58
59} // namespace eve::image
60
61// UvPaintSession.cpp
62namespace eve::image {
63// namespace
64
67
69 if (image.getFormat() != "RGBA8")
70 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Unsupported, "UV painting requires RGBA8 pixels",
71 "image", {}, "image.uvPaintSession"));
72 original_ = std::make_unique<ImageData>(image);
73 current_ = std::make_unique<ImageData>(image);
74 undo_.clear();
75 ++revision_;
76 return Result<void>::success();
77}
78
79Result<UvPaintReceipt> UvPaintSession::paintCircleResult(float u, float v, float radiusPixels, float r, float g,
80 float b, float a, bool wrapU, bool wrapV) {
81 if (!current_)
83 "UV paint session is not initialized", "session", {},
84 "image.uvPaintSession"));
85 auto candidate = std::make_unique<ImageData>(*current_);
86 auto painted = candidate->paintCircleUv(u, v, radiusPixels, ImageData::Colorf{r, g, b, a}, wrapU, wrapV);
87 if (!painted.ok()) return painted;
88 undo_.push_back(std::move(current_));
89 if (undo_.size() > 32u) undo_.erase(undo_.begin());
90 current_ = std::move(candidate);
91 ++revision_;
92 return painted;
93}
94
96 if (!current_)
98 "UV paint session is not initialized", "session", {},
99 "image.uvPaintSession"));
100 if (undo_.empty())
102 "UV paint undo history is empty", "undo", {},
103 "image.uvPaintSession"));
104 current_ = std::move(undo_.back());
105 undo_.pop_back();
106 ++revision_;
107 return Result<void>::success();
108}
109
111 if (!original_)
113 "UV paint session is not initialized", "session", {},
114 "image.uvPaintSession"));
115 current_ = std::make_unique<ImageData>(*original_);
116 undo_.clear();
117 ++revision_;
118 return Result<void>::success();
119}
120
122 if (!current_)
124 "UV paint session is not initialized", "session", {},
125 "image.uvPaintSession"));
126 original_ = std::make_unique<ImageData>(*current_);
127 undo_.clear();
128 ++revision_;
129 return Result<void>::success();
130}
131
133 if (!current_)
135 "UV paint session is not initialized",
136 "session", {}, "image.uvPaintSession"));
137 return Result<std::unique_ptr<ImageData>>::success(std::make_unique<ImageData>(*current_));
138}
139
141 if (!current_)
143 "UV paint session is not initialized", "session", {},
144 "image.uvPaintSession"));
145 ImageData replacement(*current_);
146 destination.adopt(replacement);
147 return Result<void>::success();
148}
149
150} // namespace eve::image
ScriptFieldRegion region
std::string message
tensor::Graph g
Definition GpuGraph.cpp:7
vk::UniqueImage image
float u
Definition Grass.cpp:233
double r
float v
std::uint32_t height
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::string path
Definition PlayHost.cpp:111
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Represents raw pixel data.
Definition ImageData.h:38
medialoader::Colorf Colorf
Definition ImageData.h:41
void adopt(eve::Resource &replacement) override
Replace this instance's pixels with replacement's (cache reload). The replacement is drained; its des...
Definition ImageData.cpp:62
Result< void > undoResult()
Restore the previous committed paint transaction.
Result< std::unique_ptr< ImageData > > currentImageResult() const
Return a caller-owned current pixel snapshot.
Result< void > initializeResult(const ImageData &image)
Initialize with an owning RGBA8 copy and clear history.
UvPaintSession()
Uv paint session.
Result< void > copyCurrentToResult(ImageData &destination) const
Atomically copy current pixels into caller-owned RGBA8 storage.
Result< UvPaintReceipt > paintCircleResult(float u, float v, float radiusPixels, float r, float g, float b, float a, bool wrapU=false, bool wrapV=false)
Paint one circular UV brush as a single undoable transaction.
~UvPaintSession()
Uv paint session.
Result< void > restoreResult()
Restore the initial/baked pixels and clear history.
Result< void > bakeResult()
Make current pixels the new restore point and clear history.
Result< UvPaintRegion > prepareUvPaintRegionResult(int targetWidth, int targetHeight, float u, float v, float radiusU, float radiusV, float r, float g, float b, float a, bool flipV)
Validate and prepare the canonical clipped region for a UV brush.
Reusable renderer-neutral bounds for one normalized UV paint operation.
Result< void > prepareResult(int width, int height, float centerU, float centerV, float brushRadiusU, float brushRadiusV, float red, float green, float blue, float alpha, bool flipV=true)
Atomically replace this reusable scratch with a validated region.
int height() const noexcept
Inclusive clipped height in pixels.
int width() const noexcept
Inclusive clipped width in pixels.