source.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. return imgproxyReaderRead(self->readerHandle, buffer, length);
  20. }
  21. // seek function for vips imgproxy source. whence can be SEEK_SET (0), SEEK_CUR (1), or SEEK_END (2).
  22. static gint64
  23. vips_imgproxy_source_seek(VipsSource *source, gint64 offset, int whence)
  24. {
  25. VipsImgproxySource *self = (VipsImgproxySource *) source;
  26. return imgproxyReaderSeek(self->readerHandle, offset, whence);
  27. }
  28. static void
  29. vips_imgproxy_source_dispose(GObject *gobject)
  30. {
  31. VipsImgproxySource *source = (VipsImgproxySource *) gobject;
  32. closeImgproxyReader(source->readerHandle);
  33. G_OBJECT_CLASS(vips_imgproxy_source_parent_class)->dispose(gobject);
  34. }
  35. // attaches seek/read handlers to the imgproxy source class
  36. static void
  37. vips_imgproxy_source_class_init(VipsImgproxySourceClass *klass)
  38. {
  39. GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
  40. VipsObjectClass *object_class = VIPS_OBJECT_CLASS(klass);
  41. VipsSourceClass *source_class = VIPS_SOURCE_CLASS(klass);
  42. object_class->nickname = "imgproxy_source";
  43. object_class->description = "imgproxy input source";
  44. gobject_class->dispose = vips_imgproxy_source_dispose;
  45. source_class->read = vips_imgproxy_source_read;
  46. source_class->seek = vips_imgproxy_source_seek;
  47. }
  48. // initializes the imgproxy source (nothing to do here yet)
  49. static void
  50. vips_imgproxy_source_init(VipsImgproxySource *source)
  51. {
  52. }
  53. // creates a new imgproxy source with the given reader handle
  54. VipsImgproxySource *
  55. vips_new_imgproxy_source(uintptr_t readerHandle)
  56. {
  57. VipsImgproxySource *source = g_object_new(VIPS_TYPE_IMGPROXY_SOURCE, NULL);
  58. source->readerHandle = readerHandle;
  59. return source;
  60. }