Skip to content

Commit

Permalink
kramv - fix slot counts so viewer doesn't crash, more C++ conversion,…
Browse files Browse the repository at this point in the history
… add isSourcePremultiplied

isPremulRGB wasn't getting flagged as a premul file in the props, so now it does.  This means the viewer won't try to apply premul again.
  • Loading branch information
alecazam committed Jan 26, 2023
1 parent 8f508ee commit 55559a8
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 57 deletions.
44 changes: 22 additions & 22 deletions kramv/KramViewerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ using namespace NAMESPACE_STL;

#define ArrayCount(x) (sizeof(x) / sizeof(x[0]))

#ifdef NDEBUG
bool doPrintPanZoom = false;
#else
bool doPrintPanZoom = false;
#endif
//#ifdef NDEBUG
//bool doPrintPanZoom = false;
//#else
//bool doPrintPanZoom = false;
//#endif

// Writing out to rgba32 for sampling, but unorm formats like ASTC and RGBA8
// are still off and need to use the following.
Expand Down Expand Up @@ -677,7 +677,7 @@ Data::Data()
{
_showSettings = new ShowSettings();

_textSlots.resize(2);
_textSlots.resize(kTextSlotCount);
}
Data::~Data()
{
Expand Down Expand Up @@ -2324,14 +2324,14 @@ bool Data::handleEventAction(const Action* action, bool isShiftKeyDown, ActionSt
_showSettings->panY = 0.0f;

text = "Scale Image\n";
if (doPrintPanZoom) {
string tmp;
sprintf(tmp,
"Pan %.3f,%.3f\n"
"Zoom %.2fx\n",
_showSettings->panX, _showSettings->panY, _showSettings->zoom);
text += tmp;
}
// if (doPrintPanZoom) {
// string tmp;
// sprintf(tmp,
// "Pan %.3f,%.3f\n"
// "Zoom %.2fx\n",
// _showSettings->panX, _showSettings->panY, _showSettings->zoom);
// text += tmp;
// }

isChanged = true;
}
Expand All @@ -2350,14 +2350,14 @@ bool Data::handleEventAction(const Action* action, bool isShiftKeyDown, ActionSt
text = "Reload Model\n";
else
text = "Reload Image\n";
if (doPrintPanZoom) {
string tmp;
sprintf(tmp,
"Pan %.3f,%.3f\n"
"Zoom %.2fx\n",
_showSettings->panX, _showSettings->panY, _showSettings->zoom);
text += tmp;
}
// if (doPrintPanZoom) {
// string tmp;
// sprintf(tmp,
// "Pan %.3f,%.3f\n"
// "Zoom %.2fx\n",
// _showSettings->panX, _showSettings->panY, _showSettings->zoom);
// text += tmp;
// }

isChanged = true;
}
Expand Down
6 changes: 4 additions & 2 deletions kramv/KramViewerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,9 @@ enum TextSlot
{
kTextSlotHud,
kTextSlotEyedropper,
kTextSlotAtlas
kTextSlotAtlas,

kTextSlotCount // not a slot
};

struct File {
Expand Down Expand Up @@ -589,6 +591,6 @@ bool isSupportedModelFilename(const char* filename);
bool isSupportedArchiveFilename(const char* filename);
bool isSupportedJsonFilename(const char* filename);

extern bool doPrintPanZoom;
//extern bool doPrintPanZoom;

} // namespace kram
55 changes: 28 additions & 27 deletions kramv/KramViewerMain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -903,22 +903,22 @@ -(void)updateZoom:(float)zoom
float2 ptOrigin = simd::min(pt0.xy, pt1.xy);
float2 ptSize = abs(pt0.xy - pt1.xy);

CGRect imageRect = CGRectMake(ptOrigin.x, ptOrigin.y, ptSize.x, ptSize.y);
CGRect viewRect = CGRectMake(-1.0f, -1.0f, 2.0f, 2.0f);
float4 imageRect = float4m(ptOrigin.x, ptOrigin.y, ptSize.x, ptSize.y);
float4 viewRect = float4m(-1.0f, -1.0f, 2.0f, 2.0f);

int32_t numTexturesX = _showSettings->totalChunks();
int32_t numTexturesY = _showSettings->mipCount;

if (_showSettings->isShowingAllLevelsAndMips) {
imageRect.origin.y -= (numTexturesY - 1) * imageRect.size.height;
imageRect.y -= (numTexturesY - 1) * imageRect.w;

imageRect.size.width *= numTexturesX;
imageRect.size.height *= numTexturesY;
imageRect.z *= numTexturesX; // w
imageRect.w *= numTexturesY; // h
}

float visibleWidth = imageRect.size.width * _showSettings->viewSizeX /
float visibleWidth = imageRect.z * _showSettings->viewSizeX /
_showSettings->viewContentScaleFactor;
float visibleHeight = imageRect.size.height * _showSettings->viewSizeY /
float visibleHeight = imageRect.w * _showSettings->viewSizeY /
_showSettings->viewContentScaleFactor;

// don't allow image to get too big
Expand Down Expand Up @@ -955,12 +955,12 @@ -(void)updateZoom:(float)zoom
}

