File: io.c

package info (click to toggle)
libm4ri 20200125-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,560 kB
  • sloc: ansic: 12,633; sh: 4,304; makefile: 137
file content (374 lines) | stat: -rw-r--r-- 10,411 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*******************************************************************
*
*                M4RI: Linear Algebra over GF(2)
*
*    Copyright (C) 2011 Martin Albrecht <martinralbrecht@googlemail.com>
*
*  Distributed under the terms of the GNU General Public License (GPL)
*  version 2 or higher.
*
*    This code is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*    General Public License for more details.
*
*  The full text of the GPL is available at:
*
*                  http://www.gnu.org/licenses/
*
********************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "m4ri_config.h"

#if __M4RI_HAVE_LIBPNG
#include <png.h>
#endif //__M4RI_HAVE_LIBPNG


#include "io.h"
#include "echelonform.h"

void mzd_info(const mzd_t *A, int do_rank) {
  printf("nrows: %6d, ncols: %6d, density: %6.5f, hash: 0x%016zx",A->nrows,A->ncols,mzd_density(A,1),mzd_hash(A));
  if(do_rank) {
    mzd_t *AA = mzd_copy(NULL, A);
    printf(", rank: %6zu\n",(size_t)mzd_echelonize(AA,0));
    mzd_free(AA);
  } else {
    printf("\n");
  }
}

#define SAFECHAR (m4ri_radix + m4ri_radix / 4 + 1)

void mzd_fprint_row(FILE* stream, mzd_t const *M, const rci_t i) {
  char temp[SAFECHAR];
  fprintf(stream, "[");
  word *row = M->rows[i];
  for (wi_t j = 0; j < M->width - 1; ++j) {
    m4ri_word_to_str(temp, row[j], 1);
    fprintf(stream, "%s|", temp);
  }
  row = row + M->width - 1;
  int const wide = (M->ncols % m4ri_radix) ? M->ncols % m4ri_radix : m4ri_radix;
  for (int j = 0; j < wide; ++j) {
    if(j != 0 && (j % 4) == 0)
      fprintf(stream, ":");
    if (__M4RI_GET_BIT(*row, j)) 
      fprintf(stream, "1");
    else
      fprintf(stream, " ");
  }
  fprintf(stream, "]\n");
}


#if __M4RI_HAVE_LIBPNG
#define PNGSIGSIZE 8

mzd_t * mzd_from_png(const char *fn, int verbose) {
  int retval = 0;
  mzd_t *A = NULL;
  png_byte pngsig[PNGSIGSIZE];

  FILE *fh = fopen(fn,"rb");

  if (!fh) {
    if (verbose)
      printf("Could not open file '%s' for reading\n",fn);
    return NULL;
  };
    
  if (fread((char*)pngsig, PNGSIGSIZE, 1, fh) != 1) {
    if (verbose)
      printf("Could not read file '%s'\n",fn);
    retval = 1;
    goto from_png_close_fh;
  }

  if (png_sig_cmp(pngsig, 0, PNGSIGSIZE) != 0) {
    if (verbose)
      printf("'%s' is not a PNG file.\n",fn);
    retval = 2;
    goto from_png_close_fh;
  }

  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  if (!png_ptr) {
    if (verbose)
      printf("failed to initialise PNG read struct.\n");
    retval = 3;
    goto from_png_close_fh;
  }
  png_set_user_limits(png_ptr, 0x7fffffffL,  0x7fffffffL);

  png_infop info_ptr = png_create_info_struct(png_ptr);

  if (!info_ptr) {
    if (verbose)
      printf("failed to initialise PNG info struct\n");
    retval = 3;
    goto from_png_destroy_read_struct;
  }

  png_init_io(png_ptr, fh);
  png_set_sig_bytes(png_ptr, PNGSIGSIZE);
  png_read_info(png_ptr, info_ptr);
 
  const png_uint_32 m                = png_get_image_height(png_ptr, info_ptr);
  const png_uint_32 n                = png_get_image_width(png_ptr, info_ptr);
  const png_uint_32 bit_depth        = png_get_bit_depth(png_ptr, info_ptr);
  const png_uint_32 channels         = png_get_channels(png_ptr, info_ptr);
  const png_uint_32 color_type       = png_get_color_type(png_ptr, info_ptr);
  const png_uint_32 compression_type = png_get_compression_type(png_ptr, info_ptr);
  const png_uint_32 interlace_type   = png_get_interlace_type(png_ptr, info_ptr);

  if (interlace_type != PNG_INTERLACE_NONE) {
    if (verbose)
      printf("interlaced images not supported\n");
    goto from_png_destroy_read_struct;
  };

  if (verbose)
    printf("reading %u x %u matrix (bit depth: %u, channels: %u, color type: %u, compression type: %u)\n",(unsigned int)m,
           (unsigned int)n, (unsigned int)bit_depth, (unsigned int)channels, (unsigned int)color_type, (unsigned int)compression_type);

  if(color_type != 0 && color_type != 3) {
    if (verbose)
      printf("only graycscale and palette colors are supported.\n");
    goto from_png_destroy_read_struct;
  }
    
  A = mzd_init(m, n);
  const word bitmask_end = A->high_bitmask;
  png_bytep row = m4ri_mm_calloc(sizeof(char),n/8+1);

  word tmp;
  wi_t j;

  png_set_packswap(png_ptr);
  //png_set_invert_mono(png_ptr);

  for(rci_t i=0; i<m; i++) {
    png_read_row(png_ptr, row, NULL);
    for(j=0; j<A->width-1; j++) {
      tmp = ((word)row[8*j+7])<<56 | ((word)row[8*j+6])<<48 \
        |   ((word)row[8*j+5])<<40 | ((word)row[8*j+4])<<32 \
        |   ((word)row[8*j+3])<<24 | ((word)row[8*j+2])<<16 \
        |   ((word)row[8*j+1])<< 8 | ((word)row[8*j+0])<< 0;
      A->rows[i][j] = ~tmp;
    }
    tmp = 0;
    switch((n/8 + ((n%8) ? 1 : 0))%8) {
    case 0: tmp |= ((word)row[8*j+7])<<56;
    case 7: tmp |= ((word)row[8*j+6])<<48;
    case 6: tmp |= ((word)row[8*j+5])<<40;
    case 5: tmp |= ((word)row[8*j+4])<<32;
    case 4: tmp |= ((word)row[8*j+3])<<24;
    case 3: tmp |= ((word)row[8*j+2])<<16;
    case 2: tmp |= ((word)row[8*j+1])<< 8;
    case 1: tmp |= ((word)row[8*j+0])<< 0;
    };
    A->rows[i][j] |= (~tmp & bitmask_end);
  }

  m4ri_mm_free(row);
  png_read_end(png_ptr, NULL);

 from_png_destroy_read_struct: 
  png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)0);

 from_png_close_fh: 
  fclose(fh);

  if (retval != 0 && A) {
    mzd_free(A);
    return NULL;
  } else {
    return A;
  }
}

int mzd_to_png(const mzd_t *A, const char *fn, int compression_level, const char *comment, int verbose) {
  FILE *fh = fopen(fn, "wb");

  if (!fh) {
    if(verbose)
      printf("Could not open file '%s' for writing\n",fn);
    return 1;
  }

  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  if (!png_ptr) {
    if(verbose)
      printf("failed to initialise PNG write struct.\n");
    fclose(fh);
    return 3;
  }
  png_set_user_limits(png_ptr, 0x7fffffffL,  0x7fffffffL);

  png_infop info_ptr = png_create_info_struct(png_ptr);

  if (!info_ptr) {
    if (verbose)
      printf("failed to initialise PNG info struct\n");
    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(fh);
    return 3;
  }

  if (setjmp(png_jmpbuf(png_ptr))) {
    if (verbose)
      printf("error writing PNG file\n");
    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(fh);
    return 1;
  }

  png_init_io(png_ptr, fh);
  png_set_compression_level(png_ptr, compression_level);

  png_set_IHDR(png_ptr, info_ptr, A->ncols, A->nrows, 1, \
               PNG_COLOR_TYPE_GRAY, \
               PNG_INTERLACE_NONE, \
               PNG_COMPRESSION_TYPE_DEFAULT, \
               PNG_FILTER_TYPE_DEFAULT);

  png_text txt_ptr[3];

  char pdate[21];
  time_t ptime=time(NULL);
  struct tm *ltime=localtime(&ptime);
  sprintf(pdate,"%04d/%02d/%02d %02d:%02d:%02d",ltime->tm_year+1900,ltime->tm_mon+1,ltime->tm_mday,ltime->tm_hour,ltime->tm_min,ltime->tm_sec);

  txt_ptr[0].key="Software";
  txt_ptr[0].text="M4RI";
  txt_ptr[0].compression=PNG_TEXT_COMPRESSION_NONE;
  txt_ptr[1].key="Date";
  txt_ptr[1].text=pdate;
  txt_ptr[1].compression=PNG_TEXT_COMPRESSION_NONE;
  txt_ptr[2].key="Comment";
  txt_ptr[2].text=(char*)comment;
  txt_ptr[2].compression=PNG_TEXT_COMPRESSION_NONE;

  png_set_text(png_ptr, info_ptr, txt_ptr, 3);

  png_write_info(png_ptr, info_ptr);

  png_set_packswap(png_ptr);
  png_set_invert_mono(png_ptr);
  
  png_bytep row = m4ri_mm_calloc(sizeof(char),A->ncols/8+8);

  wi_t j=0;
  word tmp = 0;
  for(rci_t i=0; i<A->nrows; i++) {
    word *rowptr = A->rows[i];
    for(j=0; j<A->width-1; j++) {
      tmp = rowptr[j];
      row[8*j+0] = (png_byte)((tmp>> 0) & 0xff);
      row[8*j+1] = (png_byte)((tmp>> 8) & 0xff);
      row[8*j+2] = (png_byte)((tmp>>16) & 0xff);
      row[8*j+3] = (png_byte)((tmp>>24) & 0xff);
      row[8*j+4] = (png_byte)((tmp>>32) & 0xff);
      row[8*j+5] = (png_byte)((tmp>>40) & 0xff);
      row[8*j+6] = (png_byte)((tmp>>48) & 0xff);
      row[8*j+7] = (png_byte)((tmp>>56) & 0xff);
    }
    tmp = rowptr[j];
    switch( (A->ncols/8 + ((A->ncols%8) ? 1 : 0)) %8 ) {
    case 0: row[8*j+7] = (png_byte)((tmp>>56) & 0xff);
    case 7: row[8*j+6] = (png_byte)((tmp>>48) & 0xff);
    case 6: row[8*j+5] = (png_byte)((tmp>>40) & 0xff); 
    case 5: row[8*j+4] = (png_byte)((tmp>>32) & 0xff); 
    case 4: row[8*j+3] = (png_byte)((tmp>>24) & 0xff);
    case 3: row[8*j+2] = (png_byte)((tmp>>16) & 0xff);
    case 2: row[8*j+1] = (png_byte)((tmp>> 8) & 0xff);
    case 1: row[8*j+0] = (png_byte)((tmp>> 0) & 0xff);
    };
    png_write_row(png_ptr, row);
  }
  m4ri_mm_free(row);

  png_write_end(png_ptr, info_ptr);
  png_destroy_write_struct(&png_ptr, &info_ptr);
  fclose(fh);
  return 0;
}

#endif //__M4RI_HAVE_LIBPNG

mzd_t *mzd_from_jcf(const char *fn, int verbose) {
  int retval = 0;
  mzd_t *A = NULL;
  FILE *fh = fopen(fn,"r");

  rci_t m,n;
  long p  = 0;
  long nonzero = 0;

  if (!fh) {
    if (verbose)
      printf("Could not open file '%s' for reading\n",fn);
    return NULL;
  }

  if (fscanf(fh, "%d %d %ld\n%ld\n\n",&m,&n,&p,&nonzero) != 4) {
    if (verbose)
      printf("File '%s' does not seem to be in JCF format.",fn);
    retval = 1;
    goto from_jcf_close_fh;
  }

  if(p != 2) {
    if (verbose)
      printf("Expected p==2 but found p==%ld\n",p);
    retval = 1;
    goto from_jcf_close_fh;
  }

  if (verbose)
    printf("reading %lu x %lu matrix with at most %ld non-zero entries (density at most: %6.5f)\n",
           (unsigned long)m, (unsigned long)n, (unsigned long)nonzero, ((double)nonzero)/((double)m*n));

  A = mzd_init(m,n);
  
  long i = -1;
  long j = 0;

  while(fscanf(fh,"%ld\n",&j) == 1) {
    if (j<0) {
      i++, j = -j;
    }
    if (((j-1) >= n) || (i>= m))
      m4ri_die("trying to write to (%ld,%ld) in %ld x %ld matrix\n", i, j-1, m, n);
    mzd_write_bit(A, i, j-1, 1);
  };

 from_jcf_close_fh:
  fclose(fh);

  if(retval != 0 && A) {
    mzd_free(A);
    return NULL;
  } else {
    return A;
  }
}

mzd_t *mzd_from_str(rci_t m, rci_t n, const char *str) {
  int idx = 0;
  mzd_t *A = mzd_init(m, n);
  for(rci_t i=0; i<A->nrows; i++) {
    for(rci_t j=0; j<A->ncols; j++) {
      mzd_write_bit(A, i, j, str[idx++] == '1');
    }
  }
  return A;
}