Added ImageRotate
This commit is contained in:
parent
bf69b38056
commit
3585081e10
|
|
@ -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 \
|
||||
|
|
|
|||
79
examples/textures/textures_image_rotate.c
Normal file
79
examples/textures/textures_image_rotate.c
Normal 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 loading and drawing");
|
||||
|
||||
// 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;
|
||||
}
|
||||
BIN
examples/textures/textures_image_rotate.png
Normal file
BIN
examples/textures/textures_image_rotate.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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);|
|
||||
|
|
|
|||
|
|
@ -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" />
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
double rad = degrees * PI / 180.0;
|
||||
double sin_rad = sin(rad);
|
||||
double cos_rad = cos(rad);
|
||||
|
||||
int new_width = abs(image->width * cos_rad) + abs(image->height * sin_rad);
|
||||
int new_height = abs(image->height * cos_rad) + abs(image->width * sin_rad);
|
||||
|
||||
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
|
||||
unsigned char *rotatedData = (unsigned char *)RL_CALLOC(new_width * new_height, bytesPerPixel);
|
||||
|
||||
for (int y = 0; y < new_height; y++)
|
||||
{
|
||||
for (int x = 0; x < new_width; x++)
|
||||
{
|
||||
double oldX = ((x - new_width / 2.0) * cos_rad + (y - new_height / 2.0) * sin_rad) + image->width / 2.0;
|
||||
double oldY = ((y - new_height / 2.0) * cos_rad - (x - new_width / 2.0) * sin_rad) + image->height / 2.0;
|
||||
|
||||
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);
|
||||
|
||||
double px = oldX - x1;
|
||||
double py = oldY - y1;
|
||||
|
||||
for (int i = 0; i < bytesPerPixel; i++)
|
||||
{
|
||||
double f1 = ((unsigned char *)image->data)[(y1 * image->width + x1) * bytesPerPixel + i];
|
||||
double f2 = ((unsigned char *)image->data)[(y1 * image->width + x2) * bytesPerPixel + i];
|
||||
double f3 = ((unsigned char *)image->data)[(y2 * image->width + x1) * bytesPerPixel + i];
|
||||
double f4 = ((unsigned char *)image->data)[(y2 * image->width + x2) * bytesPerPixel + i];
|
||||
|
||||
double val = f1 * (1 - px) * (1 - py) + f2 * px * (1 - py) + f3 * (1 - px) * py + f4 * px * py;
|
||||
|
||||
rotatedData[(y * new_width + x) * bytesPerPixel + i] = (unsigned char)val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RL_FREE(image->data);
|
||||
image->data = rotatedData;
|
||||
image->width = new_width;
|
||||
image->height = new_height;
|
||||
}
|
||||
}
|
||||
|
||||
// Rotate image clockwise 90deg
|
||||
void ImageRotateCW(Image *image)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user