OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 34 matching lines...) Loading... |
45 #include "base/cross/bits.h" | 45 #include "base/cross/bits.h" |
46 #include "core/cross/types.h" | 46 #include "core/cross/types.h" |
47 #include "core/cross/texture.h" | 47 #include "core/cross/texture.h" |
48 | 48 |
49 class FilePath; | 49 class FilePath; |
50 | 50 |
51 namespace o3d { | 51 namespace o3d { |
52 | 52 |
53 class MemoryReadStream; | 53 class MemoryReadStream; |
54 class RawData; | 54 class RawData; |
55 | 55 class Pack; |
56 class Bitmap { | 56 |
| 57 // Bitmap provides an API for basic image operations on bitmap images, |
| 58 // including scale and crop. The contents of bitmap can be created from |
| 59 // a RawData object via LoadFromRawData(), and also can be transfered |
| 60 // to mip of a Texure2D or a specific face of TextureCUBE via methods |
| 61 // in Texture. |
| 62 |
| 63 class Bitmap : public ParamObject { |
57 public: | 64 public: |
| 65 typedef SmartPointer<Bitmap> Ref; |
| 66 |
| 67 explicit Bitmap(ServiceLocator* service_locator); |
| 68 virtual ~Bitmap() {} |
58 | 69 |
59 // We will fail to load images that are bigger than 4kx4k to avoid security | 70 // We will fail to load images that are bigger than 4kx4k to avoid security |
60 // risks. GPUs don't usually support bigger sizes anyway. | 71 // risks. GPUs don't usually support bigger sizes anyway. |
61 // The biggest bitmap buffer size with these dimensions is: | 72 // The biggest bitmap buffer size with these dimensions is: |
62 // 4k x 4k x 4xsizeof(float) x6 x4/3 (x6 for cube maps, x4/3 for mipmaps) | 73 // 4k x 4k x 4xsizeof(float) x6 x4/3 (x6 for cube maps, x4/3 for mipmaps) |
63 // That makes 2GB, representable in an unsigned int, so we will avoid wraps. | 74 // That makes 2GB, representable in an unsigned int, so we will avoid wraps. |
64 static const unsigned int kMaxImageDimension = 4096; | 75 static const unsigned int kMaxImageDimension = 4096; |
65 enum ImageFileType { | 76 enum ImageFileType { |
66 UNKNOWN, | 77 UNKNOWN, |
67 TGA, | 78 TGA, |
68 JPEG, | 79 JPEG, |
69 PNG, | 80 PNG, |
70 DDS, | 81 DDS, |
71 }; | 82 }; |
72 | 83 |
73 Bitmap() | |
74 : image_data_(NULL), | |
75 format_(Texture::UNKNOWN_FORMAT), | |
76 width_(0), | |
77 height_(0), | |
78 num_mipmaps_(0), | |
79 is_cubemap_(false) {} | |
80 ~Bitmap() {} | |
81 | |
82 static bool CheckImageDimensions(unsigned int width, unsigned int height) { | 84 static bool CheckImageDimensions(unsigned int width, unsigned int height) { |
83 return width > 0 && height > 0 && | 85 return width > 0 && height > 0 && |
84 width <= kMaxImageDimension && height < kMaxImageDimension; | 86 width <= kMaxImageDimension && height < kMaxImageDimension; |
85 } | 87 } |
86 | 88 |
87 // Creates a copy of a bitmap, copying the pixels as well. | 89 // Creates a copy of a bitmap, copying the pixels as well. |
88 // Parameters: | 90 // Parameters: |
89 // source: the source bitmap. | 91 // source: the source bitmap. |
90 void CopyDeepFrom(const Bitmap &source) { | 92 void CopyDeepFrom(const Bitmap &source) { |
91 Allocate(source.format_, source.width_, source.height_, | 93 Allocate(source.format_, source.width_, source.height_, |
(...skipping 135 matching lines...) Loading... |
227 // with no mip-maps (only the base level). | 229 // with no mip-maps (only the base level). |
228 // Parameters: | 230 // Parameters: |
229 // filename: the name of the file to into. | 231 // filename: the name of the file to into. |
230 // Returns: | 232 // Returns: |
231 // true if successful. | 233 // true if successful. |
232 bool SaveToPNGFile(const char* filename); | 234 bool SaveToPNGFile(const char* filename); |
233 | 235 |
234 // Checks that the alpha channel for the entire bitmap is 1.0 | 236 // Checks that the alpha channel for the entire bitmap is 1.0 |
235 bool CheckAlphaIsOne() const; | 237 bool CheckAlphaIsOne() const; |
236 | 238 |
| 239 // Copy pixels from source bitmap. Scales if the width and height of source |
| 240 // and dest do not match. |
| 241 // Parameters: |
| 242 // source_img: source bitmap which would be drawn. |
| 243 // source_x: x-coordinate of the starting pixel in the source image. |
| 244 // source_x: y-coordinate of the starting pixel in the source image. |
| 245 // source_width: width of the source image to draw. |
| 246 // source_height: Height of the source image to draw. |
| 247 // dest_x: x-coordinate of the starting pixel in the dest image. |
| 248 // dest_y: y-coordinate of the starting pixel in the dest image. |
| 249 // dest_width: width of the dest image to draw. |
| 250 // dest_height: height of the dest image to draw. |
| 251 void DrawImage(Bitmap* source_img, int source_x, int source_y, |
| 252 int source_width, int source_height, |
| 253 int dest_x, int dest_y, |
| 254 int dest_width, int dest_height); |
| 255 |
| 256 // Crop part of an image from src, scale it to an arbitrary size |
| 257 // and paste in dest image. Utility function for all DrawImage |
| 258 // function in bitmap and textures. Scale operation is based on |
| 259 // bilinear interpolation. |
| 260 // Note: this doesn't work for DXTC, or floating-point images. |
| 261 // |
| 262 // Parameters: |
| 263 // src: source image which would be copied from. |
| 264 // src_x: x-coordinate of the starting pixel in the src image. |
| 265 // src_y: y-coordinate of the starting pixel in the src image. |
| 266 // src_width: width of the part in src image to be croped. |
| 267 // src_height: height of the part in src image to be croped. |
| 268 // src_img_width: width of the src image. |
| 269 // src_img_height: height of the src image. |
| 270 // dest: dest image which would be copied to. |
| 271 // dest_x: x-coordinate of the starting pixel in the dest image. |
| 272 // dest_y: y-coordinate of the starting pixel in the dest image. |
| 273 // dest_width: width of the part in dest image to be pasted to. |
| 274 // dest_height: height of the part in dest image to be pasted to. |
| 275 // dest_img_width: width of the dest image. |
| 276 // dest_img_height: height of the src image. |
| 277 // component: size of each pixel in terms of array element. |
| 278 // Returns: |
| 279 // true if crop and scale succeeds. |
| 280 static void BilinearInterpolateScale(const uint8* src, |
| 281 int src_x, int src_y, |
| 282 int src_width, int src_height, |
| 283 int src_img_width, int src_img_height, |
| 284 uint8* dest, |
| 285 int dest_x, int dest_y, |
| 286 int dest_width, int dest_height, |
| 287 int dest_img_width, int dest_img_height, |
| 288 int component); |
| 289 |
237 // Detects the type of image file based on the filename. | 290 // Detects the type of image file based on the filename. |
238 static ImageFileType GetFileTypeFromFilename(const char *filename); | 291 static ImageFileType GetFileTypeFromFilename(const char *filename); |
239 // Detects the type of image file based on the mime-type. | 292 // Detects the type of image file based on the mime-type. |
240 static ImageFileType GetFileTypeFromMimeType(const char *mime_type); | 293 static ImageFileType GetFileTypeFromMimeType(const char *mime_type); |
241 | 294 |
242 // Adds filler alpha byte (0xff) after every pixel. Assumes buffer was | 295 // Adds filler alpha byte (0xff) after every pixel. Assumes buffer was |
243 // allocated with enough storage) | 296 // allocated with enough storage) |
244 // can convert RGB -> RGBA, BGR -> BGRA, etc. | 297 // can convert RGB -> RGBA, BGR -> BGRA, etc. |
245 static void XYZToXYZA(unsigned char *image_data, int pixel_count); | 298 static void XYZToXYZA(unsigned char *image_data, int pixel_count); |
246 | 299 |
(...skipping 66 matching lines...) Loading... |
313 // dst: a buffer with enough space for the target version. Pixels are | 366 // dst: a buffer with enough space for the target version. Pixels are |
314 // written from the end to the beginning so dst can be the same buffer as | 367 // written from the end to the beginning so dst can be the same buffer as |
315 // src if the transformation is an upscaling. | 368 // src if the transformation is an upscaling. |
316 static bool Scale(unsigned int src_width, | 369 static bool Scale(unsigned int src_width, |
317 unsigned int src_height, | 370 unsigned int src_height, |
318 Texture::Format format, | 371 Texture::Format format, |
319 const unsigned char *src, | 372 const unsigned char *src, |
320 unsigned int dst_width, | 373 unsigned int dst_width, |
321 unsigned int dst_height, | 374 unsigned int dst_height, |
322 unsigned char *dst); | 375 unsigned char *dst); |
| 376 |
| 377 // adjust start points and boundaries when using DrawImage data |
| 378 // in bitmap and textures. |
| 379 // Parameters: |
| 380 // src_x: x-coordinate of the starting pixel in the source image. |
| 381 // src_y: y-coordinate of the starting pixel in the source image. |
| 382 // src_width: width of the source image to draw. |
| 383 // src_height: height of the source image to draw. |
| 384 // src_bmp_width: original width of source bitmap. |
| 385 // src_bmp_height: original height of source bitmap. |
| 386 // dest_x: x-coordinate of the starting pixel in the dest image. |
| 387 // dest_y: y-coordinate of the starting pixel in the dest image. |
| 388 // dest_width: width of the dest image to draw. |
| 389 // dest_height: height of the dest image to draw. |
| 390 // dest_bmp_width: original width of dest bitmap. |
| 391 // dest_bmp_height: original height of dest bitmap. |
| 392 // Returns: |
| 393 // false if src or dest rectangle is out of boundaries. |
| 394 static bool AdjustDrawImageBoundary(int* src_x, int* src_y, |
| 395 int* src_width, int* src_height, |
| 396 int src_bmp_width, int src_bmp_height, |
| 397 int* dest_x, int* dest_y, |
| 398 int* dest_width, int* dest_height, |
| 399 int dest_bmp_width, int dest_bmp_height); |
| 400 |
323 private: | 401 private: |
| 402 friend class IClassManager; |
| 403 static ObjectBase::Ref Create(ServiceLocator* service_locator); |
| 404 |
324 // pointer to the raw bitmap data | 405 // pointer to the raw bitmap data |
325 scoped_array<uint8> image_data_; | 406 scoped_array<uint8> image_data_; |
326 // format of the texture this is meant to represent. | 407 // format of the texture this is meant to represent. |
327 Texture::Format format_; | 408 Texture::Format format_; |
328 // width of the bitmap in pixels. | 409 // width of the bitmap in pixels. |
329 unsigned int width_; | 410 unsigned int width_; |
330 // height of the bitmap in pixels. | 411 // height of the bitmap in pixels. |
331 unsigned int height_; | 412 unsigned int height_; |
332 // number of mipmap levels in this texture. | 413 // number of mipmap levels in this texture. |
333 unsigned int num_mipmaps_; | 414 unsigned int num_mipmaps_; |
334 // is this cube-map data | 415 // is this cube-map data |
335 bool is_cubemap_; | 416 bool is_cubemap_; |
336 | 417 |
| 418 // utility function used in AdjustDrawImageBoundary. |
| 419 // It adjusts start point and related measures |
| 420 // for a specific dimension. |
| 421 // Parameter: |
| 422 // src_a: the coordinate which is negative. |
| 423 // dest_a: same coordinate in the other image. |
| 424 // src_length: length measure of source image to draw. |
| 425 // dest_length: length measure of dest image to draw. |
| 426 // src_bmp_length: length measure of src image. |
| 427 // Returns: |
| 428 // true if adjust is successful. |
| 429 static bool AdjustDrawImageBoundHelper(int* src_a, int* dest_a, |
| 430 int* src_length, int* dest_length, |
| 431 int src_bmp_length); |
| 432 |
| 433 O3D_DECL_CLASS(Bitmap, ParamObject); |
337 DISALLOW_COPY_AND_ASSIGN(Bitmap); | 434 DISALLOW_COPY_AND_ASSIGN(Bitmap); |
338 }; | 435 }; |
339 | 436 |
340 } // namespace o3d | 437 } // namespace o3d |
341 | 438 |
342 #endif // O3D_CORE_CROSS_BITMAP_H_ | 439 #endif // O3D_CORE_CROSS_BITMAP_H_ |
OLD | NEW |