Added ImageRotate (#3078)

* Added ImageRotate

* Quick rename of the example

* Update ImageRotate by changing doubles to floats and checking code convention

* Update API
This commit is contained in:
Dane Madsen 2023-05-24 17:22:51 +10:00 committed by GitHub
parent bf69b38056
commit e465ed0850
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 429 additions and 247 deletions

View File

@ -453,6 +453,7 @@ TEXTURES = \
textures/textures_image_generation \
textures/textures_image_loading \
textures/textures_image_processing \
textures/textures_image_rotate \
textures/textures_image_text \
textures/textures_to_image \
textures/textures_raw_data \

View File

@ -0,0 +1,79 @@
/*******************************************************************************************
*
* raylib [textures] example - Image Rotation
*
* Example originally created with raylib 1.0, last time updated with raylib 1.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2014-2023 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#define NUM_TEXTURES 3
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture rotation");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image image45 = LoadImage("resources/raylib_logo.png");
Image image90 = LoadImage("resources/raylib_logo.png");
Image imageNeg90 = LoadImage("resources/raylib_logo.png");
ImageRotate(&image45, 45);
ImageRotate(&image90, 90);
ImageRotate(&imageNeg90, -90);
Texture2D textures[NUM_TEXTURES] = { 0 };
textures[0] = LoadTextureFromImage(image45);
textures[1] = LoadTextureFromImage(image90);
textures[2] = LoadTextureFromImage(imageNeg90);
int currentTexture = 0;
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsKeyPressed(KEY_RIGHT))
{
currentTexture = (currentTexture + 1)%NUM_TEXTURES; // Cycle between the textures
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTexture(textures[currentTexture], screenWidth/2 - textures[currentTexture].width/2, screenHeight/2 - textures[currentTexture].height/2, WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(textures[i]);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -6757,6 +6757,21 @@
}
]
},
{
"name": "ImageRotate",
"description": "Rotate image by input angle in degrees (-359 to 359) ",
"returnType": "void",
"params": [
{
"type": "Image *",
"name": "image"
},
{
"type": "int",
"name": "degrees"
}
]
},
{
"name": "ImageRotateCW",
"description": "Rotate image clockwise 90deg",

View File

@ -5266,6 +5266,15 @@ return {
{type = "Image *", name = "image"}
}
},
{
name = "ImageRotate",
description = "Rotate image by input angle in degrees (-359 to 359) ",
returnType = "void",
params = {
{type = "Image *", name = "image"},
{type = "int", name = "degrees"}
}
},
{
name = "ImageRotateCW",
description = "Rotate image clockwise 90deg",

File diff suppressed because it is too large Load Diff

View File

@ -656,7 +656,7 @@
<Param type="unsigned int" name="frames" desc="" />
</Callback>
</Callbacks>
<Functions count="517">
<Functions count="518">
<Function name="InitWindow" retType="void" paramCount="3" desc="Initialize window and OpenGL context">
<Param type="int" name="width" desc="" />
<Param type="int" name="height" desc="" />
@ -1685,6 +1685,10 @@
<Function name="ImageFlipHorizontal" retType="void" paramCount="1" desc="Flip image horizontally">
<Param type="Image *" name="image" desc="" />
</Function>
<Function name="ImageRotate" retType="void" paramCount="2" desc="Rotate image by input angle in degrees (-359 to 359) ">
<Param type="Image *" name="image" desc="" />
<Param type="int" name="degrees" desc="" />
</Function>
<Function name="ImageRotateCW" retType="void" paramCount="1" desc="Rotate image clockwise 90deg">
<Param type="Image *" name="image" desc="" />
</Function>

View File

@ -202,6 +202,7 @@ ImageDrawText|void|(Image *dst, Vector2 position, const char *text, int fontSize
ImageDrawTextEx|void|(Image *dst, Vector2 position, Font font, const char *text, float fontSize, float spacing, Color color);|
ImageFlipVertical|void|(Image *image);|
ImageFlipHorizontal|void|(Image *image);|
ImageRotate|void|(Image *image, int degrees);|
ImageRotateCW|void|(Image *image);|
ImageRotateCCW|void|(Image *image);|
ImageColorTint|void|(Image *image, Color color);|

View File

@ -1570,6 +1570,12 @@
<Param name="Image *image" />
</Overload>
</KeyWord>
<KeyWord name="ImageRotate" func="yes">
<Overload retVal="void" descr="Rotate image by input angle in degrees (-359 to 359)">
<Param name="Image *image" />
<Param name="int degrees" />
</Overload>
</KeyWord>
<KeyWord name="ImageRotateCW" func="yes">
<Overload retVal="void" descr="Rotate image clockwise 90deg">
<Param name="Image *image" />

View File

@ -346,6 +346,7 @@ RLAPI void ImageMipmaps(Image *image);
RLAPI void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); // Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
RLAPI void ImageFlipVertical(Image *image); // Flip image vertically
RLAPI void ImageFlipHorizontal(Image *image); // Flip image horizontally
RLAPI void ImageRotate(Image *image, int degrees); // Rotate image by input angle in degrees (-359 to 359)
RLAPI void ImageRotateCW(Image *image); // Rotate image clockwise 90deg
RLAPI void ImageRotateCCW(Image *image); // Rotate image counter-clockwise 90deg
RLAPI void ImageColorTint(Image *image, Color color); // Modify image color: tint

View File

@ -1268,6 +1268,7 @@ RLAPI void ImageMipmaps(Image *image);
RLAPI void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); // Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
RLAPI void ImageFlipVertical(Image *image); // Flip image vertically
RLAPI void ImageFlipHorizontal(Image *image); // Flip image horizontally
RLAPI void ImageRotate(Image *image, int degrees); // Rotate image by input angle in degrees (-359 to 359)
RLAPI void ImageRotateCW(Image *image); // Rotate image clockwise 90deg
RLAPI void ImageRotateCCW(Image *image); // Rotate image counter-clockwise 90deg
RLAPI void ImageColorTint(Image *image, Color color); // Modify image color: tint

View File

@ -2118,6 +2118,65 @@ void ImageFlipHorizontal(Image *image)
}
}
// Rotate image in degrees
void ImageRotate(Image *image, int degrees)
{
// Security check to avoid program crash
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
if (image->mipmaps > 1) TRACELOG(LOG_WARNING, "Image manipulation only applied to base mipmap level");
if (image->format >= PIXELFORMAT_COMPRESSED_DXT1_RGB) TRACELOG(LOG_WARNING, "Image manipulation not supported for compressed formats");
else
{
float rad = degrees * PI / 180.0f;
float sinRadius = sin(rad);
float cosRadius = cos(rad);
int width = abs(image->width * cosRadius) + abs(image->height * sinRadius);
int height = abs(image->height * cosRadius) + abs(image->width * sinRadius);
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
unsigned char *rotatedData = (unsigned char *)RL_CALLOC(width * height, bytesPerPixel);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float oldX = ((x - width / 2.0f) * cosRadius + (y - height / 2.0f) * sinRadius) + image->width / 2.0f;
float oldY = ((y - height / 2.0f) * cosRadius - (x - width / 2.0f) * sinRadius) + image->height / 2.0f;
if (oldX >= 0 && oldX < image->width && oldY >= 0 && oldY < image->height)
{
int x1 = (int)floor(oldX);
int y1 = (int)floor(oldY);
int x2 = MIN(x1 + 1, image->width - 1);
int y2 = MIN(y1 + 1, image->height - 1);
float px = oldX - x1;
float py = oldY - y1;
for (int i = 0; i < bytesPerPixel; i++)
{
float f1 = ((unsigned char *)image->data)[(y1 * image->width + x1) * bytesPerPixel + i];
float f2 = ((unsigned char *)image->data)[(y1 * image->width + x2) * bytesPerPixel + i];
float f3 = ((unsigned char *)image->data)[(y2 * image->width + x1) * bytesPerPixel + i];
float f4 = ((unsigned char *)image->data)[(y2 * image->width + x2) * bytesPerPixel + i];
float val = f1 * (1 - px) * (1 - py) + f2 * px * (1 - py) + f3 * (1 - px) * py + f4 * px * py;
rotatedData[(y * width + x) * bytesPerPixel + i] = (unsigned char)val;
}
}
}
}
RL_FREE(image->data);
image->data = rotatedData;
image->width = width;
image->height = height;
}
}
// Rotate image clockwise 90deg
void ImageRotateCW(Image *image)
{