image_hash_matcher.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "image_hash_matcher.h"
  2. /**
  3. * vips_image_read_to_memory: converts VipsImage to RGBA format and reads into memory buffer
  4. * @in: VipsImage to convert and read
  5. * @buf: pointer to buffer pointer (will be allocated)
  6. * @size: pointer to size_t to store the buffer size
  7. *
  8. * Converts the VipsImage to RGBA format using VIPS operations and reads the raw pixel data.
  9. * The caller is responsible for freeing the buffer using vips_memory_buffer_free().
  10. *
  11. * Returns: 0 on success, -1 on error
  12. */
  13. int
  14. vips_image_read_from_to_memory(void *in, size_t in_size, void **out, size_t *out_size, int *out_width, int *out_height)
  15. {
  16. if (!in || !out || !out_size || !out_width || !out_height) {
  17. vips_error("vips_image_read_from_to_memory", "invalid arguments");
  18. return -1;
  19. }
  20. VipsImage *base = vips_image_new_from_buffer(in, in_size, "", NULL);
  21. if (base == NULL) {
  22. return -1;
  23. }
  24. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  25. // Initialize output parameters
  26. *out = NULL;
  27. *out_size = 0;
  28. // Convert to sRGB colorspace first if needed
  29. if (vips_colourspace(base, &t[0], VIPS_INTERPRETATION_sRGB, NULL) != 0) {
  30. VIPS_UNREF(base);
  31. vips_error("vips_image_read_from_to_memory", "failed to convert to sRGB");
  32. return -1;
  33. }
  34. in = t[0];
  35. // Add alpha channel if not present (convert to RGBA)
  36. if (!vips_image_hasalpha(base)) {
  37. // Add alpha channel
  38. if (vips_addalpha(base, &t[1], NULL) != 0) {
  39. VIPS_UNREF(base);
  40. vips_error("vips_image_read_from_to_memory", "failed to add alpha channel");
  41. return -1;
  42. }
  43. in = t[1];
  44. }
  45. // Get raw pixel data, width and height
  46. *out = vips_image_write_to_memory(in, out_size);
  47. *out_width = base->Xsize;
  48. *out_height = base->Ysize;
  49. // Dispose the image regardless of the result
  50. VIPS_UNREF(base);
  51. if (*out == NULL) {
  52. vips_error("vips_image_read_from_to_memory", "failed to write image to memory");
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. /**
  58. * vips_memory_buffer_free: frees memory buffer allocated by vips_image_write_to_memory
  59. * @buf: buffer pointer to free
  60. *
  61. * Frees the memory buffer allocated by vips_image_write_to_memory.
  62. * Safe to call with NULL pointer.
  63. */
  64. void
  65. vips_memory_buffer_free(void *buf)
  66. {
  67. if (buf) {
  68. g_free(buf);
  69. }
  70. }