Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRAPHICS: Add a function for creating maps to PaletteLookup #5408

Merged
merged 2 commits into from Nov 11, 2023

Conversation

ccawley2011
Copy link
Member

The resulting map can be passed to Graphics::crossBlitMap(), which allows for faster conversion of images with different palettes.

@neuromancer
Copy link
Contributor

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Nov 10, 2023

Walkthrough

Walkthrough

The changes introduce a new function createMap in the PaletteLookup namespace/class across multiple files. This function takes a source palette, its length, and a boolean flag as parameters, and returns a pointer to an array of 32-bit unsigned integers. The function creates a map by iterating through the source palette and finding the best color for each entry using the findBestColor function, which now includes a new boolean parameter.

Changes

File Path Change Summary
graphics/palette.cpp Introduced a new function createMap that takes a source palette, its length, and a boolean flag as parameters. The function returns a pointer to a uint32 array, dynamically allocates memory for the array, and populates it by calling the findBestColor function for each color in the source palette.
graphics/palette.h Added a new function createMap that takes the palette data, the number of palette entries, and a boolean flag as parameters, and returns a pointer to the created map or nullptr. The findBestColor function now includes a new boolean parameter useNaiveAlg with a default value.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Comment on lines 94 to 110
return bestColor;
}

uint32 *PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
if (len <= _paletteSize && memcmp(_palette, srcPalette, len) == 0)
return nullptr;

uint32 *map = new uint32[len];
for (uint i = 0; i < len; i++) {
byte r = *srcPalette++;
byte g = *srcPalette++;
byte b = *srcPalette++;

map[i] = findBestColor(r, g, b, useNaiveAlg);
}
return map;
}
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 10, 2023

Choose a reason for hiding this comment

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

The createMap function allocates memory for the map but does not provide a mechanism to free it. This could lead to memory leaks if the caller does not properly manage the returned pointer. Consider using smart pointers to manage memory automatically.

- uint32 *map = new uint32[len];
+ std::unique_ptr<uint32[]> map(new uint32[len]);

And change the function return type to std::unique_ptr<uint32[]>.


Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
return bestColor;
}
uint32 *PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
if (len <= _paletteSize && memcmp(_palette, srcPalette, len) == 0)
return nullptr;
uint32 *map = new uint32[len];
for (uint i = 0; i < len; i++) {
byte r = *srcPalette++;
byte g = *srcPalette++;
byte b = *srcPalette++;
map[i] = findBestColor(r, g, b, useNaiveAlg);
}
return map;
}
return bestColor;
}
std::unique_ptr<uint32[]> PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
if (len <= _paletteSize && memcmp(_palette, srcPalette, len) == 0)
return nullptr;
std::unique_ptr<uint32[]> map(new uint32[len]);
for (uint i = 0; i < len; i++) {
byte r = *srcPalette++;
byte g = *srcPalette++;
byte b = *srcPalette++;
map[i] = findBestColor(r, g, b, useNaiveAlg);
}
return map;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

@coderabbitai can you check the consideration points for this review to see if there is something we ask you not use in this code review?

Copy link
Contributor

Choose a reason for hiding this comment

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

@neuromancer I apologize for any confusion, but as an AI, I don't have access to specific project guidelines or consideration points for this review beyond the information provided in the pull request and comments. If there are specific guidelines or restrictions on using certain features like smart pointers, could you please provide more details or point me to the relevant documentation? I'm here to help and adjust my suggestions based on your project's requirements.

Copy link
Contributor

Choose a reason for hiding this comment

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

@coderabbitai Please consider the following project guidelines points:

  • We do not use C++ exceptions
  • We do not use the std C++ library in our codebase, therefore, we do not use STL classes in our codebase
  • We have predefined common integer types. Use them instead of rolling your own: byte, int8, uint8, int16, uint16, int32, uint32
  • All the input/output is performed using our own code which is abstracted in backends directory

Then rewrite or discard your current review.

Copy link
Contributor

Choose a reason for hiding this comment

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

@neuromancer Thank you for providing the project guidelines. Given these constraints, my previous suggestion to use std::unique_ptr is not applicable as it's part of the STL.

Considering the guidelines, I suggest the following:

  1. Document the ownership and responsibility of the allocated memory in the function's comments. Make it clear that the caller is responsible for freeing the memory when it's no longer needed.

  2. If there's a common memory management strategy in your codebase, consider applying it here. For example, if you have a custom smart pointer implementation or a garbage collector, use that to manage the memory.

  3. If possible, consider an alternative design where you don't need to return a raw pointer. For example, you could pass in a pre-allocated buffer as an argument to the function, which would then fill it with the map data. This way, the caller would be responsible for both allocation and deallocation, keeping the memory management in one place.

Please let me know if you need further assistance or if there's a specific approach you'd like to discuss.

graphics/palette.cpp Outdated Show resolved Hide resolved
Comment on lines +103 to +105
byte r = *srcPalette++;
byte g = *srcPalette++;
byte b = *srcPalette++;
Copy link
Contributor

Choose a reason for hiding this comment

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

The increment of srcPalette pointer is not checked against the length of the source palette. This could lead to reading beyond the end of the source palette if len is larger than the actual size of the source palette. Consider adding a check to prevent this.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Comment on lines 94 to +110
return bestColor;
}

uint32 *PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
if (len <= _paletteSize && memcmp(_palette, srcPalette, len * 3) == 0)
return nullptr;

uint32 *map = new uint32[len];
for (uint i = 0; i < len; i++) {
byte r = *srcPalette++;
byte g = *srcPalette++;
byte b = *srcPalette++;

map[i] = findBestColor(r, g, b, useNaiveAlg);
}
return map;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The createMap function allocates memory for map but does not provide a mechanism to free it. This could lead to memory leaks. Consider returning a smart pointer (like std::unique_ptr) instead of a raw pointer to ensure that the memory is automatically deallocated when it is no longer needed.

- uint32 *PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
+ std::unique_ptr<uint32[]> PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
...
- uint32 *map = new uint32[len];
+ std::unique_ptr<uint32[]> map(new uint32[len]);
...
- return map;
+ return std::move(map);

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
return bestColor;
}
uint32 *PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
if (len <= _paletteSize && memcmp(_palette, srcPalette, len * 3) == 0)
return nullptr;
uint32 *map = new uint32[len];
for (uint i = 0; i < len; i++) {
byte r = *srcPalette++;
byte g = *srcPalette++;
byte b = *srcPalette++;
map[i] = findBestColor(r, g, b, useNaiveAlg);
}
return map;
}
return bestColor;
}
std::unique_ptr<uint32[]> PaletteLookup::createMap(const byte *srcPalette, uint len, bool useNaiveAlg) {
if (len <= _paletteSize && memcmp(_palette, srcPalette, len * 3) == 0)
return nullptr;
std::unique_ptr<uint32[]> map(new uint32[len]);
for (uint i = 0; i < len; i++) {
byte r = *srcPalette++;
byte g = *srcPalette++;
byte b = *srcPalette++;
map[i] = findBestColor(r, g, b, useNaiveAlg);
}
return std::move(map);
}

@sev-
Copy link
Member

sev- commented Nov 11, 2023

Thank you!

@sev- sev- merged commit c6dbbe4 into scummvm:master Nov 11, 2023
8 checks passed
@ccawley2011 ccawley2011 deleted the palette-lookup-map branch November 11, 2023 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants