Free Hero Mesh

Check-in [2da92b67e8]
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.
Overview
Comment:Implement recompression modified pictures
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2da92b67e8fb6c61b03e541a88e5461df4b57256
User & Date: user on 2021-01-20 02:48:38
Other Links: manifest | tags
Context
2021-02-03
02:13
Implement clicking on MRU on left margin in level editor in order to select one check-in: c52084ea2e user: user tags: trunk
2021-01-20
02:48
Implement recompression modified pictures check-in: 2da92b67e8 user: user tags: trunk
2021-01-19
02:43
Implement import/export in the picture editor. check-in: a5ef9613c5 user: user tags: trunk
Changes

Modified picedit.c from [304021af13] to [2216af07de].

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
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







-
+




















+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    n--;
    if(t==2) c=fgetc(fp);
    if(t==3) c=p[-pic->size];
    *p++=c;
  }
}

static inline void load_rotate(Picture*pic) {
static void load_rotate(Picture*pic) {
  int x,y;
  int m=pic->meth;
  int s=pic->size;
  Uint8*d=pic->data+s;
  static Uint8*buf;
  Uint8*p;
  if(!buf) {
    buf=malloc(255*255);
    if(!buf) fatal("Allocation failed\n");
  }
  p=buf;
  for(y=0;y<s;y++) for(x=0;x<s;x++) {
    if(m&1) x=s-x-1;
    if(m&2) y=s-y-1;
    *p++=d[m&4?x*s+y:y*s+x];
    if(m&1) x=s-x-1;
    if(m&2) y=s-y-1;
  }
  memcpy(d,buf,s*s);
}

static void out_run(FILE*fp,int ch,int am,const Uint8*d,int le) {
  int n=am%85?:85;
  fputc(ch+n-1,fp);
  if(le) fwrite(d,1,le<n?le:n,fp);
  if(ch) d+=n,le-=n;
  while(am-=n) {
    n=am>7225?7225:am;
    fputc(ch+n/85-1,fp);
    if(le>0) fwrite(d,1,le<n?le:n,fp);
    if(ch) d+=n,le-=n;
  }
}

static void compress_picture_1(FILE*fp,Picture*pic) {
  int ps=pic->size;
  int ms=ps*ps;
  const Uint8*d=pic->data+ps;
  int i=0;
  int ca,homo,hetero;
  while(i<ms) {
    ca=0;
    while(i+ca<ms && d[i+ca]==d[i+ca-ps]) ca++;
    homo=1;
    while(i+homo<ms && d[i+homo]==d[i]) homo++;
    hetero=1;
    while(i+hetero<ms) {
      if(d[i+hetero]==d[i+hetero-ps] && d[i+hetero+1]==d[i+hetero+1-ps]) break;
      if(d[i+hetero]==d[i+hetero+1] && i+hetero==ms-1) break;
      if(d[i+hetero]==d[i+hetero+1] && d[i+hetero]==d[i+hetero+2]) break;
      hetero++;
    }
    if(ca>=homo && (ca>=hetero || homo>1)) {
      out_run(fp,170,ca,0,0);
      i+=ca;
    } else if(homo>1) {
      out_run(fp,0,homo-1,d+i,1);
      i+=homo;
    } else {
      if(hetero>85) hetero=85;
      out_run(fp,85,hetero,d+i,hetero);
      i+=hetero;
    }
  }
}

static void compress_picture(FILE*out,Picture*pic) {
  int bm=15;
  int bs=pic->size*pic->size;
  FILE*fp=fmemopen(0,bs,"w");
  int i,j;
  if(!fp) fatal("Error with fmemopen");
  for(i=0;i<8;i++) {
    compress_picture_1(fp,pic);
    if(!ferror(fp)) {
      j=ftell(fp);
      if(j>0 && j<bs) bs=j,bm=i;
    }
    rewind(fp);
    pic->meth="\1\3\1\7\1\3\1\7"[i];
    load_rotate(pic);
  }
  pic->meth=bm;
  if(bm && bm!=15) load_rotate(pic);
  fclose(fp);
  if(bm==15) fwrite(pic->data+pic->size,pic->size,pic->size,out); else compress_picture_1(out,pic);
}

static void block_rotate(Uint8*d,Uint8 s,Uint8 w,Uint8 h,Uint8 m) {
  Uint8*b=malloc(w*h);
  Uint8*p=b;
  int x,y;
  if(!b) fatal("Allocation failed\n");
  if(w!=h && (m&4)) goto end;
795
796
797
798
799
800
801



802

803
804
805
806
807
808
809
862
863
864
865
866
867
868
869
870
871

872
873
874
875
876
877
878
879







+
+
+
-
+







}

static void edit_picture(sqlite3_int64 id) {
  sqlite3_stmt*st;
  Picture*pict[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  char*name;
  const unsigned char*data;
  Uint8*buf=0;
  size_t size=0;
  FILE*fp;
  int i;
  int i,n;
  if(i=sqlite3_prepare_v2(userdb,"SELECT SUBSTR(`NAME`,1,LENGTH(`NAME`)-4),`DATA` FROM `PICEDIT` WHERE `ID`=?1;",-1,&st,0)) {
    screen_message(sqlite3_errmsg(userdb));
    return;
  }
  sqlite3_bind_int64(st,1,id);
  i=sqlite3_step(st);
  if(i!=SQLITE_ROW) {
822
823
824
825
826
827
828











829
830
831





832
833





834
835
836
837
838
839
840
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909



910
911
912
913
914
915

916
917
918
919
920
921
922
923
924
925
926
927







+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+

-
+
+
+
+
+







    *pict=malloc(sizeof(Picture)+(i+1)*i);
    if(!*pict) fatal("Allocation failed");
    pict[0]->size=i;
    memset(pict[0]->data,0,(i+1)*i);
  }
  edit_picture_1(pict,name);
  free(name);
  fp=open_memstream((char**)&buf,&size);
  if(!fp) fatal("Cannot open memory stream\n");
  for(i=n=0;i<16;i++) if(pict[i]) n++;
  fputc(n,fp);
  for(i=0;i<n;i++) fputc(pict[i]->size,fp);
  for(i=0;i<n>>1;i++) fputc(0,fp);
  for(i=0;i<n;i++) compress_picture(fp,pict[i]);
  fclose(fp);
  if(!buf || !size) fatal("Allocation failed\n");
  *buf|=pict[0]->meth<<4;
  for(i=1;i<n;i++) buf[n+1+((i-1)>>1)]|=pict[i]->meth<<(i&1?0:4);
  for(i=0;i<16;i++) {
    
    free(pict[i]);
  for(i=0;i<16;i++) free(pict[i]);
  if(i=sqlite3_prepare_v2(userdb,"UPDATE `PICEDIT` SET `DATA`=?2 WHERE ID=?1;",-1,&st,0)) {
    screen_message(sqlite3_errmsg(userdb));
    free(buf);
    return;
  }
  
  sqlite3_bind_int64(st,1,id);
  sqlite3_bind_blob(st,2,buf,size,free);
  i=sqlite3_step(st);
  if(i!=SQLITE_DONE) screen_message(sqlite3_errmsg(userdb));
  sqlite3_finalize(st);
}

static int add_picture(int t) {
  sqlite3_stmt*st;
  const char*s=screen_prompt("Enter name of new picture:");
  int i;
  if(!s || !*s) return 0;

Modified picedit.doc from [ef1b74df0a] to [73e280af70].

16
17
18
19
20
21
22




23
24
25
26
27
28
29
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33







+
+
+
+







=== Main menu ===

The main menu displays the list of the names of all pictures. This list is
sorted by name.

You can push escape to quit and save, or shift+escape quits without saving.

The F1 key adds a picture.

The F2 key deletes a picture by name.

The F3 key allows entering a name of a picture to edit.


=== Drawing ===

The following keyboard commands are available: