source.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "source.h"
  2. // define glib subtype for vips async source
  3. #define VIPS_TYPE_IMGPROXY_SOURCE (vips_imgproxy_source_get_type())
  4. G_DEFINE_FINAL_TYPE(VipsImgproxySource, vips_imgproxy_source, VIPS_TYPE_SOURCE)
  5. extern void closeImgproxyReader(uintptr_t handle);
  6. extern gint64 imgproxyReaderSeek(uintptr_t handle, gint64 offset, int whence);
  7. extern gint64 imgproxyReaderRead(uintptr_t handle, gpointer buffer, gint64 size);
  8. // dereferences source
  9. void
  10. unref_imgproxy_source(VipsImgproxySource *source)
  11. {
  12. VIPS_UNREF(source);
  13. }
  14. // read function for vips imgproxy source
  15. static gint64
  16. vips_imgproxy_source_read(VipsSource *source, void *buffer, size_t length)
  17. {
  18. VipsImgproxySource *self = (VipsImgproxySource *) source;
  19. gint64 read_length = imgproxyReaderRead(self->readerHandle, buffer, length);
  20. if (read_length < 0) {
  21. vips_error("vips_imgproxy_source_read", "failed to read from imgproxy source");
  22. }
  23. return read_length;
  24. }
  25. // seek function for vips imgproxy source. whence can be SEEK_SET (0), SEEK_CUR (1), or SEEK_END (2).
  26. static gint64
  27. vips_imgproxy_source_seek(VipsSource *source, gint64 offset, int whence)
  28. {
  29. VipsImgproxySource *self = (VipsImgproxySource *) source;
  30. gint64 actual_offset = imgproxyReaderSeek(self->readerHandle, offset, whence);
  31. if (actual_offset < 0) {
  32. vips_error("vips_imgproxy_source_seek", "failed to seek in imgproxy source");
  33. }
  34. return actual_offset;
  35. }
  36. static void
  37. vips_imgproxy_source_dispose(GObject *gobject)
  38. {
  39. VipsImgproxySource *source = (VipsImgproxySource *) gobject;
  40. closeImgproxyReader(source->readerHandle);
  41. G_OBJECT_CLASS(vips_imgproxy_source_parent_class)->dispose(gobject);
  42. }
  43. // attaches seek/read handlers to the imgproxy source class
  44. static void
  45. vips_imgproxy_source_class_init(VipsImgproxySourceClass *klass)
  46. {
  47. GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
  48. VipsObjectClass *object_class = VIPS_OBJECT_CLASS(klass);
  49. VipsSourceClass *source_class = VIPS_SOURCE_CLASS(klass);
  50. object_class->nickname = "imgproxy_source";
  51. object_class->description = "imgproxy input source";
  52. gobject_class->dispose = vips_imgproxy_source_dispose;
  53. source_class->read = vips_imgproxy_source_read;
  54. source_class->seek = vips_imgproxy_source_seek;
  55. }
  56. // initializes the imgproxy source (nothing to do here yet)
  57. static void
  58. vips_imgproxy_source_init(VipsImgproxySource *source)
  59. {
  60. }
  61. // creates a new imgproxy source with the given reader handle
  62. VipsImgproxySource *
  63. vips_new_imgproxy_source(uintptr_t readerHandle)
  64. {
  65. VipsImgproxySource *source = g_object_new(VIPS_TYPE_IMGPROXY_SOURCE, NULL);
  66. source->readerHandle = readerHandle;
  67. return source;
  68. }