Austin Schuh | 3333ec7 | 2022-12-29 16:21:06 -0800 | [diff] [blame^] | 1 | /* Copyright (C) 2013-2016, The Regents of The University of Michigan. |
| 2 | All rights reserved. |
| 3 | This software was developed in the APRIL Robotics Lab under the |
| 4 | direction of Edwin Olson, ebolson@umich.edu. This software may be |
| 5 | available under alternative licensing terms; contact the address above. |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are met: |
| 8 | 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | list of conditions and the following disclaimer. |
| 10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | this list of conditions and the following disclaimer in the documentation |
| 12 | and/or other materials provided with the distribution. |
| 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | The views and conclusions contained in the software and documentation are those |
| 24 | of the authors and should not be interpreted as representing official policies, |
| 25 | either expressed or implied, of the Regents of The University of Michigan. |
| 26 | */ |
| 27 | |
| 28 | #include <assert.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include "pam.h" |
| 34 | #include "pnm.h" |
| 35 | #include "image_u8x4.h" |
| 36 | |
| 37 | // least common multiple of 64 (sandy bridge cache line) and 64 (stride needed |
| 38 | // for 16byte-wide RGBA processing). |
| 39 | #define DEFAULT_ALIGNMENT_U8X4 64 |
| 40 | |
| 41 | image_u8x4_t *image_u8x4_create(unsigned int width, unsigned int height) |
| 42 | { |
| 43 | return image_u8x4_create_alignment(width, height, DEFAULT_ALIGNMENT_U8X4); |
| 44 | } |
| 45 | |
| 46 | image_u8x4_t *image_u8x4_create_alignment(unsigned int width, unsigned int height, unsigned int alignment) |
| 47 | { |
| 48 | int stride = 4*width; |
| 49 | |
| 50 | if ((stride % alignment) != 0) |
| 51 | stride += alignment - (stride % alignment); |
| 52 | |
| 53 | uint8_t *buf = calloc(height*stride, sizeof(uint8_t)); |
| 54 | |
| 55 | // const initializer |
| 56 | image_u8x4_t tmp = { .width = width, .height = height, .stride = stride, .buf = buf }; |
| 57 | |
| 58 | image_u8x4_t *im = calloc(1, sizeof(image_u8x4_t)); |
| 59 | memcpy(im, &tmp, sizeof(image_u8x4_t)); |
| 60 | return im; |
| 61 | } |
| 62 | |
| 63 | image_u8x4_t *image_u8x4_copy(const image_u8x4_t *in) |
| 64 | { |
| 65 | uint8_t *buf = malloc(in->height*in->stride*sizeof(uint8_t)); |
| 66 | memcpy(buf, in->buf, in->height*in->stride*sizeof(uint8_t)); |
| 67 | |
| 68 | // const initializer |
| 69 | image_u8x4_t tmp = { .width = in->width, .height = in->height, .stride = in->stride, .buf = buf }; |
| 70 | |
| 71 | image_u8x4_t *copy = calloc(1, sizeof(image_u8x4_t)); |
| 72 | memcpy(copy, &tmp, sizeof(image_u8x4_t)); |
| 73 | return copy; |
| 74 | } |
| 75 | |
| 76 | void image_u8x4_destroy(image_u8x4_t *im) |
| 77 | { |
| 78 | if (!im) |
| 79 | return; |
| 80 | |
| 81 | free(im->buf); |
| 82 | free(im); |
| 83 | } |
| 84 | |
| 85 | //////////////////////////////////////////////////////////// |
| 86 | image_u8x4_t *image_u8x4_create_from_pam(const char *inpath) |
| 87 | { |
| 88 | pam_t *pam = pam_create_from_file(inpath); |
| 89 | if (!pam) |
| 90 | return NULL; |
| 91 | |
| 92 | image_u8x4_t *im = image_u8x4_create(pam->width, pam->height); |
| 93 | |
| 94 | for (int y = 0; y < pam->height; y++) { |
| 95 | if (pam->depth == 1) { |
| 96 | for (int x = 0; x < pam->width; x++) { |
| 97 | im->buf[y*im->stride + 4*x + 0] = pam->data[pam->width*y + x + 0]; |
| 98 | im->buf[y*im->stride + 4*x + 1] = pam->data[pam->width*y + x + 0]; |
| 99 | im->buf[y*im->stride + 4*x + 2] = pam->data[pam->width*y + x + 0]; |
| 100 | im->buf[y*im->stride + 4*x + 3] = 255; |
| 101 | } |
| 102 | } else if (pam->depth == 3) { |
| 103 | for (int x = 0; x < pam->width; x++) { |
| 104 | im->buf[y*im->stride + 4*x + 0] = pam->data[3*pam->width*y + 3*x + 0]; |
| 105 | im->buf[y*im->stride + 4*x + 1] = pam->data[3*pam->width*y + 3*x + 1]; |
| 106 | im->buf[y*im->stride + 4*x + 2] = pam->data[3*pam->width*y + 3*x + 2]; |
| 107 | im->buf[y*im->stride + 4*x + 3] = 255; |
| 108 | } |
| 109 | } else if (pam->depth == 4) { |
| 110 | memcpy(&im->buf[y*im->stride], &pam->data[4*pam->width*y], 4*pam->width); |
| 111 | } else { |
| 112 | assert(0); // not implemented |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pam_destroy(pam); |
| 117 | return im; |
| 118 | } |
| 119 | //////////////////////////////////////////////////////////// |
| 120 | // PNM file i/o |
| 121 | |
| 122 | // Create an RGBA image from PNM |
| 123 | image_u8x4_t *image_u8x4_create_from_pnm(const char *path) |
| 124 | { |
| 125 | pnm_t *pnmp = pnm_create_from_file(path); |
| 126 | if (pnmp == NULL) |
| 127 | return NULL; |
| 128 | |
| 129 | pnm_t pnm = *pnmp; |
| 130 | image_u8x4_t *imp = NULL; |
| 131 | |
| 132 | switch (pnm.format) { |
| 133 | case PNM_FORMAT_GRAY: { |
| 134 | imp = image_u8x4_create(pnm.width, pnm.height); |
| 135 | |
| 136 | // copy struct by value for common subexpression elimination |
| 137 | const image_u8x4_t im = *imp; |
| 138 | |
| 139 | for (int y = 0; y < im.height; y++) { |
| 140 | for (int x = 0; x < im.width; x++) { |
| 141 | uint8_t gray = pnm.buf[y*pnm.width + x]; |
| 142 | im.buf[y*im.stride + 4*x + 0] = gray; |
| 143 | im.buf[y*im.stride + 4*x + 1] = gray; |
| 144 | im.buf[y*im.stride + 4*x + 2] = gray; |
| 145 | im.buf[y*im.stride + 4*x + 3] = 0xff; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | case PNM_FORMAT_RGB: { |
| 153 | imp = image_u8x4_create(pnm.width, pnm.height); |
| 154 | |
| 155 | // copy struct by value for common subexpression elimination |
| 156 | const image_u8x4_t im = *imp; |
| 157 | |
| 158 | // Gray conversion for RGB is gray = (r + g + g + b)/4 |
| 159 | for (int y = 0; y < im.height; y++) { |
| 160 | for (int x = 0; x < im.width; x++) { |
| 161 | |
| 162 | uint8_t r = pnm.buf[y*pnm.width*3 + 3*x + 0]; |
| 163 | uint8_t g = pnm.buf[y*pnm.width*3 + 3*x + 1]; |
| 164 | uint8_t b = pnm.buf[y*pnm.width*3 + 3*x + 2]; |
| 165 | |
| 166 | im.buf[y*im.stride + 4*x + 0] = r; |
| 167 | im.buf[y*im.stride + 4*x + 1] = g; |
| 168 | im.buf[y*im.stride + 4*x + 2] = b; |
| 169 | im.buf[y*im.stride + 4*x + 3] = 0xff; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | pnm_destroy(pnmp); |
| 178 | return imp; |
| 179 | } |
| 180 | |
| 181 | int image_u8x4_write_pnm(const image_u8x4_t *imp, const char *path) |
| 182 | { |
| 183 | // copy struct by value to ensure common subexpression elimination occurs |
| 184 | const image_u8x4_t im = *imp; |
| 185 | |
| 186 | FILE *f = fopen(path, "wb"); |
| 187 | int res = 0; |
| 188 | |
| 189 | if (f == NULL) { |
| 190 | res = -1; |
| 191 | goto finish; |
| 192 | } |
| 193 | |
| 194 | // Only outputs to RGB |
| 195 | fprintf(f, "P6\n%d %d\n255\n", im.width, im.height); |
| 196 | |
| 197 | for (int y = im.height-1; y >= 0; y--) { |
| 198 | for (int x = 0; x < im.width; x++) { |
| 199 | |
| 200 | uint8_t r = im.buf[y*im.stride + 4*x + 0]; |
| 201 | uint8_t g = im.buf[y*im.stride + 4*x + 1]; |
| 202 | uint8_t b = im.buf[y*im.stride + 4*x + 2]; |
| 203 | |
| 204 | fwrite(&r, 1, 1, f); |
| 205 | fwrite(&g, 1, 1, f); |
| 206 | fwrite(&b, 1, 1, f); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | finish: |
| 211 | if (f != NULL) |
| 212 | fclose(f); |
| 213 | |
| 214 | return res; |
| 215 | } |
| 216 | |
| 217 | void image_u8x4_write_pam(const image_u8x4_t *im, const char *path) |
| 218 | { |
| 219 | FILE *f = fopen(path, "w"); |
| 220 | fprintf(f, "P7\n"); |
| 221 | fprintf(f, "WIDTH %d\n", im->width); |
| 222 | fprintf(f, "HEIGHT %d\n", im->height); |
| 223 | fprintf(f, "DEPTH 4\n"); |
| 224 | fprintf(f, "MAXVAL 255\n"); |
| 225 | fprintf(f, "TUPLTYPE RGB_ALPHA\n"); |
| 226 | fprintf(f, "ENDHDR\n"); |
| 227 | |
| 228 | for (int y = 0; y < im->height; y++) |
| 229 | fwrite(&im->buf[y*im->stride], 1, 4*im->width, f); |
| 230 | |
| 231 | fclose(f); |
| 232 | |
| 233 | } |