DiscardMissingTileImagePolicy.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import defaultValue from '../Core/defaultValue.js';
  2. import defined from '../Core/defined.js';
  3. import DeveloperError from '../Core/DeveloperError.js';
  4. import getImagePixels from '../Core/getImagePixels.js';
  5. import Resource from '../Core/Resource.js';
  6. /**
  7. * A policy for discarding tile images that match a known image containing a
  8. * "missing" image.
  9. *
  10. * @alias DiscardMissingTileImagePolicy
  11. * @constructor
  12. *
  13. * @param {Object} options Object with the following properties:
  14. * @param {Resource|String} options.missingImageUrl The URL of the known missing image.
  15. * @param {Cartesian2[]} options.pixelsToCheck An array of {@link Cartesian2} pixel positions to
  16. * compare against the missing image.
  17. * @param {Boolean} [options.disableCheckIfAllPixelsAreTransparent=false] If true, the discard check will be disabled
  18. * if all of the pixelsToCheck in the missingImageUrl have an alpha value of 0. If false, the
  19. * discard check will proceed no matter the values of the pixelsToCheck.
  20. */
  21. function DiscardMissingTileImagePolicy(options) {
  22. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  23. //>>includeStart('debug', pragmas.debug);
  24. if (!defined(options.missingImageUrl)) {
  25. throw new DeveloperError('options.missingImageUrl is required.');
  26. }
  27. if (!defined(options.pixelsToCheck)) {
  28. throw new DeveloperError('options.pixelsToCheck is required.');
  29. }
  30. //>>includeEnd('debug');
  31. this._pixelsToCheck = options.pixelsToCheck;
  32. this._missingImagePixels = undefined;
  33. this._missingImageByteLength = undefined;
  34. this._isReady = false;
  35. var resource = Resource.createIfNeeded(options.missingImageUrl);
  36. var that = this;
  37. function success(image) {
  38. if (defined(image.blob)) {
  39. that._missingImageByteLength = image.blob.size;
  40. }
  41. var pixels = getImagePixels(image);
  42. if (options.disableCheckIfAllPixelsAreTransparent) {
  43. var allAreTransparent = true;
  44. var width = image.width;
  45. var pixelsToCheck = options.pixelsToCheck;
  46. for (var i = 0, len = pixelsToCheck.length; allAreTransparent && i < len; ++i) {
  47. var pos = pixelsToCheck[i];
  48. var index = pos.x * 4 + pos.y * width;
  49. var alpha = pixels[index + 3];
  50. if (alpha > 0) {
  51. allAreTransparent = false;
  52. }
  53. }
  54. if (allAreTransparent) {
  55. pixels = undefined;
  56. }
  57. }
  58. that._missingImagePixels = pixels;
  59. that._isReady = true;
  60. }
  61. function failure() {
  62. // Failed to download "missing" image, so assume that any truly missing tiles
  63. // will also fail to download and disable the discard check.
  64. that._missingImagePixels = undefined;
  65. that._isReady = true;
  66. }
  67. resource.fetchImage({
  68. preferBlob : true,
  69. preferImageBitmap : true,
  70. flipY : true
  71. }).then(success).otherwise(failure);
  72. }
  73. /**
  74. * Determines if the discard policy is ready to process images.
  75. * @returns {Boolean} True if the discard policy is ready to process images; otherwise, false.
  76. */
  77. DiscardMissingTileImagePolicy.prototype.isReady = function() {
  78. return this._isReady;
  79. };
  80. /**
  81. * Given a tile image, decide whether to discard that image.
  82. *
  83. * @param {Image} image An image to test.
  84. * @returns {Boolean} True if the image should be discarded; otherwise, false.
  85. *
  86. * @exception {DeveloperError} <code>shouldDiscardImage</code> must not be called before the discard policy is ready.
  87. */
  88. DiscardMissingTileImagePolicy.prototype.shouldDiscardImage = function(image) {
  89. //>>includeStart('debug', pragmas.debug);
  90. if (!this._isReady) {
  91. throw new DeveloperError('shouldDiscardImage must not be called before the discard policy is ready.');
  92. }
  93. //>>includeEnd('debug');
  94. var pixelsToCheck = this._pixelsToCheck;
  95. var missingImagePixels = this._missingImagePixels;
  96. // If missingImagePixels is undefined, it indicates that the discard check has been disabled.
  97. if (!defined(missingImagePixels)) {
  98. return false;
  99. }
  100. if (defined(image.blob) && image.blob.size !== this._missingImageByteLength) {
  101. return false;
  102. }
  103. var pixels = getImagePixels(image);
  104. var width = image.width;
  105. for (var i = 0, len = pixelsToCheck.length; i < len; ++i) {
  106. var pos = pixelsToCheck[i];
  107. var index = pos.x * 4 + pos.y * width;
  108. for (var offset = 0; offset < 4; ++offset) {
  109. var pixel = index + offset;
  110. if (pixels[pixel] !== missingImagePixels[pixel]) {
  111. return false;
  112. }
  113. }
  114. }
  115. return true;
  116. };
  117. export default DiscardMissingTileImagePolicy;