<commit_before>/********************************************************************[libaroma]*
 * Copyright (C) 2011-2015 <NAME> (http://amarullz.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *______________________________________________________________________________
 *
 * Filename    : ctl_image.c
 * Description : image control
 *
 * + This is part of libaroma, an embedded ui toolkit.
 * + 28/01/15 - Author(s): <NAME>
 *
 */
#ifndef __libaroma_ctl_image_c__
#define __libaroma_ctl_image_c__
#include <aroma_internal.h>
#include "../ui/ui_internal.h"

/* HANDLER */
dword _libaroma_ctl_image_msg(LIBAROMA_CONTROLP, LIBAROMA_MSGP);
void _libaroma_ctl_image_draw (LIBAROMA_CONTROLP, LIBAROMA_CANVASP);
void _libaroma_ctl_image_destroy(LIBAROMA_CONTROLP);
byte _libaroma_ctl_image_thread(LIBAROMA_CONTROLP ctl);
static LIBAROMA_CONTROL_HANDLER _libaroma_ctl_image_handler={
  message:_libaroma_ctl_image_msg,
  draw:_libaroma_ctl_image_draw,
  focus:NULL,
  destroy:_libaroma_ctl_image_destroy,
  thread:_libaroma_ctl_image_thread
};
  
/*
 * Structure   : __LIBAROMA_CTL_IMAGE
 * Typedef     : _LIBAROMA_CTL_IMAGE, * _LIBAROMA_CTL_IMAGEP
 * Descriptions: internal label data
 */
typedef struct __LIBAROMA_CTL_IMAGE _LIBAROMA_CTL_IMAGE;
typedef struct __LIBAROMA_CTL_IMAGE * _LIBAROMA_CTL_IMAGEP;
struct __LIBAROMA_CTL_IMAGE{
  LIBAROMA_CANVASP img;
  LIBAROMA_MUTEX mutex;
  byte update;
  byte hidden;
  byte mode;
};

byte _libaroma_ctl_image_thread(LIBAROMA_CONTROLP ctl) {
  _LIBAROMA_CTL_CHECK(
	  _libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 0
	);
  if (me->update){
    libaroma_mutex_lock(me->mutex);
  	me->update=0;
  	libaroma_mutex_unlock(me->mutex);
    return 1;
  }
  return 0;
}

void _libaroma_ctl_image_destroy(LIBAROMA_CONTROLP ctl){
  _LIBAROMA_CTL_CHECK(
	  _libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 
	);
	libaroma_mutex_lock(me->mutex);
  if (me->img){
    libaroma_canvas_free(me->img);
  }
  libaroma_mutex_unlock(me->mutex);
  libaroma_mutex_free(me->mutex);
  free(me);
}

void _libaroma_ctl_image_draw(
    LIBAROMA_CONTROLP ctl, LIBAROMA_CANVASP c){
  _LIBAROMA_CTL_CHECK(
	  _libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 
	);
  libaroma_control_erasebg(ctl,c);
  if (!me->hidden){
    libaroma_mutex_lock(me->mutex);
    if (me->img){
      int dx = 0;
      int dy = 0;
      int dw = c->w;
      int dh = c->h;
      int sx = 0;
      int sy = 0;
      int sw = me->img->w;
      int sh = me->img->h;

      if ((me->mode & LIBAROMA_CTL_IMAGE_MODE_NOSCALE) == LIBAROMA_CTL_IMAGE_MODE_NOSCALE){
        if ((me->mode & LIBAROMA_CTL_IMAGE_MODE_CENTER) == LIBAROMA_CTL_IMAGE_MODE_CENTER){
          dx = (c->w >> 1) - (me->img->w >> 1);
          dy = (c->h >> 1) - (me->img->h >> 1);
          libaroma_draw(c, me->img, dx, dy, 1);
        }
        else{
          libaroma_draw(c, me->img, dx, dy, 1);
        }
      }
      else if ((me->mode & 0xf0) != 0){
        byte isfit = ((me->mode & LIBAROMA_CTL_IMAGE_MODE_FIT) == LIBAROMA_CTL_IMAGE_MODE_FIT);
        /* prevent div by zero */
        if ((me->img->w>0)&&(me->img->h>0)){
          float b = c->w/((float) me->img->w);
          int hcheck = (int) (b * me->img->h);
          if (isfit){
            if (hcheck>c->h) b = c->h/((float) me->img->h);
          }
          else{
            if (hcheck<c->h) b = c->h/((float) me->img->h);
          }
          int w2=(int)(me->img->w*b);
          int h2=(int)(me->img->h*b);
          int x2=(w2-c->w)>>1;
          int y2=(h2-c->h)>>1;
          dx -= x2;
          dy -= y2;
          dw = w2;
          dh = h2;
          if (dx<0){
            sx += (0 - dx) / b;
            sw += dx / b;
            dw += dx;
            dx = 0;
          }
          if (dy<0){
            sy += (0 - dy) / b;
            sh += dy / b;
            dh += dy;
            dy = 0;
          }
          if (dw>c->w){
            sw -= (dw - c->w) / b;
            dw = c->w;
          }
          if (dh>c->h){
            sh -= (dh - c->h) / b;
            dh = c->h;
          }
        }
      }
      if ((me->mode & LIBAROMA_CTL_IMAGE_MODE_NOSMOOTH) == LIBAROMA_CTL_IMAGE_MODE_NOSMOOTH){
        libaroma_draw_scale_nearest(c, me->img, dx,dy,dw,dh,sx,sy,sw,sh);
      }
      else{
        libaroma_draw_scale_smooth(c, me->img, dx,dy,dw,dh,sx,sy,sw,sh);
      }
    }
    libaroma_mutex_unlock(me->mutex);
  }
}

