OLD | NEW |
1 // Copyright 2009 Google Inc. | 1 // Copyright 2009 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 etc1_encode_block(block, mask, encoded); | 580 etc1_encode_block(block, mask, encoded); |
581 memcpy(pOut, encoded, sizeof(encoded)); | 581 memcpy(pOut, encoded, sizeof(encoded)); |
582 } else { | 582 } else { |
583 memset(pOut, 0xFF, sizeof(encoded)); | 583 memset(pOut, 0xFF, sizeof(encoded)); |
584 } | 584 } |
585 pOut += sizeof(encoded); | 585 pOut += sizeof(encoded); |
586 } | 586 } |
587 } | 587 } |
588 return true; | 588 return true; |
589 } | 589 } |
| 590 |
| 591 static const char kMagic[] = { 'P', 'K', 'M', ' ', '1', '0' }; |
| 592 |
| 593 static const etc1_uint32 ETC1_PKM_FORMAT_OFFSET = 6; |
| 594 static const etc1_uint32 ETC1_PKM_ENCODED_WIDTH_OFFSET = 8; |
| 595 static const etc1_uint32 ETC1_PKM_ENCODED_HEIGHT_OFFSET = 10; |
| 596 static const etc1_uint32 ETC1_PKM_WIDTH_OFFSET = 12; |
| 597 static const etc1_uint32 ETC1_PKM_HEIGHT_OFFSET = 14; |
| 598 |
| 599 static const etc1_uint32 ETC1_RGB_NO_MIPMAPS = 0; |
| 600 |
| 601 static void writeBEUint16(etc1_byte* pOut, etc1_uint32 data) { |
| 602 pOut[0] = (etc1_byte) (data >> 8); |
| 603 pOut[1] = (etc1_byte) data; |
| 604 } |
| 605 |
| 606 static etc1_uint32 readBEUint16(const etc1_byte* pIn) { |
| 607 return (pIn[0] << 8) | pIn[1]; |
| 608 } |
| 609 |
| 610 // Format a PKM header |
| 611 |
| 612 void etc1_pkm_format_header(etc1_byte* pHeader, etc1_uint32 width, etc1_uint32 h
eight) { |
| 613 memcpy(pHeader, kMagic, sizeof(kMagic)); |
| 614 etc1_uint32 encodedWidth = (width + 3) & ~3; |
| 615 etc1_uint32 encodedHeight = (height + 3) & ~3; |
| 616 writeBEUint16(pHeader + ETC1_PKM_FORMAT_OFFSET, ETC1_RGB_NO_MIPMAPS); |
| 617 writeBEUint16(pHeader + ETC1_PKM_ENCODED_WIDTH_OFFSET, encodedWidth); |
| 618 writeBEUint16(pHeader + ETC1_PKM_ENCODED_HEIGHT_OFFSET, encodedHeight); |
| 619 writeBEUint16(pHeader + ETC1_PKM_WIDTH_OFFSET, width); |
| 620 writeBEUint16(pHeader + ETC1_PKM_HEIGHT_OFFSET, height); |
| 621 } |
| 622 |
| 623 // Check if a PKM header is correctly formatted. |
| 624 |
| 625 bool etc1_pkm_is_valid(const etc1_byte* pHeader) { |
| 626 if (memcmp(pHeader, kMagic, sizeof(kMagic))) { |
| 627 return false; |
| 628 } |
| 629 etc1_uint32 format = readBEUint16(pHeader + ETC1_PKM_FORMAT_OFFSET); |
| 630 etc1_uint32 encodedWidth = readBEUint16(pHeader + ETC1_PKM_ENCODED_WIDTH_OFF
SET); |
| 631 etc1_uint32 encodedHeight = readBEUint16(pHeader + ETC1_PKM_ENCODED_HEIGHT_O
FFSET); |
| 632 etc1_uint32 width = readBEUint16(pHeader + ETC1_PKM_WIDTH_OFFSET); |
| 633 etc1_uint32 height = readBEUint16(pHeader + ETC1_PKM_HEIGHT_OFFSET); |
| 634 return format == ETC1_RGB_NO_MIPMAPS && |
| 635 encodedWidth >= width && encodedWidth - width < 4 && |
| 636 encodedHeight >= height && encodedHeight - height < 4; |
| 637 } |
| 638 |
| 639 // Read the image width from a PKM header |
| 640 |
| 641 etc1_uint32 etc1_pkm_get_width(const etc1_byte* pHeader) { |
| 642 return readBEUint16(pHeader + ETC1_PKM_WIDTH_OFFSET); |
| 643 } |
| 644 |
| 645 // Read the image height from a PKM header |
| 646 |
| 647 etc1_uint32 etc1_pkm_get_height(const etc1_byte* pHeader){ |
| 648 return readBEUint16(pHeader + ETC1_PKM_HEIGHT_OFFSET); |
| 649 } |
OLD | NEW |