You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.6 KiB

  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [textures] example - Image processing
  4. --
  5. -- NOTE: Images are loaded in CPU memory (RAM) textures are loaded in GPU memory (VRAM)
  6. --
  7. -- This example has been created using raylib 1.6 (www.raylib.com)
  8. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. --
  10. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  11. --
  12. -------------------------------------------------------------------------------------------
  13. --#include <stdlib.h> -- Required for: free()
  14. NUM_PROCESSES = 8
  15. -- enum ImageProcess
  16. local COLOR_NONE = 1
  17. local COLOR_GRAYSCALE = 2
  18. local COLOR_TINT = 3
  19. local COLOR_INVERT = 4
  20. local COLOR_CONTRAST = 5
  21. local COLOR_BRIGHTNESS = 6
  22. local FLIP_VERTICAL = 7
  23. local FLIP_HORIZONTAL = 8
  24. local processText = {
  25. "NO PROCESSING",
  26. "COLOR GRAYSCALE",
  27. "COLOR TINT",
  28. "COLOR INVERT",
  29. "COLOR CONTRAST",
  30. "COLOR BRIGHTNESS",
  31. "FLIP VERTICAL",
  32. "FLIP HORIZONTAL"
  33. }
  34. -- Initialization
  35. -------------------------------------------------------------------------------------------
  36. local screenWidth = 800
  37. local screenHeight = 450
  38. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing")
  39. -- NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  40. local image = LoadImage("resources/parrots.png") -- Loaded in CPU memory (RAM)
  41. image = ImageFormat(image, TextureFormat.UNCOMPRESSED_R8G8B8A8) -- Format image to RGBA 32bit (required for texture update)
  42. local texture = LoadTextureFromImage(image) -- Image converted to texture, GPU memory (VRAM)
  43. local currentProcess = COLOR_NONE
  44. local textureReload = false
  45. local selectRecs = {}
  46. for i = 1, NUM_PROCESSES do selectRecs[i] = Rectangle(40, 50 + 32*i, 150, 30) end
  47. SetTargetFPS(60)
  48. -------------------------------------------------------------------------------------------
  49. -- Main game loop
  50. while not WindowShouldClose() do -- Detect window close button or ESC key
  51. -- Update
  52. ---------------------------------------------------------------------------------------
  53. if (IsKeyPressed(KEY.DOWN)) then
  54. currentProcess = currentProcess + 1
  55. if (currentProcess > NUM_PROCESSES) then currentProcess = 1 end
  56. textureReload = true
  57. elseif (IsKeyPressed(KEY.UP)) then
  58. currentProcess = currentProcess - 1
  59. if (currentProcess < 1) then currentProcess = NUM_PROCESSES end
  60. textureReload = true
  61. end
  62. if (textureReload) then
  63. UnloadImage(image) -- Unload current image data
  64. image = LoadImage("resources/parrots.png") -- Re-load image data
  65. -- NOTE: Image processing is a costly CPU process to be done every frame,
  66. -- If image processing is required in a frame-basis, it should be done
  67. -- with a texture and by shaders
  68. if (currentProcess == COLOR_GRAYSCALE) then image = ImageColorGrayscale(image)
  69. elseif (currentProcess == COLOR_TINT) then image = ImageColorTint(image, GREEN)
  70. elseif (currentProcess == COLOR_INVERT) then image = ImageColorInvert(image)
  71. elseif (currentProcess == COLOR_CONTRAST) then image = ImageColorContrast(image, -40)
  72. elseif (currentProcess == COLOR_BRIGHTNESS) then image = ImageColorBrightness(image, -80)
  73. elseif (currentProcess == FLIP_VERTICAL) then image = ImageFlipVertical(image)
  74. elseif (currentProcess == FLIP_HORIZONTAL) then image = ImageFlipHorizontal(image)
  75. end
  76. local pixels = {}
  77. pixels = GetImageData(image) -- Get pixel data from image (RGBA 32bit)
  78. texture = UpdateTexture(texture, pixels) -- Update texture with new image data
  79. textureReload = false
  80. end
  81. ---------------------------------------------------------------------------------------
  82. -- Draw
  83. ---------------------------------------------------------------------------------------
  84. BeginDrawing()
  85. ClearBackground(RAYWHITE)
  86. DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY)
  87. -- Draw rectangles
  88. for i = 1, NUM_PROCESSES do
  89. if (i == currentProcess) then
  90. DrawRectangleRec(selectRecs[i], SKYBLUE)
  91. DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, BLUE)
  92. DrawText(processText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(processText[i], 10)//2, selectRecs[i].y + 11, 10, DARKBLUE)
  93. else
  94. DrawRectangleRec(selectRecs[i], LIGHTGRAY)
  95. DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, GRAY)
  96. DrawText(processText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(processText[i], 10)//2, selectRecs[i].y + 11, 10, DARKGRAY)
  97. end
  98. end
  99. DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE)
  100. DrawRectangleLines(screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, texture.width, texture.height, BLACK)
  101. EndDrawing()
  102. ---------------------------------------------------------------------------------------
  103. end
  104. -- De-Initialization
  105. -------------------------------------------------------------------------------------------
  106. UnloadTexture(texture) -- Unload texture from VRAM
  107. UnloadImage(image) -- Unload image from RAM
  108. CloseWindow() -- Close window and OpenGL context
  109. -------------------------------------------------------------------------------------------