Skip to content

Add getImageSize()#3337

Merged
vadi2 merged 3 commits intoMudlet:developmentfrom
vadi2:add-getimagesize
Feb 16, 2020
Merged

Add getImageSize()#3337
vadi2 merged 3 commits intoMudlet:developmentfrom
vadi2:add-getimagesize

Conversation

@vadi2
Copy link
Copy Markdown
Member

@vadi2 vadi2 commented Feb 12, 2020

Brief overview of PR changes/additions

Add getImageSize(). Use it like this:

local width, height = getImageSize("image location here")

Motivation for adding to Mudlet

So you can size a label appropriately when you download an image from the Internet. This is also necessary for an important script on the pkuxkx game.

Other info (issues closed, discussion etc)

Close #813.

I used a new thing here - std::optional, instead of the usual std::pair<bool, result> pattern - because we have access to it now, and it's much cleaner! See https://www.bfilipek.com/2018/05/using-optional.html about it.

@vadi2 vadi2 added this to the 4.5.0 milestone Feb 12, 2020
@vadi2 vadi2 requested a review from a team as a code owner February 12, 2020 19:57
@vadi2 vadi2 requested review from a team February 12, 2020 19:57
@add-deployment-links
Copy link
Copy Markdown

add-deployment-links bot commented Feb 12, 2020

Hey there! Thanks for helping Mudlet improve. 🌟

Test versions

You can directly test the changes here:

No need to install anything - just unzip and run.
Let us know if it works well, and if it doesn't, please give details.

{
QString imageLocation;
if (!lua_isstring(L, 1)) {
lua_pushfstring(L, "getImageSize: bad argument #1 type (image location as string as string expected, got %s!)", luaL_typename(L, 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"as string as string" !!! 😮

lua_pushnumber(L, size->height());
} else {
lua_pushnil(L);
lua_pushfstring(L, "couldn't retrieve image size. Is the location '%s' correct?", imageLocation.toUtf8().constData());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the text is likely to be embedded in a sentence - is it wise to split the text here by inserting a period in the middle? Also, do we want a more nuanced response, that handles all of:

  • the file was not found at all
  • the file was found but doesn't seem to be an image
  • the file was found but it was an image but of null/invalid dimensions

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to get into all of that - but I've fixed the sentence.


std::optional<QSize> mudlet::getImageSize(const QString& imageLocation)
{
QImage image(imageLocation);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apropos a previous comment - imageLocation is supposed to be a pathFileName so would it not be reasonable to check that it is a valid, readable file first, BEFORE trying to read it as an image?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qt will try and fail, and that's what the null check is for later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but won't the image be null both if the file does not exist OR it is an empty (null) image?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll still say we didn't find an image in that very rare case and people will debug from there on

@vadi2
Copy link
Copy Markdown
Member Author

vadi2 commented Feb 14, 2020

@SlySven fixed, have a look again.

Copy link
Copy Markdown
Member

@demonnic demonnic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested working with 4 images, fails properly when the image doesn't exist at the path specified

@vadi2 vadi2 merged commit 5b5a203 into Mudlet:development Feb 16, 2020
@vadi2 vadi2 deleted the add-getimagesize branch February 16, 2020 15:38
vadi2 added a commit that referenced this pull request Feb 17, 2020
* Add getImageSize()

* Tweaks as suggested by peer review on reddit

* Review feedback

(cherry picked from commit 5b5a203)
dicene pushed a commit to dicene/Mudlet that referenced this pull request Feb 19, 2020
* Add getImageSize()

* Tweaks as suggested by peer review on reddit

* Review feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Need a function to get the dimensions of an image

3 participants