|
| 1 | +package org.schabi.newpipe.extractor; |
| 2 | + |
| 3 | +import javax.annotation.Nonnull; |
| 4 | +import java.io.Serializable; |
| 5 | +import java.util.Objects; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class representing images in the extractor. |
| 9 | + * |
| 10 | + * <p> |
| 11 | + * An image has four properties: its URL, its height, its width and its estimated quality level. |
| 12 | + * </p> |
| 13 | + * |
| 14 | + * <p> |
| 15 | + * Depending of the services, the height, the width or both properties may be not known. |
| 16 | + * Implementations <b>must use</b> the relevant unknown constants in this case |
| 17 | + * ({@link #HEIGHT_UNKNOWN} and {@link #WIDTH_UNKNOWN}), to ensure properly the lack of knowledge |
| 18 | + * of one or both of these properties to extractor clients. |
| 19 | + * </p> |
| 20 | + * |
| 21 | + * <p> |
| 22 | + * They should also respect the ranges defined in the estimated image resolution levels as much as |
| 23 | + * possible, to ensure consistency to extractor clients. |
| 24 | + * </p> |
| 25 | + */ |
| 26 | +public final class Image implements Serializable { |
| 27 | + |
| 28 | + /** |
| 29 | + * Constant representing that the height of an {@link Image} is unknown. |
| 30 | + */ |
| 31 | + public static final int HEIGHT_UNKNOWN = -1; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constant representing that the width of an {@link Image} is unknown. |
| 35 | + */ |
| 36 | + public static final int WIDTH_UNKNOWN = -1; |
| 37 | + |
| 38 | + @Nonnull |
| 39 | + private final String url; |
| 40 | + private final int height; |
| 41 | + private final int width; |
| 42 | + @Nonnull |
| 43 | + private final ResolutionLevel estimatedResolutionLevel; |
| 44 | + |
| 45 | + /** |
| 46 | + * Construct an {@link Image} instance. |
| 47 | + * |
| 48 | + * @param url the URL to the image, which should be not null or empty |
| 49 | + * @param height the image's height |
| 50 | + * @param width the image's width |
| 51 | + * @param estimatedResolutionLevel the image's estimated resolution level, which must not be |
| 52 | + * null |
| 53 | + * @throws NullPointerException if {@code estimatedResolutionLevel} is null |
| 54 | + */ |
| 55 | + public Image(@Nonnull final String url, |
| 56 | + final int height, |
| 57 | + final int width, |
| 58 | + @Nonnull final ResolutionLevel estimatedResolutionLevel) |
| 59 | + throws NullPointerException { |
| 60 | + this.url = url; |
| 61 | + this.height = height; |
| 62 | + this.width = width; |
| 63 | + this.estimatedResolutionLevel = Objects.requireNonNull( |
| 64 | + estimatedResolutionLevel, "estimatedResolutionLevel is null"); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the URL of this {@link Image}. |
| 69 | + * |
| 70 | + * @return the {@link Image}'s URL. |
| 71 | + */ |
| 72 | + @Nonnull |
| 73 | + public String getUrl() { |
| 74 | + return url; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the height of this {@link Image}. |
| 79 | + * |
| 80 | + * <p> |
| 81 | + * If it is unknown, {@link #HEIGHT_UNKNOWN} is returned instead. |
| 82 | + * </p> |
| 83 | + * |
| 84 | + * @return the {@link Image}'s height or {@link #HEIGHT_UNKNOWN} |
| 85 | + */ |
| 86 | + public int getHeight() { |
| 87 | + return height; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the width of this {@link Image}. |
| 92 | + * |
| 93 | + * <p> |
| 94 | + * If it is unknown, {@link #WIDTH_UNKNOWN} is returned instead. |
| 95 | + * </p> |
| 96 | + * |
| 97 | + * @return the {@link Image}'s width or {@link #WIDTH_UNKNOWN} |
| 98 | + */ |
| 99 | + public int getWidth() { |
| 100 | + return width; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get the estimated resolution level of this image. |
| 105 | + * |
| 106 | + * <p> |
| 107 | + * If it is unknown, {@link ResolutionLevel#UNKNOWN} is returned instead. |
| 108 | + * </p> |
| 109 | + * |
| 110 | + * @return the estimated resolution level, which is never {@code null} |
| 111 | + * @see ResolutionLevel |
| 112 | + */ |
| 113 | + @Nonnull |
| 114 | + public ResolutionLevel getEstimatedResolutionLevel() { |
| 115 | + return estimatedResolutionLevel; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get a string representation of this {@link Image} instance. |
| 120 | + * |
| 121 | + * <p> |
| 122 | + * The representation will be in the following format, where {@code url}, {@code height}, |
| 123 | + * {@code width} and {@code estimatedResolutionLevel} represent the corresponding properties: |
| 124 | + * <br> |
| 125 | + * <br> |
| 126 | + * {@code Image {url=url, height='height, width=width, |
| 127 | + * estimatedResolutionLevel=estimatedResolutionLevel}'} |
| 128 | + * </p> |
| 129 | + * |
| 130 | + * @return a string representation of this {@link Image} instance |
| 131 | + */ |
| 132 | + @Nonnull |
| 133 | + @Override |
| 134 | + public String toString() { |
| 135 | + return "Image {" + "url=" + url + ", height=" + height + ", width=" + width |
| 136 | + + ", estimatedResolutionLevel=" + estimatedResolutionLevel + "}"; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * The estimated resolution level of an {@link Image}. |
| 141 | + * |
| 142 | + * <p> |
| 143 | + * Some services don't return the size of their images, but we may know for a specific image |
| 144 | + * type that a service returns, according to real data, an approximation of the resolution |
| 145 | + * level. |
| 146 | + * </p> |
| 147 | + */ |
| 148 | + public enum ResolutionLevel { |
| 149 | + |
| 150 | + /** |
| 151 | + * The high resolution level. |
| 152 | + * |
| 153 | + * <p> |
| 154 | + * This level applies to images with a height greater than or equal to 720px. |
| 155 | + * </p> |
| 156 | + */ |
| 157 | + HIGH, |
| 158 | + |
| 159 | + /** |
| 160 | + * The medium resolution level. |
| 161 | + * |
| 162 | + * <p> |
| 163 | + * This level applies to images with a height between 175px inclusive and 720px exclusive. |
| 164 | + * </p> |
| 165 | + */ |
| 166 | + MEDIUM, |
| 167 | + |
| 168 | + /** |
| 169 | + * The low resolution level. |
| 170 | + * |
| 171 | + * <p> |
| 172 | + * This level applies to images with a height between 1px inclusive and 175px exclusive. |
| 173 | + * </p> |
| 174 | + */ |
| 175 | + LOW, |
| 176 | + |
| 177 | + /** |
| 178 | + * The unknown resolution level. |
| 179 | + * |
| 180 | + * <p> |
| 181 | + * This value is returned when the extractor doesn't know what resolution level an image |
| 182 | + * could have, for example if the extractor loops in an array of images with different |
| 183 | + * resolution levels without knowing the height. |
| 184 | + * </p> |
| 185 | + */ |
| 186 | + UNKNOWN; |
| 187 | + |
| 188 | + /** |
| 189 | + * Get a {@link ResolutionLevel} based from the given height. |
| 190 | + * |
| 191 | + * @param heightPx the height from which returning the good {@link ResolutionLevel} |
| 192 | + * @return the {@link ResolutionLevel} corresponding to the height provided. See the |
| 193 | + * {@link ResolutionLevel} values for details about what value is returned. |
| 194 | + */ |
| 195 | + public static ResolutionLevel fromHeight(final int heightPx) { |
| 196 | + if (heightPx <= 0) { |
| 197 | + return UNKNOWN; |
| 198 | + } |
| 199 | + |
| 200 | + if (heightPx < 175) { |
| 201 | + return LOW; |
| 202 | + } |
| 203 | + |
| 204 | + if (heightPx < 720) { |
| 205 | + return MEDIUM; |
| 206 | + } |
| 207 | + |
| 208 | + return HIGH; |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments