common.odin 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package image
  2. /*
  3. Copyright 2021 Jeroen van Rijn <[email protected]>.
  4. Made available under Odin's BSD-2 license.
  5. List of contributors:
  6. Jeroen van Rijn: Initial implementation, optimization.
  7. Ginger Bill: Cosmetic changes.
  8. */
  9. import "core:bytes"
  10. import "core:mem"
  11. Image :: struct {
  12. width: int,
  13. height: int,
  14. channels: int,
  15. depth: int,
  16. pixels: bytes.Buffer,
  17. /*
  18. Some image loaders/writers can return/take an optional background color.
  19. For convenience, we return them as u16 so we don't need to switch on the type
  20. in our viewer, and can just test against nil.
  21. */
  22. background: Maybe([3]u16),
  23. metadata_ptr: rawptr,
  24. metadata_type: typeid,
  25. }
  26. /*
  27. IMPORTANT: `.do_not_expand_*` options currently skip handling of the `alpha_*` options,
  28. therefore Gray+Alpha will be returned as such even if you add `.alpha_drop_if_present`,
  29. and `.alpha_add_if_missing` and keyed transparency will likewise be ignored.
  30. The same goes for indexed images. This will be remedied in a near future update.
  31. */
  32. /*
  33. Image_Option:
  34. `.info`
  35. This option behaves as `.return_ihdr` and `.do_not_decompress_image` and can be used
  36. to gather an image's dimensions and color information.
  37. `.return_header`
  38. Fill out img.sidecar.header with the image's format-specific header struct.
  39. If we only care about the image specs, we can set `.return_header` +
  40. `.do_not_decompress_image`, or `.info`, which works as if both of these were set.
  41. `.return_metadata`
  42. Returns all chunks not needed to decode the data.
  43. It also returns the header as if `.return_header` was set.
  44. `.do_not_decompress_image`
  45. Skip decompressing IDAT chunk, defiltering and the rest.
  46. `.do_not_expand_grayscale`
  47. Do not turn grayscale (+ Alpha) images into RGB(A).
  48. Returns just the 1 or 2 channels present, although 1, 2 and 4 bit are still scaled to 8-bit.
  49. `.do_not_expand_indexed`
  50. Do not turn indexed (+ Alpha) images into RGB(A).
  51. Returns just the 1 or 2 (with `tRNS`) channels present.
  52. Make sure to use `return_metadata` to also return the palette chunk so you can recolor it yourself.
  53. `.do_not_expand_channels`
  54. Applies both `.do_not_expand_grayscale` and `.do_not_expand_indexed`.
  55. `.alpha_add_if_missing`
  56. If the image has no alpha channel, it'll add one set to max(type).
  57. Turns RGB into RGBA and Gray into Gray+Alpha
  58. `.alpha_drop_if_present`
  59. If the image has an alpha channel, drop it.
  60. You may want to use `.alpha_premultiply` in this case.
  61. NOTE: For PNG, this also skips handling of the tRNS chunk, if present,
  62. unless you select `alpha_premultiply`.
  63. In this case it'll premultiply the specified pixels in question only,
  64. as the others are implicitly fully opaque.
  65. `.alpha_premultiply`
  66. If the image has an alpha channel, returns image data as follows:
  67. RGB *= A, Gray = Gray *= A
  68. `.blend_background`
  69. If a bKGD chunk is present in a PNG, we normally just set `img.background`
  70. with its value and leave it up to the application to decide how to display the image,
  71. as per the PNG specification.
  72. With `.blend_background` selected, we blend the image against the background
  73. color. As this negates the use for an alpha channel, we'll drop it _unless_
  74. you also specify `.alpha_add_if_missing`.
  75. Options that don't apply to an image format will be ignored by their loader.
  76. */
  77. Option :: enum {
  78. info = 0,
  79. do_not_decompress_image,
  80. return_header,
  81. return_metadata,
  82. alpha_add_if_missing,
  83. alpha_drop_if_present,
  84. alpha_premultiply,
  85. blend_background,
  86. // Unimplemented
  87. do_not_expand_grayscale,
  88. do_not_expand_indexed,
  89. do_not_expand_channels,
  90. }
  91. Options :: distinct bit_set[Option];
  92. Error :: enum {
  93. Invalid_PNG_Signature,
  94. IHDR_Not_First_Chunk,
  95. IHDR_Corrupt,
  96. IDAT_Missing,
  97. IDAT_Must_Be_Contiguous,
  98. IDAT_Corrupt,
  99. PNG_Does_Not_Adhere_to_Spec,
  100. PLTE_Encountered_Unexpectedly,
  101. PLTE_Invalid_Length,
  102. TRNS_Encountered_Unexpectedly,
  103. BKGD_Invalid_Length,
  104. Invalid_Image_Dimensions,
  105. Unknown_Color_Type,
  106. Invalid_Color_Bit_Depth_Combo,
  107. Unknown_Filter_Method,
  108. Unknown_Interlace_Method,
  109. Requested_Channel_Not_Present,
  110. Post_Processing_Error,
  111. }
  112. /*
  113. Functions to help with image buffer calculations
  114. */
  115. compute_buffer_size :: proc(width, height, channels, depth: int, extra_row_bytes := int(0)) -> (size: int) {
  116. size = ((((channels * width * depth) + 7) >> 3) + extra_row_bytes) * height;
  117. return;
  118. }
  119. /*
  120. For when you have an RGB(A) image, but want a particular channel.
  121. */
  122. Channel :: enum u8 {
  123. R = 1,
  124. G = 2,
  125. B = 3,
  126. A = 4,
  127. }
  128. return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok: bool) {
  129. ok = false;
  130. t: bytes.Buffer;
  131. idx := int(channel);
  132. if img.channels == 2 && idx == 4 {
  133. // Alpha requested, which in a two channel image is index 2: G.
  134. idx = 2;
  135. }
  136. if idx > img.channels {
  137. return {}, false;
  138. }
  139. switch img.depth {
  140. case 8:
  141. buffer_size := compute_buffer_size(img.width, img.height, 1, 8);
  142. t = bytes.Buffer{};
  143. resize(&t.buf, buffer_size);
  144. i := bytes.buffer_to_bytes(&img.pixels);
  145. o := bytes.buffer_to_bytes(&t);
  146. for len(i) > 0 {
  147. o[0] = i[idx];
  148. i = i[img.channels:];
  149. o = o[1:];
  150. }
  151. case 16:
  152. buffer_size := compute_buffer_size(img.width, img.height, 2, 8);
  153. t = bytes.Buffer{};
  154. resize(&t.buf, buffer_size);
  155. i := mem.slice_data_cast([]u16, img.pixels.buf[:]);
  156. o := mem.slice_data_cast([]u16, t.buf[:]);
  157. for len(i) > 0 {
  158. o[0] = i[idx];
  159. i = i[img.channels:];
  160. o = o[1:];
  161. }
  162. case 1, 2, 4:
  163. // We shouldn't see this case, as the loader already turns these into 8-bit.
  164. return {}, false;
  165. }
  166. res = new(Image);
  167. res.width = img.width;
  168. res.height = img.height;
  169. res.channels = 1;
  170. res.depth = img.depth;
  171. res.pixels = t;
  172. res.background = img.background;
  173. res.metadata_ptr = img.metadata_ptr;
  174. res.metadata_type = img.metadata_type;
  175. return res, true;
  176. }