Skip to content

Commit

Permalink
Merge pull request #202 from HappySeaFox/feature/pnm
Browse files Browse the repository at this point in the history
Feature/pnm
  • Loading branch information
HappySeaFox authored Nov 16, 2023
2 parents 7007272 + cfa267f commit 32a87a9
Show file tree
Hide file tree
Showing 17 changed files with 4,766 additions and 15 deletions.
29 changes: 22 additions & 7 deletions FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@
</tr>
<tr>
<td>11</td>
<td><a href="https://wikipedia.org/wiki/Portable_anymap">PNM</a></td>
<td>
<b>Grayscale:</b> 1-bit, 8-bit, 16-bit.
<b>Indexed:</b> 1-bit, 8-bit.
<b>RGB:</b> 24-bit, 48-bit.
<br/><br/>
<b>Content:</b> Static.
</td>
<td>-</td>
<td>Unsupported</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>12</td>
<td><a href="https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format">PSD</a></td>
<td>
<b>Grayscale:</b> 8-bit, 16-bit.
Expand All @@ -265,7 +280,7 @@
<td>-</td>
</tr>
<tr>
<td>12</td>
<td>13</td>
<td><a href="http://qoiformat.org">QOI</a></td>
<td>
<b>RGB:</b> 24-bit.
Expand All @@ -284,7 +299,7 @@
<td>-</td>
</tr>
<tr>
<td>13</td>
<td>14</td>
<td><a href="https://wikipedia.org/wiki/Scalable_Vector_Graphics">SVG</a></td>
<td>
<b>Bit depth:</b> 32-bit.
Expand All @@ -303,7 +318,7 @@
<td>resvg or nanosvg</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
<td><a href="https://wikipedia.org/wiki/Truevision_TGA">TGA</a></td>
<td>
<b>Grayscale:</b> 8-bit.
Expand All @@ -319,7 +334,7 @@
<td>-</td>
</tr>
<tr>
<td>15</td>
<td>16</td>
<td><a href="https://wikipedia.org/wiki/TIFF">TIFF</a></td>
<td>
<b>Bit depth:</b> 1-bit, 2-bit, 4-bit, 8-bit, 16-bit, 24-bit, 32-bit, 48-bit, 64-bit.
Expand All @@ -340,7 +355,7 @@
<td>libtiff</td>
</tr>
<tr>
<td>16</td>
<td>17</td>
<td><a href="http://fileformats.archiveteam.org/wiki/Quake_2_Texture">WAL</a></td>
<td>
<b>Indexed:</b> 8-bit.
Expand All @@ -353,7 +368,7 @@
<td>-</td>
</tr>
<tr>
<td>17</td>
<td>18</td>
<td><a href="https://wikipedia.org/wiki/WebP">WEBP</a></td>
<td>
<b>Bit depth:</b> 24-bit, 32-bit.
Expand All @@ -366,7 +381,7 @@
<td>libwebp</td>
</tr>
<tr>
<td>18</td>
<td>19</td>
<td><a href="https://en.wikipedia.org/wiki/X_BitMap">XBM</a></td>
<td>
<b>Bit depth:</b> 1-bit.
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ images with the help of [ksquirrel-libs](FAQ.md#how-old-is-sail), the predecesso
| 8 | [JPEG XL](https://wikipedia.org/wiki/JPEG_XL) | R | libjxl |
| 9 | [PCX](https://wikipedia.org/wiki/PCX) | R | |
| 10 | [PNG](https://wikipedia.org/wiki/Portable_Network_Graphics) | RW | libpng |
| 11 | [PSD](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format) | R | |
| 12 | [QOI](http://qoiformat.org) | RW | |
| 13 | [SVG](https://wikipedia.org/wiki/Scalable_Vector_Graphics) | R | resvg |
| 14 | [TGA](https://wikipedia.org/wiki/Truevision_TGA) | R | |
| 15 | [TIFF](https://wikipedia.org/wiki/TIFF) | RW | libtiff |
| .. | ... | | |
| 17 | [WEBP](https://wikipedia.org/wiki/WebP) | R | libwebp |
| 12 | [PSD](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format) | R | |
| 13 | [QOI](http://qoiformat.org) | RW | |
| 14 | [SVG](https://wikipedia.org/wiki/Scalable_Vector_Graphics) | R | resvg |
| 15 | [TGA](https://wikipedia.org/wiki/Truevision_TGA) | R | |
| 16 | [TIFF](https://wikipedia.org/wiki/TIFF) | RW | libtiff |
| .. | ... | | |
| 18 | [WEBP](https://wikipedia.org/wiki/WebP) | R | libwebp |
| .. | ... | | |

See the full list [here](FORMATS.md). Work to add more image formats is ongoing.
Expand Down
2 changes: 1 addition & 1 deletion src/sail-codecs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_subdirectory(common/bmp)
set(HIGHEST_PRIORITY_CODECS gif jpeg png tiff)
set(HIGH_PRIORITY_CODECS bmp svg)
set(MEDIUM_PRIORITY_CODECS avif jpeg2000 jpegxl webp)
set(LOW_PRIORITY_CODECS ico pcx psd qoi tga)
set(LOW_PRIORITY_CODECS ico pcx pnm psd qoi tga)
set(LOWEST_PRIORITY_CODECS wal xbm)

set(CODECS ${HIGHEST_PRIORITY_CODECS}
Expand Down
3 changes: 3 additions & 0 deletions src/sail-codecs/pnm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Common codec configuration
#
sail_codec(NAME pnm SOURCES helpers.h helpers.c pnm.c ICON pnm.png)
169 changes: 169 additions & 0 deletions src/sail-codecs/pnm/helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/* This file is part of SAIL (https://github.com/HappySeaFox/sail)
Copyright (c) 2023 Dmitry Baryshev
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include <sail-common/sail-common.h>

#include "helpers.h"

sail_status_t pnm_private_skip_to_letters_numbers_force_read(struct sail_io *io, char *first_char) {

char c;

do {
SAIL_TRY(io->strict_read(io->stream, &c, 1));

if (c == '#') {
do {
SAIL_TRY(io->strict_read(io->stream, &c, 1));
} while(c != '\n');
}
} while (!isalnum(c));

*first_char = c;

return SAIL_OK;
}

sail_status_t pnm_private_skip_to_letters_numbers(struct sail_io *io, char starting_char, char *first_char) {

if (isalnum(starting_char)) {
*first_char = starting_char;
return SAIL_OK;
}

SAIL_TRY(pnm_private_skip_to_letters_numbers_force_read(io, first_char));

return SAIL_OK;
}

sail_status_t pnm_private_read_word(struct sail_io *io, char *str, size_t str_size) {

if (str_size < 2) {
SAIL_LOG_AND_RETURN(SAIL_ERROR_INVALID_ARGUMENT);
}

char first_char;
SAIL_TRY(pnm_private_skip_to_letters_numbers(io, SAIL_PNM_INVALID_STARTING_CHAR, &first_char));

unsigned i = 0;
char c = first_char;

bool eof;
SAIL_TRY(io->eof(io->stream, &eof));

sail_status_t saved_status = SAIL_OK;

while (isalnum(c) && i < str_size - 1 && !eof) {
*(str + i++) = c;
SAIL_TRY_OR_EXECUTE(io->strict_read(io->stream, &c, 1),
/* on error */ saved_status = __sail_status);
SAIL_TRY(io->eof(io->stream, &eof));

if (saved_status != SAIL_OK && !eof) {
return saved_status;
}
}

/* The buffer is full but no word delimiter found. */
if (i == str_size - 1 && !eof) {
SAIL_LOG_ERROR("PNM: No word delimiter found");
SAIL_LOG_AND_RETURN(SAIL_ERROR_BROKEN_IMAGE);
}

*(str + i) = '\0';

return SAIL_OK;
}

sail_status_t pnm_private_read_pixels(struct sail_io *io, struct sail_image *image, unsigned channels, unsigned bpc, double multiplier_to_full_range) {

for (unsigned row = 0; row < image->height; row++) {
uint8_t *scan8 = sail_scan_line(image, row);
uint16_t *scan16 = sail_scan_line(image, row);

for (unsigned column = 0; column < image->width; column++) {
for(unsigned channel = 0; channel < channels; channel++) {
char buffer[8];
SAIL_TRY(pnm_private_read_word(io, buffer, sizeof(buffer)));

unsigned value;
#ifdef _MSC_VER
if (sscanf_s(buffer, "%u", &value) != 1) {
#else
if (sscanf(buffer, "%u", &value) != 1) {
#endif
SAIL_LOG_ERROR("PNM: Failed to read color value");
SAIL_LOG_AND_RETURN(SAIL_ERROR_BROKEN_IMAGE);
}

if (SAIL_LIKELY(bpc == 8)) {
*scan8++ = (uint8_t)(value * multiplier_to_full_range);
} else {
*scan16++ = (uint16_t)(value * multiplier_to_full_range);
}
}
}
}

return SAIL_OK;
}

enum SailPixelFormat pnm_private_rgb_sail_pixel_format(enum SailPnmVersion pnm_version, unsigned bpc) {

switch (pnm_version) {
case SAIL_PNM_VERSION_P1:
case SAIL_PNM_VERSION_P4: return SAIL_PIXEL_FORMAT_BPP1_INDEXED;

case SAIL_PNM_VERSION_P2:
case SAIL_PNM_VERSION_P5: {
switch (bpc) {
case 8: return SAIL_PIXEL_FORMAT_BPP8_GRAYSCALE;
case 16: return SAIL_PIXEL_FORMAT_BPP16_GRAYSCALE;

default: return SAIL_PIXEL_FORMAT_UNKNOWN;
}
}

case SAIL_PNM_VERSION_P3:
case SAIL_PNM_VERSION_P6: {
switch (bpc) {
case 8: return SAIL_PIXEL_FORMAT_BPP24_RGB;
case 16: return SAIL_PIXEL_FORMAT_BPP48_RGB;

default: return SAIL_PIXEL_FORMAT_UNKNOWN;
}
}

default: {
return SAIL_PIXEL_FORMAT_UNKNOWN;
}
}
}

59 changes: 59 additions & 0 deletions src/sail-codecs/pnm/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* This file is part of SAIL (https://github.com/HappySeaFox/sail)
Copyright (c) 2023 Dmitry Baryshev
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef SAIL_PNM_HELPERS_H
#define SAIL_PNM_HELPERS_H

#include <stddef.h> /* size_t */

#include <sail-common/common.h>
#include <sail-common/export.h>
#include <sail-common/status.h>

struct sail_image;
struct sail_io;

enum SailPnmVersion {
SAIL_PNM_VERSION_P1,
SAIL_PNM_VERSION_P2,
SAIL_PNM_VERSION_P3,
SAIL_PNM_VERSION_P4,
SAIL_PNM_VERSION_P5,
SAIL_PNM_VERSION_P6,
};

static const char SAIL_PNM_INVALID_STARTING_CHAR = '\0';

SAIL_HIDDEN sail_status_t pnm_private_skip_to_letters_numbers_force_read(struct sail_io *io, char *first_char);

SAIL_HIDDEN sail_status_t pnm_private_skip_to_letters_numbers(struct sail_io *io, char starting_char, char *first_char);

SAIL_HIDDEN sail_status_t pnm_private_read_word(struct sail_io *io, char *str, size_t str_size);

SAIL_HIDDEN sail_status_t pnm_private_read_pixels(struct sail_io *io, struct sail_image *image, unsigned channels, unsigned bpc, double multiplier_to_full_range);

SAIL_HIDDEN enum SailPixelFormat pnm_private_rgb_sail_pixel_format(enum SailPnmVersion pnm_version, unsigned bpc);

#endif
Loading

0 comments on commit 32a87a9

Please sign in to comment.