// or completely off-screen
if (!NSIntersectsRect(imageRect, viewRect)) {
if (!rectIntersectsRect(imageRect, viewRect)) {
isZoomChanged = false;
}

if (!isZoomChanged) {
_zoomGesture.magnification = _validMagnification;
_zoomGesture.magnification = _validMagnification; // objC
return;
}

Expand All @@ -972,25 +972,26 @@ -(void)updateZoom:(float)zoom
_data.doZoomMath(zoom, newPan);

// store this
_validMagnification = _zoomGesture.magnification;
_validMagnification = _zoomGesture.magnification; // objC

_showSettings->zoom = zoom;

_showSettings->panX = newPan.x;
_showSettings->panY = newPan.y;

if (doPrintPanZoom) {
string text;
sprintf(text,
"Pan %.3f,%.3f\n"
"Zoom %.2fx\n",
_showSettings->panX, _showSettings->panY, _showSettings->zoom);
[self setHudText:text.c_str()];
}
// if (doPrintPanZoom) {
// string text;
// sprintf(text,
// "Pan %.3f,%.3f\n"
// "Zoom %.2fx\n",
// _showSettings->panX, _showSettings->panY, _showSettings->zoom);
// [self setHudText:text.c_str()];
// }

// Cause a new sample for eyedropper
_data.updateEyedropper();
self.needsDisplay = YES;

self.needsDisplay = YES; // objC
}
}

Expand Down Expand Up @@ -1248,14 +1249,14 @@ - (void)updatePan:(float)panX panY:(float)panY
_showSettings->panX = panX;
_showSettings->panY = panY;

if (doPrintPanZoom) {
string text;
sprintf(text,
"Pan %.3f,%.3f\n"
"Zoom %.2fx\n",
_showSettings->panX, _showSettings->panY, _showSettings->zoom);
[self setHudText:text.c_str()];
}
// if (doPrintPanZoom) {
// string text;
// sprintf(text,
// "Pan %.3f,%.3f\n"
// "Zoom %.2fx\n",
// _showSettings->panX, _showSettings->panY, _showSettings->zoom);
// [self setHudText:text.c_str()];
// }

// Cause a new sample from Metal to eyeDropper
_data.updateEyedropper();
Expand Down
1 change: 1 addition & 0 deletions libkram/kram/Kram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2896,6 +2896,7 @@ static int32_t kramAppEncode(vector<const char*>& args)
// this means premul the data at read from srgb, this it to match photoshop
else if (isStringEqual(word, "-premulrgb")) {
isPremulRgb = true;
infoArgs.isSourcePremultiplied = true;
}

else if (isStringEqual(word, "-v") ||
Expand Down
2 changes: 1 addition & 1 deletion libkram/kram/KramImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ void KramEncoder::addBaseProps(const ImageInfo& info, KTXImage& dstImage) const
}
else if (info.isSRGBDst) {
// !hasAlpha doesn't change the channel designation
if (info.isPremultiplied) {
if (info.isPremultiplied || info.isSourcePremultiplied) {
dstImage.addChannelProps("Alb.ra,Alb.ga,Alb.ba,Alb.a");
}
else {
Expand Down
16 changes: 11 additions & 5 deletions libkram/kram/KramImageInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,11 +1008,17 @@ void ImageInfo::initWithArgs(const ImageInfoArgs& args)
isKTX2 = args.isKTX2;
compressor = args.compressor;

isPrezero = args.isPrezero;
isPremultiplied = args.isPremultiplied;
if (isPremultiplied)
isPrezero = false;

isPrezero = false;
isPremultiplied = false;
isSourcePremultiplied = false;

if (args.isSourcePremultiplied)
isSourcePremultiplied = true;
else if (args.isPremultiplied)
isPremultiplied = true;
else if (args.isPrezero)
isPrezero = true;

isNormal = args.isNormal;

doSDF = args.doSDF;
Expand Down
2 changes: 2 additions & 0 deletions libkram/kram/KramImageInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ImageInfoArgs {
bool doMipmaps = true; // default to mips on
bool isVerbose = false;
bool doSDF = false;
bool isSourcePremultiplied = false;
bool isPremultiplied = false;
bool isPrezero = false;
bool isNormal = false; // signed, but may be stored unorm and swizzled (f.e. astc/bc3nm gggr or rrrg)
Expand Down Expand Up @@ -138,6 +139,7 @@ class ImageInfo {
bool isSigned = false;
bool isNormal = false;
bool isColorWeighted = false;
bool isSourcePremultiplied = false;
bool isPremultiplied = false; // don't premul
bool isPrezero = false;
bool isHDR = false;
Expand Down

0 comments on commit 55559a8

Please sign in to comment.