dword _libaroma_ctl_image_msg(LIBAROMA_CONTROLP ctl, LIBAROMA_MSGP msg){
  /* internal check */
  _LIBAROMA_CTL_CHECK(
    _libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 0
  );
  
  switch(msg->msg){
    case LIBAROMA_MSG_WIN_ACTIVE:
    case LIBAROMA_MSG_WIN_INACTIVE:
    case LIBAROMA_MSG_WIN_RESIZE:
      {
        libaroma_mutex_lock(me->mutex);
        me->update=1;
        libaroma_mutex_unlock(me->mutex);
      }
      break;
  }
  return 0;
}

byte libaroma_ctl_image_hidden(
	LIBAROMA_CONTROLP ctl,byte hidden){
  _LIBAROMA_CTL_CHECK(
		_libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 0
	);<commit_msg>Fix no-scale flag for control image scale mode<commit_after>/********************************************************************[libaroma]*
 * Copyright (C) 2011-2015 <NAME> (http://amarullz.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *______________________________________________________________________________
 *
 * Filename    : ctl_image.c
 * Description : image control
 *
 * + This is part of libaroma, an embedded ui toolkit.
 * + 28/01/15 - Author(s): <NAME>
 *
 */
#ifndef __libaroma_ctl_image_c__
#define __libaroma_ctl_image_c__
#include <aroma_internal.h>
#include "../ui/ui_internal.h"

/* HANDLER */
dword _libaroma_ctl_image_msg(LIBAROMA_CONTROLP, LIBAROMA_MSGP);
void _libaroma_ctl_image_draw (LIBAROMA_CONTROLP, LIBAROMA_CANVASP);
void _libaroma_ctl_image_destroy(LIBAROMA_CONTROLP);
byte _libaroma_ctl_image_thread(LIBAROMA_CONTROLP ctl);
static LIBAROMA_CONTROL_HANDLER _libaroma_ctl_image_handler={
  message:_libaroma_ctl_image_msg,
  draw:_libaroma_ctl_image_draw,
  focus:NULL,
  destroy:_libaroma_ctl_image_destroy,
  thread:_libaroma_ctl_image_thread
};
  
/*
 * Structure   : __LIBAROMA_CTL_IMAGE
 * Typedef     : _LIBAROMA_CTL_IMAGE, * _LIBAROMA_CTL_IMAGEP
 * Descriptions: internal label data
 */
typedef struct __LIBAROMA_CTL_IMAGE _LIBAROMA_CTL_IMAGE;
typedef struct __LIBAROMA_CTL_IMAGE * _LIBAROMA_CTL_IMAGEP;
struct __LIBAROMA_CTL_IMAGE{
  LIBAROMA_CANVASP img;
  LIBAROMA_MUTEX mutex;
  byte update;
  byte hidden;
  byte mode;
};

byte _libaroma_ctl_image_thread(LIBAROMA_CONTROLP ctl) {
  _LIBAROMA_CTL_CHECK(
	  _libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 0
	);
  if (me->update){
    libaroma_mutex_lock(me->mutex);
  	me->update=0;
  	libaroma_mutex_unlock(me->mutex);
    return 1;
  }
  return 0;
}

void _libaroma_ctl_image_destroy(LIBAROMA_CONTROLP ctl){
  _LIBAROMA_CTL_CHECK(
	  _libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 
	);
	libaroma_mutex_lock(me->mutex);
  if (me->img){
    libaroma_canvas_free(me->img);
  }
  libaroma_mutex_unlock(me->mutex);
  libaroma_mutex_free(me->mutex);
  free(me);
}

void _libaroma_ctl_image_draw(
    LIBAROMA_CONTROLP ctl, LIBAROMA_CANVASP c){
  _LIBAROMA_CTL_CHECK(
	  _libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 
	);
  libaroma_control_erasebg(ctl,c);
  if (!me->hidden){
    libaroma_mutex_lock(me->mutex);
    if (me->img){
      int dx = 0;
      int dy = 0;
      int dw = c->w;
      int dh = c->h;
      int sx = 0;
      int sy = 0;
      int sw = me->img->w;
      int sh = me->img->h;

      if ((me->mode & LIBAROMA_CTL_IMAGE_MODE_NOSCALE) == LIBAROMA_CTL_IMAGE_MODE_NOSCALE){
        if ((me->mode & LIBAROMA_CTL_IMAGE_MODE_CENTER) == LIBAROMA_CTL_IMAGE_MODE_CENTER){
          dx = (c->w >> 1) - (me->img->w >> 1);
          dy = (c->h >> 1) - (me->img->h >> 1);
          dw = me->img->w;
          dh = me->img->h;
          if (dx < 0)
          {
            sx -= dx;
            sw += dx;
            dw += dx;
            dx = 0;
          }
          if (dy<0){
            sy -= dy;
            sh += dy;
            dh += dy;
            dy = 0;
          }
        }
        if (dw>c->w){
          sw -= (dw - c->w);
          dw = c->w;
        }
        if (dh>c->h){
          sh -= (dh - c->h);
          dh = c->h;
        }
        libaroma_draw_ex(c, me->img, dx, dy, sx, sy, sw, sh, 1, 0xff);
      }
      else{
        if ((me->mode & 0xf0) != 0){
          byte isfit = ((me->mode & LIBAROMA_CTL_IMAGE_MODE_FIT) == LIBAROMA_CTL_IMAGE_MODE_FIT);
          /* prevent div by zero */
          if ((me->img->w>0)&&(me->img->h>0)){
            float b = c->w/((float) me->img->w);
            int hcheck = (int) (b * me->img->h);
            if (isfit){
              if (hcheck>c->h) b = c->h/((float) me->img->h);
            }
            else{
              if (hcheck<c->h) b = c->h/((float) me->img->h);
            }
            int w2=(int)(me->img->w*b);
            int h2=(int)(me->img->h*b);
            int x2=(w2-c->w)>>1;
            int y2=(h2-c->h)>>1;
            dx -= x2;
            dy -= y2;
            dw = w2;
            dh = h2;
            if (dx<0){
              sx -= dx / b;
              sw += dx / b;
              dw += dx;
              dx = 0;
            }
            if (dy<0){
              sy -= dy / b;
              sh += dy / b;
              dh += dy;
              dy = 0;
            }
            if (dw>c->w){
              sw -= (dw - c->w) / b;
              dw = c->w;
            }
            if (dh>c->h){
              sh -= (dh - c->h) / b;
              dh = c->h;
            }
          }
        }
        if ((me->mode & LIBAROMA_CTL_IMAGE_MODE_NOSMOOTH) == LIBAROMA_CTL_IMAGE_MODE_NOSMOOTH){
          libaroma_draw_scale_nearest(c, me->img, dx,dy,dw,dh,sx,sy,sw,sh);
        }
        else{
          libaroma_draw_scale_smooth(c, me->img, dx,dy,dw,dh,sx,sy,sw,sh);
        }
      }
    }
    libaroma_mutex_unlock(me->mutex);
  }
}

dword _libaroma_ctl_image_msg(LIBAROMA_CONTROLP ctl, LIBAROMA_MSGP msg){
  /* internal check */
  _LIBAROMA_CTL_CHECK(
    _libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 0
  );
  
  switch(msg->msg){
    case LIBAROMA_MSG_WIN_ACTIVE:
    case LIBAROMA_MSG_WIN_INACTIVE:
    case LIBAROMA_MSG_WIN_RESIZE:
      {
        libaroma_mutex_lock(me->mutex);
        me->update=1;
        libaroma_mutex_unlock(me->mutex);
      }
      break;
  }
  return 0;
}

byte libaroma_ctl_image_hidden(
	LIBAROMA_CONTROLP ctl,byte hidden){
  _LIBAROMA_CTL_CHECK(
		_libaroma_ctl_image_handler, _LIBAROMA_CTL_IMAGEP, 0
	);
{'fraction_non_alphanumeric': 0.13779119930974978, 'fraction_numerical': 0.010353753235547885, 'mean_word_length': 2.435388263189093, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 188, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2338327', 'n_tokens_mistral': 4541, 'n_tokens_neox': 4257, 'n_words': 837}