ImageUtils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { createElementNS } from '../utils.js';
  2. import { SRGBToLinear } from '../math/ColorManagement.js';
  3. let _canvas;
  4. /**
  5. * A class containing utility functions for images.
  6. *
  7. * @hideconstructor
  8. */
  9. class ImageUtils {
  10. /**
  11. * Returns a data URI containing a representation of the given image.
  12. *
  13. * @param {(HTMLImageElement|HTMLCanvasElement)} image - The image object.
  14. * @param {string} [type='image/png'] - Indicates the image format.
  15. * @return {string} The data URI.
  16. */
  17. static getDataURL( image, type = 'image/png' ) {
  18. if ( /^data:/i.test( image.src ) ) {
  19. return image.src;
  20. }
  21. if ( typeof HTMLCanvasElement === 'undefined' ) {
  22. return image.src;
  23. }
  24. let canvas;
  25. if ( image instanceof HTMLCanvasElement ) {
  26. canvas = image;
  27. } else {
  28. if ( _canvas === undefined ) _canvas = createElementNS( 'canvas' );
  29. _canvas.width = image.width;
  30. _canvas.height = image.height;
  31. const context = _canvas.getContext( '2d' );
  32. if ( image instanceof ImageData ) {
  33. context.putImageData( image, 0, 0 );
  34. } else {
  35. context.drawImage( image, 0, 0, image.width, image.height );
  36. }
  37. canvas = _canvas;
  38. }
  39. return canvas.toDataURL( type );
  40. }
  41. /**
  42. * Converts the given sRGB image data to linear color space.
  43. *
  44. * @param {(HTMLImageElement|HTMLCanvasElement|ImageBitmap|Object)} image - The image object.
  45. * @return {HTMLCanvasElement|Object} The converted image.
  46. */
  47. static sRGBToLinear( image ) {
  48. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  49. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
  50. ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
  51. const canvas = createElementNS( 'canvas' );
  52. canvas.width = image.width;
  53. canvas.height = image.height;
  54. const context = canvas.getContext( '2d' );
  55. context.drawImage( image, 0, 0, image.width, image.height );
  56. const imageData = context.getImageData( 0, 0, image.width, image.height );
  57. const data = imageData.data;
  58. for ( let i = 0; i < data.length; i ++ ) {
  59. data[ i ] = SRGBToLinear( data[ i ] / 255 ) * 255;
  60. }
  61. context.putImageData( imageData, 0, 0 );
  62. return canvas;
  63. } else if ( image.data ) {
  64. const data = image.data.slice( 0 );
  65. for ( let i = 0; i < data.length; i ++ ) {
  66. if ( data instanceof Uint8Array || data instanceof Uint8ClampedArray ) {
  67. data[ i ] = Math.floor( SRGBToLinear( data[ i ] / 255 ) * 255 );
  68. } else {
  69. // assuming float
  70. data[ i ] = SRGBToLinear( data[ i ] );
  71. }
  72. }
  73. return {
  74. data: data,
  75. width: image.width,
  76. height: image.height
  77. };
  78. } else {
  79. console.warn( 'THREE.ImageUtils.sRGBToLinear(): Unsupported image type. No color space conversion applied.' );
  80. return image;
  81. }
  82. }
  83. }
  84. export { ImageUtils };
粤ICP备19079148号