icosave.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // ICO saver
  2. #include "vips.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. /* Save a bit of typing.
  7. */
  8. #define UC VIPS_FORMAT_UCHAR
  9. static VipsBandFormat bandfmt_ico[10] = {
  10. /* Band format: UC C US S UI I F X D DX */
  11. /* Promotion: */ UC, UC, UC, UC, UC, UC, UC, UC, UC, UC
  12. };
  13. static uint8_t icon_dir[6] = { 0, 0, 1, 0, 1, 0 }; // ICONDIR header
  14. // ICO file header
  15. typedef struct __attribute__((packed)) _IcoHeader {
  16. uint8_t dir[6]; // ICONDIR
  17. uint8_t width; // Width of the icon in pixels (0 for 256)
  18. uint8_t height; // Height of the icon in pixels (0 for 256
  19. uint8_t number_of_colors; // Number of colors, not used in our case
  20. uint8_t reserved; // Reserved, always 0
  21. uint16_t color_planes; // Color planes, always 1
  22. uint16_t bpp; // Bits per pixel
  23. uint32_t data_size; // Image data size
  24. uint32_t data_offset; // Image data offset, always 22
  25. } IcoHeader;
  26. typedef struct _VipsForeignSaveIco {
  27. VipsForeignSave parent_object;
  28. VipsTarget *target;
  29. VipsPel *line_buffer;
  30. uint16_t bands;
  31. uint32_t line_size;
  32. } VipsForeignSaveIco;
  33. typedef VipsForeignSaveClass VipsForeignSaveIcoClass;
  34. G_DEFINE_ABSTRACT_TYPE(VipsForeignSaveIco, vips_foreign_save_ico,
  35. VIPS_TYPE_FOREIGN_SAVE);
  36. static void
  37. vips_foreign_save_ico_dispose(GObject *gobject)
  38. {
  39. VipsForeignSaveIco *ico = (VipsForeignSaveIco *) gobject;
  40. VIPS_UNREF(ico->target);
  41. G_OBJECT_CLASS(vips_foreign_save_ico_parent_class)->dispose(gobject);
  42. }
  43. static int
  44. vips_foreign_save_ico_build(VipsObject *object)
  45. {
  46. VipsForeignSave *save = (VipsForeignSave *) object;
  47. VipsForeignSaveIco *ico = (VipsForeignSaveIco *) object;
  48. VipsImage *in;
  49. if (VIPS_OBJECT_CLASS(vips_foreign_save_ico_parent_class)->build(object))
  50. return -1;
  51. in = save->ready; // shortcut
  52. if ((in->Xsize > 256) || (in->Ysize > 256)) {
  53. vips_error("vips_foreign_save_ico_build", "Image is too big. Max dimension size for ICO is 256");
  54. return -1;
  55. }
  56. // bands (3 or 4) * 8 bits
  57. int bands = vips_image_get_bands(in);
  58. if (bands > 4) {
  59. vips_error("vips_foreign_save_ico_build", "ICO source file must have 3 or 4 bands (RGB or RGBA)");
  60. return -1;
  61. }
  62. uint8_t width = in->Xsize % 256; // 0 means 256
  63. uint8_t height = in->Ysize % 256; // 0 means 256
  64. uint16_t bpp = 24;
  65. if (bands > 3) {
  66. bpp = 32;
  67. }
  68. uint32_t data_offset = 22; // ICO header size + ICONDIRENTRY size
  69. IcoHeader header;
  70. memcpy(header.dir, icon_dir, sizeof(icon_dir));
  71. header.width = width; // Width of the icon in pixels (0 for 256)
  72. header.height = height; // Height of the icon in pixels (0 for 256)
  73. header.number_of_colors = 0; // Number of colors, not used in our case
  74. header.reserved = 0; // Reserved, always 0
  75. header.color_planes = 1; // Color planes, always 1
  76. header.bpp = GUINT16_TO_LE(bpp); // Bits per pixel
  77. header.data_offset = GUINT32_TO_LE(data_offset); // Image data offset, always 22
  78. void *buffer;
  79. size_t data_size;
  80. // Save PNG image to a buffer
  81. if (vips_pngsave_buffer(in, &buffer, &data_size, NULL)) {
  82. vips_error("vips_foreign_save_ico_build", "unable to save ICO image as PNG");
  83. return -1;
  84. }
  85. header.data_size = GUINT32_TO_LE(data_size); // Image data size
  86. // Write header
  87. if (vips_target_write(ico->target, &header, sizeof(header))) {
  88. g_free(buffer);
  89. vips_error("vips_foreign_save_ico_build", "unable to write ICO header to target");
  90. return -1;
  91. }
  92. // Write data
  93. if (vips_target_write(ico->target, buffer, data_size)) {
  94. g_free(buffer);
  95. vips_error("vips_foreign_save_ico_build", "unable to write ICO header to target");
  96. return -1;
  97. }
  98. g_free(buffer);
  99. if (vips_target_end(ico->target))
  100. return -1;
  101. return 0;
  102. }
  103. static void
  104. vips_foreign_save_ico_class_init(VipsForeignSaveIcoClass *class)
  105. {
  106. GObjectClass *gobject_class = G_OBJECT_CLASS(class);
  107. VipsObjectClass *object_class = (VipsObjectClass *) class;
  108. VipsForeignClass *foreign_class = (VipsForeignClass *) class;
  109. VipsForeignSaveClass *save_class = (VipsForeignSaveClass *) class;
  110. gobject_class->dispose = vips_foreign_save_ico_dispose;
  111. gobject_class->set_property = vips_object_set_property;
  112. gobject_class->get_property = vips_object_get_property;
  113. object_class->nickname = "icosave_base";
  114. object_class->description = "save ico";
  115. object_class->build = vips_foreign_save_ico_build;
  116. // We do not support saving monochrome images yet (VIPS_FOREIGN_SAVEABLE_MONO)
  117. // In v4 we will support it, so we leave it here commented out
  118. save_class->saveable =
  119. VIPS_SAVEABLE_RGB | // latest vips: VIPS_FOREIGN_SAVEABLE_RGB
  120. VIPS_SAVEABLE_RGBA; // latest vips: VIPS_FOREIGN_SAVEABLE_ALPHA
  121. save_class->format_table = bandfmt_ico;
  122. }
  123. static void
  124. vips_foreign_save_ico_init(VipsForeignSaveIco *ico)
  125. {
  126. }
  127. typedef struct _VipsForeignSaveIcoTarget {
  128. VipsForeignSaveIco parent_object;
  129. VipsTarget *target;
  130. } VipsForeignSaveIcoTarget;
  131. typedef VipsForeignSaveIcoClass VipsForeignSaveIcoTargetClass;
  132. G_DEFINE_TYPE(VipsForeignSaveIcoTarget, vips_foreign_save_ico_target,
  133. vips_foreign_save_ico_get_type());
  134. static int
  135. vips_foreign_save_ico_target_build(VipsObject *object)
  136. {
  137. VipsForeignSaveIco *ico = (VipsForeignSaveIco *) object;
  138. VipsForeignSaveIcoTarget *target = (VipsForeignSaveIcoTarget *) object;
  139. ico->target = target->target;
  140. g_object_ref(ico->target);
  141. return VIPS_OBJECT_CLASS(vips_foreign_save_ico_target_parent_class)
  142. ->build(object);
  143. }
  144. static void
  145. vips_foreign_save_ico_target_class_init(VipsForeignSaveIcoTargetClass *class)
  146. {
  147. GObjectClass *gobject_class = G_OBJECT_CLASS(class);
  148. VipsObjectClass *object_class = (VipsObjectClass *) class;
  149. gobject_class->set_property = vips_object_set_property;
  150. gobject_class->get_property = vips_object_get_property;
  151. object_class->nickname = "icosave_target";
  152. object_class->description = "save image to target as PNG";
  153. object_class->build = vips_foreign_save_ico_target_build;
  154. VIPS_ARG_OBJECT(class, "target", 1,
  155. "Target",
  156. "Target to save to",
  157. VIPS_ARGUMENT_REQUIRED_INPUT,
  158. G_STRUCT_OFFSET(VipsForeignSaveIcoTarget, target),
  159. VIPS_TYPE_TARGET);
  160. }
  161. static void
  162. vips_foreign_save_ico_target_init(VipsForeignSaveIcoTarget *target)
  163. {
  164. }
  165. /**
  166. * vips_icosave_target: (method)
  167. * @in: image to save
  168. * @target: save image to this target
  169. * @...: `NULL`-terminated list of optional named arguments
  170. *
  171. * As [method@Image.icosave], but save to a target.
  172. *
  173. * ::: seealso
  174. * [method@Image.icosave], [method@Image.write_to_target].
  175. *
  176. * Returns: 0 on success, -1 on error.
  177. */
  178. int
  179. vips_icosave_target(VipsImage *in, VipsTarget *target, ...)
  180. {
  181. va_list ap;
  182. int result;
  183. va_start(ap, target);
  184. result = vips_call_split("icosave_target", ap, in, target);
  185. va_end(ap);
  186. return result;
  187. }
  188. // wrapper function which hides varargs (...) from CGo
  189. int
  190. vips_icosave_target_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts)
  191. {
  192. return vips_icosave_target(in, VIPS_TARGET(target), NULL);
  193. }